-
Notifications
You must be signed in to change notification settings - Fork 2
/
loss.py
62 lines (51 loc) · 2.45 KB
/
loss.py
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
#mmd loss function implementation taken from https://github.com/easezyc/deep-transfer-learning/blob/master/MUDA/MFSAN/MFSAN_2src/mmd.py
#coral loss function implementation taken from https://github.com/DenisDsh/PyTorch-Deep-CORAL/blob/master/coral.py
import torch
import numpy as np
from torch.autograd import Variable
def guassian_kernel(source, target, kernel_mul=2.0, kernel_num=5, fix_sigma=None):
n_samples = int(source.size()[0])+int(target.size()[0])
total = torch.cat([source, target], dim=0)
total0 = total.unsqueeze(0).expand(int(total.size(0)), int(total.size(0)), int(total.size(1)))
total1 = total.unsqueeze(1).expand(int(total.size(0)), int(total.size(0)), int(total.size(1)))
L2_distance = ((total0-total1)**2).sum(2)
if fix_sigma:
bandwidth = fix_sigma
else:
bandwidth = torch.sum(L2_distance.data) / (n_samples**2-n_samples)
bandwidth /= kernel_mul ** (kernel_num // 2)
bandwidth_list = [bandwidth * (kernel_mul**i) for i in range(kernel_num)]
kernel_val = [torch.exp(-L2_distance / bandwidth_temp) for bandwidth_temp in bandwidth_list]
return sum(kernel_val)#/len(kernel_val)
def mmd(source, target, kernel_mul=2.0, kernel_num=5, fix_sigma=None):
#print(source.shape, target.shape)
batch_size = int(source.size()[0])
kernels = guassian_kernel(source, target,
kernel_mul=kernel_mul, kernel_num=kernel_num, fix_sigma=fix_sigma)
XX = kernels[:batch_size, :batch_size]
YY = kernels[batch_size:, batch_size:]
XY = kernels[:batch_size, batch_size:]
YX = kernels[batch_size:, :batch_size]
loss = torch.mean(XX + YY - XY -YX)
return loss
def coral(source, target):
d = source.size(1) # dim vector
source_c = compute_covariance(source)
target_c = compute_covariance(target)
loss = torch.sum(torch.mul((source_c - target_c), (source_c - target_c)))
loss = loss / (4 * d * d)
return loss
def compute_covariance(input_data):
n = input_data.size(0) # batch_size
# Check if using gpu or cpu
if input_data.is_cuda:
device = torch.device('cuda')
else:
device = torch.device('cpu')
id_row = torch.ones(n).resize(1, n).to(device=device)
sum_column = torch.mm(id_row, input_data)
mean_column = torch.div(sum_column, n)
term_mul_2 = torch.mm(mean_column.t(), mean_column)
d_t_d = torch.mm(input_data.t(), input_data)
c = torch.add(d_t_d, (-1 * term_mul_2)) * 1 / (n - 1)
return c