-
Notifications
You must be signed in to change notification settings - Fork 2
/
sms_rewt.py
231 lines (185 loc) · 8.02 KB
/
sms_rewt.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import torch
import sys
import numpy as np
from deep_net import *
from logistic_regression import *
from sklearn.metrics import f1_score
from weighted_cage import *
from sklearn.feature_extraction.text import TfidfVectorizer
from losses import *
import pickle
from torch.utils.data import TensorDataset, DataLoader
#sys.path.append(r'.')
run = 1
torch.set_default_dtype(torch.float64)
torch.set_printoptions(threshold=20)
objs = []
with open('./Data/SMS/d_processed.p', 'rb') as f:
while 1:
try:
o = pickle.load(f)
except EOFError:
break
objs.append(o)
x_supervised = torch.tensor(objs[0]).double()
y_supervised = torch.tensor(objs[3]).long()
l_supervised = torch.tensor(objs[2]).long()
s_supervised = torch.tensor(objs[2]).double()
objs = []
with open('./Data/SMS/U_processed.p', 'rb') as f:
while 1:
try:
o = pickle.load(f)
except EOFError:
break
objs.append(o)
x_unsupervised = torch.tensor(objs[0]).double()
y_unsupervised = torch.tensor(objs[3]).long()
l_unsupervised = torch.tensor(objs[2]).long()
s_unsupervised = torch.tensor(objs[2]).double()
covered_indices = l_unsupervised.sum(1).nonzero().squeeze()
x_unsupervised = x_unsupervised[covered_indices]
y_unsupervised = y_unsupervised[covered_indices]
l_unsupervised = l_unsupervised[covered_indices]
s_unsupervised = s_unsupervised[covered_indices]
objs = []
with open('./Data/SMS/validation_processed.p', 'rb') as f:
while 1:
try:
o = pickle.load(f)
except EOFError:
break
objs.append(o)
x_valid = torch.tensor(objs[0]).double()[-69:]
y_valid = torch.tensor(objs[3]).long()[-69:]
l_valid = torch.tensor(objs[2]).long()[-69:]
s_valid = torch.tensor(objs[2]).double()[-69:]
objs1 = []
with open('./Data/SMS/test_processed.p', 'rb') as f:
while 1:
try:
o = pickle.load(f)
except EOFError:
break
objs1.append(o)
x_test = torch.tensor(objs1[0]).double()
y_test = objs1[3]
l_test = torch.tensor(objs1[2]).long()
s_test = torch.tensor(objs1[2]).double()
n_classes = 2
n_lfs = 73
n_features = x_supervised.shape[1]
n_hidden = 512
k = torch.from_numpy(np.array([1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0])).long()
k = 1 - k
continuous_mask = torch.zeros(n_lfs).double()
a = torch.ones(n_lfs).double() * 0.9
for i in range(s_supervised.shape[0]):
for j in range(s_supervised.shape[1]):
if s_supervised[i, j].item() > 0.999:
s_supervised[i, j] = 0.999
if s_supervised[i, j].item() < 0.001:
s_supervised[i, j] = 0.001
for i in range(s_unsupervised.shape[0]):
for j in range(s_unsupervised.shape[1]):
if s_unsupervised[i, j].item() > 0.999:
s_unsupervised[i, j] = 0.999
if s_unsupervised[i, j].item() < 0.001:
s_unsupervised[i, j] = 0.001
for i in range(s_valid.shape[0]):
for j in range(s_valid.shape[1]):
if s_valid[i, j].item() > 0.999:
s_valid[i, j] = 0.999
if s_valid[i, j].item() < 0.001:
s_valid[i, j] = 0.001
for i in range(s_test.shape[0]):
for j in range(s_test.shape[1]):
if s_test[i, j].item() > 0.999:
s_test[i, j] = 0.999
if s_test[i, j].item() < 0.001:
s_test[i, j] = 0.001
l = torch.cat([l_supervised, l_unsupervised])
s = torch.cat([s_supervised, s_unsupervised])
pi_y = torch.ones(n_classes).double()
x_train = torch.cat([x_supervised, x_unsupervised])
y_train = torch.cat([y_supervised, y_unsupervised])
supervised_mask = torch.cat([torch.ones(l_supervised.shape[0]), torch.zeros(l_unsupervised.shape[0])])
pi = torch.ones((n_classes, n_lfs)).double()
pi.requires_grad = True
theta = torch.ones((n_classes, n_lfs)).double() * 1
theta.requires_grad = True
pi_y.requires_grad = True
lr_model = DeepNet(n_features, n_hidden, n_classes)
#lr_model = LogisticRegression(n_features, n_classes)
# optimizer = torch.optim.Adam([{"params": nn_model.parameters()}, {"params": [pi, pi_y, theta]}], lr=0.001)
optimizer_lr = torch.optim.Adam(lr_model.parameters(), lr=0.0001)
optimizer_gm = torch.optim.Adam([theta, pi, pi_y], lr=0.01, weight_decay=0)
# optimizer = torch.optim.Adam([theta, pi, pi_y], lr=0.01, weight_decay=0)
supervised_criterion = torch.nn.CrossEntropyLoss()
dataset = TensorDataset(x_train, y_train, l, s, supervised_mask)
loader = DataLoader(dataset, batch_size=256, shuffle=True, pin_memory=True)
best_score = 0
best_epoch = 0
weights = torch.ones(k.shape[0])
for epoch in range(100):
# nn_model.train()
lr_model.train()
for batch_ndx, sample in enumerate(loader):
optimizer_lr.zero_grad()
optimizer_gm.zero_grad()
supervised_indices = sample[4].nonzero().view(-1)
unsupervised_indices = (1 - sample[4]).nonzero().squeeze()
if len(supervised_indices) > 0:
loss_1 = supervised_criterion(lr_model(sample[0][supervised_indices]), sample[1][supervised_indices])
else:
loss_1 = 0
unsupervised_lr_probability = torch.nn.Softmax()(lr_model(sample[0][unsupervised_indices]))
loss_2 = entropy(unsupervised_lr_probability)
y_pred_unsupervised = np.argmax(
probability(theta, pi_y, pi, sample[2][unsupervised_indices], sample[3][unsupervised_indices], k, n_classes,
continuous_mask, weights).detach().numpy(), 1)
loss_3 = supervised_criterion(lr_model(sample[0][unsupervised_indices]), torch.tensor(y_pred_unsupervised))
if len(supervised_indices) > 0:
loss_4 = log_likelihood_loss_supervised(theta, pi_y, pi, sample[1][supervised_indices],
sample[2][supervised_indices], sample[3][supervised_indices], k,
n_classes,
continuous_mask, weights)
else:
loss_4 = 0
loss_5 = log_likelihood_loss(theta, pi_y, pi, sample[2][unsupervised_indices], sample[3][unsupervised_indices],
k, n_classes, continuous_mask, weights)
prec_loss = precision_loss(theta, k, n_classes, a)
probs_graphical = probability(theta, pi_y, pi, sample[2], sample[3], k, n_classes, continuous_mask, weights)
probs_graphical = (probs_graphical.t() / probs_graphical.sum(1)).t()
probs_lr = torch.nn.Softmax()(lr_model(sample[0]))
loss_6 = kl_divergence(probs_graphical, probs_lr)
loss = loss_1 + loss_2 + loss_4 + loss_6 + loss_3 + loss_5 + prec_loss
loss.backward()
optimizer_gm.step()
optimizer_lr.step()
y_pred = np.argmax(probability(theta, pi_y, pi, l_test, s_test, k, n_classes, continuous_mask, weights).detach().numpy(), 1)
print("Epoch: {}\tf1_score: {}".format(epoch, f1_score(y_test, y_pred)))
probs = torch.nn.Softmax()(lr_model(x_test))
y_pred = np.argmax(probs.detach().numpy(), 1)
print("Epoch: {}\tf1_score: {}".format(epoch, f1_score(y_test, y_pred)))
y_pred = np.argmax(probability(theta, pi_y, pi, l_valid, s_valid, k, n_classes, continuous_mask, weights).detach().numpy(),
1)
print("Epoch: {}\tf1_score(Valid): {}".format(epoch, f1_score(y_valid, y_pred)))
probs = torch.nn.Softmax()(lr_model(x_valid))
y_pred = np.argmax(probs.detach().numpy(), 1)
score = f1_score(y_valid, y_pred)
if score > best_score:
best_epoch = epoch
best_score = score
loss_type = "123456p"
#run = 2
checkpoint = {
'theta': theta,
'pi': pi,
}
# torch.save(checkpoint, "./models/sms/run {}/sms_gm_{}.pt".format(run, loss_type))
# torch.save(lr_model.state_dict(), "./models/sms/run {}/sms_lr_{}.pt".format(run, loss_type))
print("Epoch: {}\tf1_score(Valid): {}".format(epoch, f1_score(y_valid, y_pred)))
if score == 1:
break
print(best_epoch, best_score)