addons/contrastive.py at master · tensorflow/addons - GitHub
文章推薦指數: 80 %
"""Implements contrastive loss.""" import tensorflow as tf. from typeguard import typechecked. from tensorflow_addons.utils.keras_utils import ...
Skiptocontent
{{message}}
tensorflow
/
addons
Public
Notifications
Fork
589
Star
1.6k
Code
Issues
208
Pullrequests
39
Actions
Projects
2
Security
Insights
More
Code
Issues
Pullrequests
Actions
Projects
Security
Insights
Permalink
master
Branches
Tags
Couldnotloadbranches
Nothingtoshow
{{refName}}
default
Couldnotloadtags
Nothingtoshow
{{refName}}
default
Atagalreadyexistswiththeprovidedbranchname.ManyGitcommandsacceptbothtagandbranchnames,socreatingthisbranchmaycauseunexpectedbehavior.Areyousureyouwanttocreatethisbranch?
addons/tensorflow_addons/losses/contrastive.py
/
Jumpto
contrastive_loss
Function
ContrastiveLoss
Class
__init__
Function
Gotofile
Gotofile
T
Gotoline
L
Gotodefinition
R
Copypath
Copypermalink
Thiscommitdoesnotbelongtoanybranchonthisrepository,andmaybelongtoaforkoutsideoftherepository.
Cannotretrievecontributorsatthistime
120lines(99sloc)
4.52KB
Raw
Blame
Editthisfile
E
OpeninGitHubDesktop
OpenwithDesktop
Viewraw
Viewblame
ThisfilecontainsbidirectionalUnicodetextthatmaybeinterpretedorcompileddifferentlythanwhatappearsbelow.Toreview,openthefileinaneditorthatrevealshiddenUnicodecharacters.
LearnmoreaboutbidirectionalUnicodecharacters
Showhiddencharacters
#Copyright2019TheTensorFlowAuthors.AllRightsReserved.
#
#LicensedundertheApacheLicense,Version2.0(the"License");
#youmaynotusethisfileexceptincompliancewiththeLicense.
#YoumayobtainacopyoftheLicenseat
#
#http://www.apache.org/licenses/LICENSE-2.0
#
#Unlessrequiredbyapplicablelaworagreedtoinwriting,software
#distributedundertheLicenseisdistributedonan"ASIS"BASIS,
#WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.
#SeetheLicenseforthespecificlanguagegoverningpermissionsand
#limitationsundertheLicense.
#==============================================================================
"""Implementscontrastiveloss."""
importtensorflowastf
fromtypeguardimporttypechecked
fromtensorflow_addons.utils.keras_utilsimportLossFunctionWrapper
fromtensorflow_addons.utils.typesimportTensorLike,Number
@tf.keras.utils.register_keras_serializable(package="Addons")
@tf.function
defcontrastive_loss(
y_true:TensorLike,y_pred:TensorLike,margin:Number=1.0
)->tf.Tensor:
r"""Computesthecontrastivelossbetween`y_true`and`y_pred`.
Thislossencouragestheembeddingtobeclosetoeachotherfor
thesamplesofthesamelabelandtheembeddingtobefarapartatleast
bythemarginconstantforthesamplesofdifferentlabels.
Theeuclideandistances`y_pred`betweentwoembeddingmatrices
`a`and`b`withshape`[batch_size,hidden_size]`canbecomputed
asfollows:
>>>a=tf.constant([[1,2],
...[3,4],
...[5,6]],dtype=tf.float16)
>>>b=tf.constant([[5,9],
...[3,6],
...[1,8]],dtype=tf.float16)
>>>y_pred=tf.linalg.norm(a-b,axis=1)
>>>y_pred
延伸文章資訊
- 1【tensorflow】关于tensorflow的losses.contrastive_loss解析
Contrastive Loss 在传统的siamese network中一般使用Contrastive Loss作为损失函数,这种损失函数可以有效的处理孪生神经网络中的paired data的关系。
- 2tfa.losses.contrastive_loss | TensorFlow Addons
Computes the contrastive loss between y_true and y_pred . ... This loss encourages the embedding ...
- 3Supervised Contrastive Learning - Keras
Note that this example requires TensorFlow Addons, which you can install using the ... Supervised...
- 4addons/contrastive.py at master · tensorflow/addons - GitHub
"""Implements contrastive loss.""" import tensorflow as tf. from typeguard import typechecked. fr...
- 5simCLR contrastive loss 实现 - 知乎专栏
simCLR: contrastive loss 计算样本之前的similarity: 计算NT-Xent loss: import tensorflow as tf import numpy ...