Implementing contrastive loss and triplet loss in Tensorflow

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

I started to play with TensorFlow two days ago and I'm wondering if there is the triplet and the contrastive losses implemented. Home Public Questions Tags Users Companies Collectives ExploreCollectives Teams StackOverflowforTeams –Startcollaboratingandsharingorganizationalknowledge. CreateafreeTeam WhyTeams? Teams CreatefreeTeam Collectives™onStackOverflow Findcentralized,trustedcontentandcollaboratearoundthetechnologiesyouusemost. LearnmoreaboutCollectives Teams Q&Aforwork Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch. LearnmoreaboutTeams ImplementingcontrastivelossandtripletlossinTensorflow AskQuestion Asked 6years,3monthsago Modified 12monthsago Viewed 38ktimes 39 New!Savequestionsoranswersandorganizeyourfavoritecontent.Learnmore. IstartedtoplaywithTensorFlowtwodaysagoandI'mwonderingifthereisthetripletandthecontrastivelossesimplemented. I'vebeenlookingatthedocumentation,butIhaven'tfoundanyexampleordescriptionaboutthesethings. tensorflowdeep-learning Share Improvethisquestion Follow editedMar20,2018at4:49 OlivierMoindrot 27.5k1111goldbadges8989silverbadges8989bronzebadges askedJul8,2016at6:20 TiagoFreitasPereiraTiagoFreitasPereira 68011goldbadge88silverbadges1010bronzebadges Addacomment  |  3Answers 3 Sortedby: Resettodefault Highestscore(default) Trending(recentvotescountmore) Datemodified(newestfirst) Datecreated(oldestfirst) 86 Update(2018/03/19):IwroteablogpostdetailinghowtoimplementtripletlossinTensorFlow. Youneedtoimplementyourselfthecontrastivelossorthetripletloss,butonceyouknowthepairsortripletsthisisquiteeasy. ContrastiveLoss Supposeyouhaveasinputthepairsofdataandtheirlabel(positiveornegative,i.e.sameclassordifferentclass).Forinstanceyouhaveimagesasinputofsize28x28x1: left=tf.placeholder(tf.float32,[None,28,28,1]) right=tf.placeholder(tf.float32,[None,28,28,1]) label=tf.placeholder(tf.int32,[None,1]).#0ifsame,1ifdifferent margin=0.2 left_output=model(left)#shape[None,128] right_output=model(right)#shape[None,128] d=tf.reduce_sum(tf.square(left_output-right_output),1) d_sqrt=tf.sqrt(d) loss=label*tf.square(tf.maximum(0.,margin-d_sqrt))+(1-label)*d loss=0.5*tf.reduce_mean(loss) TripletLoss Sameaswithcontrastiveloss,butwithtriplets(anchor,positive,negative).Youdon'tneedlabelshere. anchor_output=...#shape[None,128] positive_output=...#shape[None,128] negative_output=...#shape[None,128] d_pos=tf.reduce_sum(tf.square(anchor_output-positive_output),1) d_neg=tf.reduce_sum(tf.square(anchor_output-negative_output),1) loss=tf.maximum(0.,margin+d_pos-d_neg) loss=tf.reduce_mean(loss) TherealtroublewhenimplementingtripletlossorcontrastivelossinTensorFlowishowtosamplethetripletsorpairs.Iwillfocusongeneratingtripletsbecauseitisharderthangeneratingpairs. TheeasiestwayistogeneratethemoutsideoftheTensorflowgraph,i.e.inpythonandfeedthemtothenetworkthroughtheplaceholders.Basicallyyouselectimages3atatime,withthefirsttwofromthesameclassandthethirdfromanotherclass.Wethenperformafeedforwardonthesetriplets,andcomputethetripletloss. Theissuehereisthatgeneratingtripletsiscomplicated.Wewantthemtobevalidtriplets,tripletswithapositiveloss(otherwisethelossis0andthenetworkdoesn'tlearn). Toknowwhetheratripletisgoodornotyouneedtocomputeitsloss,soyoualreadymakeonefeedforwardthroughthenetwork... Clearly,implementingtripletlossinTensorflowishard,andtherearewaystomakeitmoreefficientthansamplinginpythonbutexplainingthemwouldrequireawholeblogpost! Share Improvethisanswer Follow editedMar20,2018at4:43 answeredJul8,2016at15:23 OlivierMoindrotOlivierMoindrot 27.5k1111goldbadges8989silverbadges8989bronzebadges 13 Hi@Olivier,Iamveryinterestedinthesamplingpart.Wouldyouorhaveyoupostedablogforit?Iamdoingwhatjustasyousaid,tofeedforwardonce,andcomputethelossesforallpossibletriplets,filteroutinvalidones,andsampleabatchtodoanotherforward+backward... – weitang114 Aug2,2016at6:13 Didn'twriteanyblogpost.OnekeyinsightistocomputeallthepossibletripletsasexplainedinOpenFace,myanswerabovecontainstheoldsolution.Toremovethemiddlesess.run()call,youcanaddatf.py_funcoperationinsidethegraphtofilteroutthebadtriplets. – OlivierMoindrot Aug8,2016at21:49 1 @weitang114:Anotherwayforthe2ndpartistojustcomputethelossforallthetriplets,removingonlytheinvalidtriplets(i.e.(+,+,+)),whichcanbecomputedinadvance.Thisconvergeswell,surprisingly. – OlivierMoindrot Aug8,2016at21:52 thankyouforthisadvice.Ididn'tgettheideathatmoment,butfounditveryusefulrecently.Thisprocessimplementedintfhelpedmereduceatrainingtimefrom5daysto1day.:) – weitang114 Oct29,2016at14:05 3 @HelloLili:Ifinallywrotethatblogpost.Hereitis:omoindrot.github.io/triplet-loss – OlivierMoindrot Mar20,2018at4:41  |  Show8morecomments 14 Tripletlosswithsemihardnegativeminingisnowimplementedintf.contrib,asfollows: triplet_semihard_loss( labels, embeddings, margin=1.0 ) where: Args: labels:1-Dtf.int32Tensorwithshape[batch_size]ofmulticlass integerlabels. embeddings:2-DfloatTensorofembeddingvectors.Embeddingsshould bel2normalized. margin:Float,marginterminthelossdefinition. Returns: triplet_loss:tf.float32scalar. Forfurtherinformation,checkthelinkbellow: https://www.tensorflow.org/versions/master/api_docs/python/tf/contrib/losses/metric_learning/triplet_semihard_loss Share Improvethisanswer Follow editedJan10,2018at3:04 8-BitBorges 9,2332626goldbadges8787silverbadges172172bronzebadges answeredJan10,2018at1:49 CoreyLynchCoreyLynch 14111silverbadge44bronzebadges 2 1 Linkonlyanswers?Includesomerelevantportionsfromthelinkhere. – Sunil Jan10,2018at2:06 1 Whilethislinkmightprovidesomelimited,immediatehelp,ananswershouldincludesufficientcontextaroundthelinksoyourfellowuserswillhavesomeideawhatitisandwhyit’sthere.Alwaysquotethemostrelevantpartofanimportantlink,tomakeitmoreusefultofuturereaderswithother,similarquestions.Inaddition,otheruserstendtorespondnegativelytoanswerswhicharebarelymorethanalinktoanexternalsite,andtheymightbedeleted. – Machavity ♦ Jan10,2018at2:07 Addacomment  |  5 Tiago,Idon'tthinkyouareusingthesameformulaOliviergave. Hereistherightcode(notsureitwillworkthough,justfixingtheformula): defcompute_euclidean_distance(x,y): """ Computestheeuclideandistancebetweentwotensorflowvariables """ d=tf.reduce_sum(tf.square(tf.sub(x,y)),1) returnd defcompute_contrastive_loss(left_feature,right_feature,label,margin): """ Computethecontrastivelossasin L=0.5*Y*D^2+0.5*(Y-1)*{max(0,margin-D)}^2 **Parameters** left_feature:Firstelementofthepair right_feature:Secondelementofthepair label:Labelofthepair(0or1) margin:Contrastivemargin **Returns** Returnthelossoperation """ label=tf.to_float(label) one=tf.constant(1.0) d=compute_euclidean_distance(left_feature,right_feature) d_sqrt=tf.sqrt(compute_euclidean_distance(left_feature,right_feature)) first_part=tf.mul(one-label,d)#(Y-1)*(d) max_part=tf.square(tf.maximum(margin-d_sqrt,0)) second_part=tf.mul(label,max_part)#(Y)*max(margin-d,0) loss=0.5*tf.reduce_mean(first_part+second_part) returnloss Share Improvethisanswer Follow editedOct20,2017at16:24 Engineero 11.7k55goldbadges5151silverbadges7373bronzebadges answeredJul16,2016at23:51 WassimGrWassimGr 54811goldbadge77silverbadges1616bronzebadges 8 HiWasssim,thanksforthefix,justapatchinyourcode.d_sqrt=tf.sqrt(compute_euclidean_distance(left_feature,right_feature))Butevenwiththisfix,Igetverylowaccuracy(butthelossdecreasesasexpected). – TiagoFreitasPereira Jul17,2016at18:34 @TiagoFreitasPereiraIamhavingthesameproblemwithmytripletlossimplementation.IwillnotifyyouifIfindasolution... – WassimGr Jul17,2016at18:52 Hey@Wassim,thanks.Ifitiseasier,youcantrytobootstrapmyproject(github.com/tiagofrepereira2012/examples.tensorflow). – TiagoFreitasPereira Jul18,2016at4:37 1 @TiagoFreitasPereira,itseemslikeithastodowiththewayweimplementtheaccuracycomputation.LookslikewhenusingTripletLossorContrastiveLossyoucan'tcomputeaccuracyusinglabelverification(becausethenetworkwasn'ttrainedtodifferentiatethe10classes),however,youhavetocomputeaccuracybyevaluatingwhetherthenetworkguessedthattwoelementsarefromthesameclassornot. – WassimGr Jul18,2016at18:21 Seesection4and5.6ofthispaperarxiv.org/pdf/1503.03832v3.pdf – WassimGr Jul19,2016at2:17  |  Show3morecomments YourAnswer ThanksforcontributingananswertoStackOverflow!Pleasebesuretoanswerthequestion.Providedetailsandshareyourresearch!Butavoid…Askingforhelp,clarification,orrespondingtootheranswers.Makingstatementsbasedonopinion;backthemupwithreferencesorpersonalexperience.Tolearnmore,seeourtipsonwritinggreatanswers. Draftsaved Draftdiscarded Signuporlogin SignupusingGoogle SignupusingFacebook SignupusingEmailandPassword Submit Postasaguest Name Email Required,butnevershown PostYourAnswer Discard Byclicking“PostYourAnswer”,youagreetoourtermsofservice,privacypolicyandcookiepolicy Nottheansweryou'relookingfor?Browseotherquestionstaggedtensorflowdeep-learningoraskyourownquestion. TheOverflowBlog Bewarethescammersposingastechrecruiters(Ep.497) Fasterfeedbackloopsmakeforfasterdevelopervelocity FeaturedonMeta BookmarkshaveevolvedintoSaves Inboximprovements:markingnotificationsasread/unread,andafiltered... CollectivesUpdate:RecognizedMembers,Articles,andGitLab The[script]tagisbeingburninated StagingGroundWorkflow:CannedComments Linked 5 Howtodetermineaccuracywithtripletlossinaconvolutionalneuralnetwork 4 HowtomakeDatasetfortripletloss 1 Lossdecreaseswhenusingsemihardtriplets 0 Deeplearningmodeltofindsimilarimages(localitysensitivehashing) 0 Onlinetripetgeneration-amIdoingitright? 1 tf.nn.l2_normalizemakesaccurracylower Related 150 Whatdoestf.nn.conv2ddointensorflow? 112 TensorFlow,"'module'objecthasnoattribute'placeholder'" 418 Whatisthedifferencebetween'SAME'and'VALID'paddingintf.nn.max_pooloftensorflow? 398 UnderstandingKerasLSTMs 679 TensorFlownotfoundusingpip 45 WhatistherelationshipbetweenstepsandepochsinTensorFlow? 766 YourCPUsupportsinstructionsthatthisTensorFlowbinarywasnotcompiledtouse:AVXAVX2 7 ImplementingIntersectionoverUnionLossUsingTensorflow HotNetworkQuestions Wouldhumanintelligenceevolveifhumanshadaccesstoinfinitefood? Whatwasthepre-ChristianinterpretationofPsalm2:7? HowdoIunscrewthisscrewtodisassembleadresser? Anytipsforcleaninganextremelyoldviolin Whatisthe"YOULEDTHEMTOUS??!!!"plotdevicecalled? Isalawyerallowedtofollowaclient'sinstructionstohideevidence? TikZ-isitpossibletoapplyatransformationtoanimage? Whenaresemanticallynon-hostileexpressionsequivalenttopragmaticallyhostileones? GeneralizedCLTforanyoperation IfmyICisblownthedecouplingcapacitorisshortwhenItestwithamultimeter Isthereanywayofmakingevilmodeworkinsidelist-packages? Whyarephononsquantized? Triangularpolkadotnumbers IsthereaFrenchequivalentforFYI(foryourinformation)? Wouldcopper-basedbloodbemoreeffectiveatcarryingoxygenratherthaniron-based?Orwouldithinderthedeliveryofoxygen? Terminology:isthe"normalacceleration"(nz)consideredtobe1G,or0,whenanaircraftisatrestonthegroundwithfuselagehorizontal? Makeeusing1,2,3,4,5,6,7,8,9 Doesarandomnumbergeneratorhaverealentropy? Whatcharacteristicsdoesmydeadlyriverneedsopeoplecan'tfindthebodiesofthosewhodrowninit? ShouldwetakeintoaccountscopechangestotheSprinttomeasuretheteamefficiency? Post-publication"marketing".Isitnecessary? WhydidTruss'splannedremovalofataxincreasecausethemarket/poundtotank? Multiplicityofarootofapolynomial ContractionsinEnglish morehotquestions Questionfeed SubscribetoRSS Questionfeed TosubscribetothisRSSfeed,copyandpastethisURLintoyourRSSreader. lang-py Yourprivacy Byclicking“Acceptallcookies”,youagreeStackExchangecanstorecookiesonyourdeviceanddiscloseinformationinaccordancewithourCookiePolicy. Acceptallcookies Customizesettings  



請為這篇文章評分?