-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.py
executable file
·85 lines (67 loc) · 2.29 KB
/
utils.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'''Some utility functions
'''
import os
import sys
import time
import datetime
import math
import torch
import torch.nn as nn
import torch.nn.init as init
import torch.nn.functional as F
import numpy as np
import random
import scipy.io
import torch
def one_hot_tensor(y_batch_tensor, num_classes, device):
y_tensor = torch.cuda.FloatTensor(y_batch_tensor.size(0),
num_classes).fill_(0)
y_tensor[np.arange(len(y_batch_tensor)), y_batch_tensor] = 1.0
return y_tensor
def label_smoothing(y_batch_tensor, num_classes, delta):
y_batch_smooth = (1 - delta - delta / (num_classes - 1)) * \
y_batch_tensor + delta / (num_classes - 1)
return y_batch_smooth
def str2bool(v):
return v.lower() in ("yes", "true", "t", "1")
class softCrossEntropy(nn.Module):
def __init__(self, reduce=True):
super(softCrossEntropy, self).__init__()
self.reduce = reduce
return
def forward(self, inputs, targets):
"""
:param inputs: predictions
:param targets: target labels in vector form
:return: loss
"""
log_likelihood = -F.log_softmax(inputs, dim=1)
sample_num, class_num = targets.shape
if self.reduce:
loss = torch.sum(torch.mul(log_likelihood, targets)) / sample_num
else:
loss = torch.sum(torch.mul(log_likelihood, targets), 1)
return loss
class CWLoss(nn.Module):
def __init__(self, num_classes, margin=50, reduce=True):
super(CWLoss, self).__init__()
self.num_classes = num_classes
self.margin = margin
self.reduce = reduce
return
def forward(self, logits, targets):
"""
:param inputs: predictions
:param targets: target labels
:return: loss
"""
onehot_targets = one_hot_tensor(targets, self.num_classes,
targets.device)
self_loss = torch.sum(onehot_targets * logits, dim=1)
other_loss = torch.max(
(1 - onehot_targets) * logits - onehot_targets * 1000, dim=1)[0]
loss = -torch.sum(torch.clamp(self_loss - other_loss + self.margin, 0))
if self.reduce:
sample_num = onehot_targets.shape[0]
loss = loss / sample_num
return loss