tfa.losses.ContrastiveLoss | TensorFlow Addons
文章推薦指數: 80 %
Computes the contrastive loss between y_true and y_pred . ... This loss encourages the embedding to be close to each other for the samples of the ...
Install
Learn
Introduction
NewtoTensorFlow?
TensorFlow
ThecoreopensourceMLlibrary
ForJavaScript
TensorFlow.jsforMLusingJavaScript
ForMobile&Edge
TensorFlowLiteformobileandedgedevices
ForProduction
TensorFlowExtendedforend-to-endMLcomponents
API
TensorFlow(v2.10.0)
Versions…
TensorFlow.js
TensorFlowLite
TFX
Resources
Models&datasets
Pre-trainedmodelsanddatasetsbuiltbyGoogleandthecommunity
Tools
EcosystemoftoolstohelpyouuseTensorFlow
Libraries&extensions
LibrariesandextensionsbuiltonTensorFlow
TensorFlowCertificateprogram
DifferentiateyourselfbydemonstratingyourMLproficiency
LearnML
EducationalresourcestolearnthefundamentalsofMLwithTensorFlow
ResponsibleAI
ResourcesandtoolstointegrateResponsibleAIpracticesintoyourMLworkflow
Community
Join
Explorewaystogetinvolved
Blog
StayuptodatewithallthingsTensorFlow
Forum↗
DiscussionplatformfortheTensorFlowcommunity
Groups
Usergroups,interestgroupsandmailinglists
Contribute
Guideforcontributingtocodeanddocumentation
WhyTensorFlow
About
Casestudies
English
中文–简体
GitHub
Signin
Addons
Overview
Guide&Tutorials
API
Install
Learn
More
API
More
Resources
More
Overview
Guide&Tutorials
API
Community
More
WhyTensorFlow
More
GitHub
tfa
Overviewregister_all
tfa.activations
Overviewgeluhardshrinklishtmishrrelusnakesoftshrinksparsemaxtanhshrink
tfa.callbacks
OverviewAverageModelCheckpointTQDMProgressBarTimeStopping
tfa.image
Overviewadjust_hsv_in_yiqangles_to_projective_transformsblendcompose_transformsconnected_componentscutoutdense_image_warpequalizeeuclidean_dist_transformgaussian_filter2dinterpolate_bilinearinterpolate_splinemean_filter2dmedian_filter2drandom_cutoutrandom_hsv_in_yiqresamplerrotatesharpnessshear_xshear_ysparse_image_warptransformtranslatetranslate_xytranslations_to_projective_transforms
tfa.layers
OverviewAdaptiveAveragePooling1DAdaptiveAveragePooling2DAdaptiveAveragePooling3DAdaptiveMaxPooling1DAdaptiveMaxPooling2DAdaptiveMaxPooling3DCRFCorrelationCostESNEmbeddingBagFilterResponseNormalizationGELUGroupNormalizationInstanceNormalizationMaxUnpooling2DMaxUnpooling2DV2MaxoutMultiHeadAttentionNoisyDensePoincareNormalizePolynomialCrossingSnakeSparsemaxSpatialPyramidPooling2DSpectralNormalizationStochasticDepthTLUWeightNormalization
tfa.losses
OverviewContrastiveLossGIoULossLiftedStructLossNpairsLossNpairsMultilabelLossPinballLossSigmoidFocalCrossEntropySparsemaxLossTripletHardLossTripletSemiHardLossWeightedKappaLosscontrastive_lossgiou_losslifted_struct_lossnpairs_lossnpairs_multilabel_losspinball_losssigmoid_focal_crossentropysparsemax_losstriplet_hard_losstriplet_semihard_loss
tfa.metrics
OverviewCohenKappaF1ScoreFBetaScoreGeometricMeanHammingLossHarmonicMeanKendallsTauMatthewsCorrelationCoefficientMeanMetricWrapperMultiLabelConfusionMatrixRSquarehamming_distancehamming_loss_fn
tfa.optimizers
OverviewAdaBeliefAdamWAveragedOptimizerWrapperCOCOBConditionalGradientCyclicalLearningRateDecoupledWeightDecayExtensionExponentialCyclicalLearningRateLAMBLazyAdamLookaheadMovingAverageMultiOptimizerNovoGradProximalAdagradRectifiedAdamSGDWSWATriangular2CyclicalLearningRateTriangularCyclicalLearningRateYogiextend_with_decoupled_weight_decay
tfa.options
Overviewdisable_custom_kernelenable_custom_kernelis_custom_kernel_disabled
tfa.rnn
OverviewESNCellLayerNormLSTMCellLayerNormSimpleRNNCellNASCellPeepholeLSTMCell
tfa.seq2seq
OverviewAttentionMechanismAttentionWrapperAttentionWrapperStateBahdanauAttentionBahdanauMonotonicAttentionBaseDecoderBasicDecoderBasicDecoderOutputBeamSearchDecoderBeamSearchDecoderOutputBeamSearchDecoderStateCustomSamplerDecoderFinalBeamSearchDecoderOutputGreedyEmbeddingSamplerInferenceSamplerLuongAttentionLuongMonotonicAttentionSampleEmbeddingSamplerSamplerScheduledEmbeddingTrainingSamplerScheduledOutputTrainingSamplerSequenceLossTrainingSamplerdynamic_decodegather_treegather_tree_from_arrayhardmaxmonotonic_attentionsafe_cumprodsequence_losstile_batch
tfa.text
OverviewCRFModelWrapperCrfDecodeForwardRnnCellcrf_binary_scorecrf_constrained_decodecrf_decodecrf_decode_backwardcrf_decode_forwardcrf_filtered_inputscrf_forwardcrf_log_likelihoodcrf_log_normcrf_multitag_sequence_scorecrf_sequence_scorecrf_unary_scoreparse_timeskip_gram_sampleskip_gram_sample_with_text_vocabviterbi_decode
crf
Overview
tfa.types
OverviewAcceptableDTypesActivationConstraintFloatTensorLikeInitializerNumberOptimizerRegularizerTensorLike
Introduction
TensorFlow
ForJavaScript
ForMobile&Edge
ForProduction
TensorFlow(v2.10.0)
Versions…
TensorFlow.js
TensorFlowLite
TFX
Models&datasets
Tools
Libraries&extensions
TensorFlowCertificateprogram
LearnML
ResponsibleAI
Join
Blog
Forum↗
Groups
Contribute
About
Casestudies
TensorFlow
Resources
Addons
API
tfa.losses.ContrastiveLoss
Stayorganizedwithcollections
Saveandcategorizecontentbasedonyourpreferences.
ViewsourceonGitHub
Computesthecontrastivelossbetweeny_trueandy_pred.
tfa.losses.ContrastiveLoss(
margin:tfa.types.Number=1.0,
reduction:str=tf.keras.losses.Reduction.SUM_OVER_BATCH_SIZE,
name:str='contrastive_loss'
)
Thislossencouragestheembeddingtobeclosetoeachotherfor
thesamplesofthesamelabelandtheembeddingtobefarapartatleast
bythemarginconstantforthesamplesofdifferentlabels.
See:http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf
Weexpectlabelsy_truetobeprovidedas1-DintegerTensor
withshape[batch_size]ofbinaryintegerlabels.Andy_predmustbe
1-DfloatTensorwithshape[batch_size]ofdistancesbetweentwo
embeddingmatrices.
Theeuclideandistancesy_predbetweentwoembeddingmatrices
aandbwithshape[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
延伸文章資訊
- 1tfa.losses.ContrastiveLoss | TensorFlow Addons
Computes the contrastive loss between y_true and y_pred . ... This loss encourages the embedding ...
- 2Supervised Contrastive Learning - Keras
Note that this example requires TensorFlow Addons, which you can install using the ... Supervised...
- 3addons/contrastive.py at master · tensorflow/addons - GitHub
"""Implements contrastive loss.""" import tensorflow as tf. from typeguard import typechecked. fr...
- 4tfa.losses.contrastive_loss | TensorFlow Addons
Computes the contrastive loss between y_true and y_pred . ... This loss encourages the embedding ...
- 5simCLR contrastive loss 实现 - 知乎专栏
simCLR: contrastive loss 计算样本之前的similarity: 计算NT-Xent loss: import tensorflow as tf import numpy ...