-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloss.py
More file actions
66 lines (51 loc) · 2.25 KB
/
loss.py
File metadata and controls
66 lines (51 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import torch
def euclidean_distance(x, y):
"""This is the squared Euclidean distance."""
return torch.sum((x - y) ** 2, dim=-1)
def approximate_hamming_similarity(x, y):
"""Approximate Hamming similarity."""
return torch.mean(torch.tanh(x) * torch.tanh(y), dim=1)
def pairwise_loss(x, y, labels, loss_type='margin', margin=1.0):
"""Compute pairwise loss.
Args:
x: [N, D] float tensor, representations for N examples.
y: [N, D] float tensor, representations for another N examples.
labels: [N] int tensor, with values in -1 or +1. labels[i] = +1 if x[i]
and y[i] are similar, and -1 otherwise.
loss_type: margin or hamming.
margin: float scalar, margin for the margin loss.
Returns:
loss: [N] float tensor. Loss for each pair of representations.
"""
labels = labels.float()
if loss_type == 'margin':
return torch.relu(margin - labels * (1 - euclidean_distance(x, y)))
elif loss_type == 'hamming':
return 0.25 * (labels - approximate_hamming_similarity(x, y)) ** 2
else:
raise ValueError('Unknown loss_type %s' % loss_type)
def triplet_loss(x_1, y, x_2, z, loss_type='margin', margin=1.0):
"""Compute triplet loss.
This function computes loss on a triplet of inputs (x, y, z). A similarity or
distance value is computed for each pair of (x, y) and (x, z). Since the
representations for x can be different in the two pairs (like our matching
model) we distinguish the two x representations by x_1 and x_2.
Args:
x_1: [N, D] float tensor.
y: [N, D] float tensor.
x_2: [N, D] float tensor.
z: [N, D] float tensor.
loss_type: margin or hamming.
margin: float scalar, margin for the margin loss.
Returns:
loss: [N] float tensor. Loss for each pair of representations.
"""
if loss_type == 'margin':
return torch.relu(margin +
euclidean_distance(x_1, y) -
euclidean_distance(x_2, z))
elif loss_type == 'hamming':
return 0.125 * ((approximate_hamming_similarity(x_1, y) - 1) ** 2 +
(approximate_hamming_similarity(x_2, z) + 1) ** 2)
else:
raise ValueError('Unknown loss_type %s' % loss_type)