-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluka_loss_relu.py
180 lines (135 loc) · 5.56 KB
/
luka_loss_relu.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import sys
import torch
from torch import nn
from torch.autograd import Variable
from holder import *
from util import *
import numpy as np
from official_eval import *
# Lukasiewicz Loss
class LukaLoss(torch.nn.Module):
def __init__(self, opt, shared):
super(LukaLoss, self).__init__()
self.opt = opt
self.shared = shared
# do not create loss node globally
self.num_correct = 0
self.num_all = 0
self.num_ex = 0
self.verbose = False
self.all_pred = []
self.all_pos_pred = []
# for official eval of f1 in NER
self.all_pred_label = []
self.all_gold_label = []
self.idx_to_label = self.load_label_map()
self.relu = torch.nn.ReLU()
# load dict entries
self.label_str = []
with open(self.opt.label_dict, 'r') as f:
for l in f:
if l.rstrip() == '':
continue
self.label_str.append(l.rstrip().split()[0])
def count_correct_labels(self, log_p, y_gold):
assert(len(log_p.shape) == 3)
batch_l, source_l, num_label = log_p.shape
y_gold = y_gold.contiguous()
log_p = log_p.contiguous()
log_p = log_p.view(-1, num_label) # (batch_l * source_l, num_label)
y_gold = y_gold.view(-1) # (batch_l * source_l)
y_pred = np.argmax(log_p.data, axis=1) # (batch_l * source_l)
return np.equal(y_pred, y_gold).sum()
def get_label(self, log_p, y_gold):
assert(len(log_p.shape) == 3)
batch_l, source_l, num_label = log_p.shape
y_gold = y_gold.contiguous()
log_p = log_p.contiguous()
log_p = log_p.view(batch_l, source_l, num_label) # (batch_l, source_l, num_label)
y_gold = y_gold.view(batch_l, source_l) # (batch_l, source_l)
y_pred = np.argmax(log_p.data, axis=2) # (batch_l, source_l)
pred_idx = []
gold_idx = []
for ex in y_pred:
pred_idx.append([self.idx_to_label[int(l)] for l in ex])
for ex in y_gold:
gold_idx.append([self.idx_to_label[int(l)] for l in ex])
return pred_idx, gold_idx
def get_luka_loss(self, pred, gold):
# mask = torch.nn.zeros_like(gold)
correct_class_probs = torch.gather(pred, 2, gold.unsqueeze(-1))
#correct_class_probs = torch.gather(pred, 2, gold.unsqueeze(-1)).squeeze(-1)
#seq_probs = torch.prod(correct_class_probs, 1)
#return -1000000 * torch.sum(seq_probs)
const = pred.shape[0] * pred.shape[1] - 1
return (-1 * torch.sum(correct_class_probs) + const)
def get_luka_loss_relu(self, pred, gold):
correct_class_probs = torch.gather(pred, 2, gold.unsqueeze(-1))
correct_class_probs_sum_seq = torch.sum(correct_class_probs, 1)
relu_seq = self.relu(torch.sum(correct_class_probs, 1) - (pred.shape[1] - 1)) # (batch)
#print(correct_class_probs[0, :, :])
#print(correct_class_probs.shape)
#print(correct_class_probs_sum_seq)
#print(relu_seq.shape)
#print(relu_seq)
relu_batch = -1000 * self.relu(torch.sum(relu_seq) - (relu_seq.shape[0] - 1))
#print(relu_batch.shape)
#print(relu_batch)
return relu_batch
def forward(self, pred, gold):
batch_l, padded_seq_l, num_label = pred.shape
log_p = pred.contiguous()
gold = gold.contiguous()
assert(num_label == self.opt.num_label + 2)
# loss
# crit = torch.nn.NLLLoss(reduction='sum') # for pytorch < 0.4.1, use size_average=False
# crit =
# if self.opt.gpuid != -1:
# crit = crit.cuda()
flat_log_p = log_p.view(-1, num_label)
flat_gold = gold.view(-1)
# loss = crit(flat_log_p, flat_gold)
#loss = self.get_luka_loss(pred, gold)
loss = self.get_luka_loss_relu(pred, gold)
#print('Luka Loss is ' + str(loss))
# stats
batch_l = pred.shape[0]
padded_seq_l = pred.shape[1]
# when counting the labels, ignore the <bos> and <eos>
self.num_correct += self.count_correct_labels(log_p[:, 1:padded_seq_l-1, :], gold[:, 1:padded_seq_l-1])
self.num_all += batch_l * (padded_seq_l-2)
self.num_ex += self.shared.batch_l
# official eval of F1 in NER
if self.opt.use_f1 == 1:
pred_label, gold_label = self.get_label(log_p[:, 1:padded_seq_l-1, :], gold[:, 1:padded_seq_l-1])
self.all_pred_label.extend(pred_label)
self.all_gold_label.extend(gold_label)
return loss
def load_label_map(self):
idx_to_label = {}
with open(self.opt.label_dict, 'r') as f:
for l in f:
if l.rstrip() == '':
continue
toks = l.rstrip().split()
idx_to_label[int(toks[1])] = toks[0]
return idx_to_label
# return a string of stats
def print_cur_stats(self):
stats = 'Acc {0:.3f} '.format(float(self.num_correct) / self.num_all)
return stats
# get training metric (scalar metric, extra metric)
def get_epoch_metric(self):
acc = float(self.num_correct) / self.num_all
if self.opt.use_f1 == 1:
pre, rec, f1 = compute_f1(self.all_pred_label, self.all_gold_label)
return f1, [pre, rec, f1, acc]
return acc, [acc] # and any other scalar metrics
def begin_pass(self):
# clear stats
self.num_correct = 0
self.num_all = 0
self.num_ex = 0
self.all_pred = []
def end_pass(self):
pass