addons/contrastive.py at master · tensorflow/addons - GitHub

文章推薦指數: 80 %
投票人數:10人

"""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 See:http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf Args: y_true:1-Dinteger`Tensor`withshape`[batch_size]`of binarylabelsindicatingpositivevsnegativepair. y_pred:1-Dfloat`Tensor`withshape`[batch_size]`of distancesbetweentwoembeddingmatrices. margin:marginterminthelossdefinition. Returns: contrastive_loss:1-Dfloat`Tensor`withshape`[batch_size]`. """ y_pred=tf.convert_to_tensor(y_pred) y_true=tf.dtypes.cast(y_true,y_pred.dtype) returny_true*tf.math.square(y_pred)+(1.0-y_true)*tf.math.square( tf.math.maximum(margin-y_pred,0.0) ) @tf.keras.utils.register_keras_serializable(package="Addons") classContrastiveLoss(LossFunctionWrapper): r"""Computesthecontrastivelossbetween`y_true`and`y_pred`. Thislossencouragestheembeddingtobeclosetoeachotherfor thesamplesofthesamelabelandtheembeddingtobefarapartatleast bythemarginconstantforthesamplesofdifferentlabels. See:http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf Weexpectlabels`y_true`tobeprovidedas1-Dinteger`Tensor` withshape`[batch_size]`ofbinaryintegerlabels.And`y_pred`mustbe 1-Dfloat`Tensor`withshape`[batch_size]`ofdistancesbetweentwo embeddingmatrices. 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 Args: margin:`Float`,marginterminthelossdefinition. Defaultvalueis1.0. reduction:(Optional)Typeof`tf.keras.losses.Reduction`toapply. Defaultvalueis`SUM_OVER_BATCH_SIZE`. name:(Optional)namefortheloss. """ @typechecked def__init__( self, margin:Number=1.0, reduction:str=tf.keras.losses.Reduction.SUM_OVER_BATCH_SIZE, name:str="contrastive_loss", ): super().__init__( contrastive_loss,reduction=reduction,name=name,margin=margin ) Copylines Copypermalink Viewgitblame Referenceinnewissue Go Youcan’tperformthatactionatthistime. Yousignedinwithanothertaborwindow.Reloadtorefreshyoursession. Yousignedoutinanothertaborwindow.Reloadtorefreshyoursession.



請為這篇文章評分?