Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F90720641
losses.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Mon, Nov 4, 04:08
Size
679 B
Mime Type
text/x-python
Expires
Wed, Nov 6, 04:08 (2 d)
Engine
blob
Format
Raw Data
Handle
22124778
Attached To
R11789 DED Contrastive Learning
losses.py
View Options
import
torch
import
torch.nn
as
nn
import
torch.nn.functional
as
F
class
TripletLoss
(
nn
.
Module
):
"""
Triplet loss
Takes embeddings of an anchor sample, a positive sample and a negative sample
"""
def
__init__
(
self
,
margin
):
super
(
TripletLoss
,
self
)
.
__init__
()
self
.
margin
=
margin
def
forward
(
self
,
anchor
,
positive
,
negative
,
size_average
=
True
):
distance_positive
=
(
anchor
-
positive
)
.
pow
(
2
)
.
sum
(
1
)
# .pow(.5)
distance_negative
=
(
anchor
-
negative
)
.
pow
(
2
)
.
sum
(
1
)
# .pow(.5)
losses
=
F
.
relu
(
distance_positive
-
distance_negative
+
self
.
margin
)
return
losses
.
mean
()
if
size_average
else
losses
.
sum
()
Event Timeline
Log In to Comment