-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsector_classifier.py
226 lines (174 loc) · 6.13 KB
/
sector_classifier.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
from embedding import Embedding
import torch.optim as optim
import torch.nn as nn
import csv
from torch.autograd import Variable
from sklearn.model_selection import train_test_split
import torch
from collections import Counter
import unicodedata
import random
from tqdm import tqdm
def plot_losses(losses):
import matplotlib.pyplot as plt
fig, axes = plt.subplots(1, 1, figsize=(8, 8))
axes.plot(losses)
axes.set_title("train loss")
axes.set_xlabel("epochs")
axes.set_ylabel("loss")
plt.tight_layout()
plt.show()
class Net(nn.Module):
def __init__(self):
super().__init__()
self.fc = nn.Linear(768, 256)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(256, 1)
self.sig = nn.Sigmoid()
def forward(self, input_):
o = self.fc(input_)
o = self.relu(o)
o = self.fc2(o)
o = self.sig(o)
return o
LABEL_MAP = {
# filename: model_input
'chemistry': "화학",
"bio": "생명",
"IT": "정보통신",
}
def create_dataset(target, *others):
dataset = []
n_target = len(target)
X = list(range(n_target))
dataset += target
Y = [1 for _ in range(n_target)]
n_X = len(X)
_others = []
for other in others:
_others += other
dataset += other
import random
random.shuffle(_others)
n_max = min(n_X, len(_others))
X = X[:n_max]
Y = Y[:n_max]
X = X + list(range(n_X, n_X + len(_others)))
Y = Y + [0 for _ in _others]
X = X[:2 * n_max]
Y = Y[:2 * n_max]
assert len(X) == len(Y)
train, test = train_test_split(X, train_size=0.9, test_size=0.1, stratify=Y)
train = [dataset[i] for i in train]
test = [dataset[i] for i in test]
counter_train = Counter([x[1] for x in train])
counter_test = Counter([x[1] for x in test])
return train, test
def create_input(text, sector, label):
x = Embedding.get_classification_vector(text, sector)
y = 1 if sector == label else 0
return x, [y]
def load_data(filename):
data = []
with open(f'./classification/{filename}.csv') as f:
rows = csv.reader(f)
for row in rows:
data.append((row[1], filename))
return data # [(text, label)]
class MFCModel(object):
"""MoneyFine classification model"""
LABEL_MAP = {
# filename: model_input
'chemistry': "화학",
"bio": "생명",
"it": "정보통신",
}
PATH = "./classification/models/5."
def __init__(self):
self.net = Net()
self.opt = optim.Adam(self.net.parameters(), lr=0.005, betas=(0.9, 0.999))
self.criterion = nn.BCELoss()
def create_dataset(self):
train_set = {}
for k, v in self.LABEL_MAP.items():
train_set[k] = load_data(k)
return train_set
def inference(self, text):
result_map = {}
for k, v in self.LABEL_MAP.items():
tv = Embedding.get_classification_vector(text, v)
pred = self.net(tv.reshape(1, -1))
result_map[v] = float(pred)
return result_map
def __call__(self, text):
return self.inference(text)
# def train(self):
# data_map = {}
# for k, v in self.LABEL_MAP.items():
# data = load_data(k)
# data_map[k] = data
# target_data = data_map[target]
# other_data = []
# for k, v in data_map.items():
# if k != target:
# other_data.append(data_map[k])
# train, test = create_dataset(target_data, *other_data)
def _random(self):
return bool(random.choice([0, 1]))
def train(self): # LABEL_MAP key
train_set = self.create_dataset()
data = []
losses = []
for i in range(20):
for k, v in train_set.items(): # k, filename, v, data
xs = []
ys = []
for each in tqdm(v):
filename = k
text = each[0]
label = each[1]
other_labels = [k for k ,v in self.LABEL_MAP.items() if k != label]
sector = None
if self._random():
sector = self.LABEL_MAP[label]
else:
sector = self.LABEL_MAP[random.choice(other_labels)]
x, y = create_input(text, sector, self.LABEL_MAP[label])
xs.append(x)
ys.append(y)
x = torch.stack(xs)
y = torch.FloatTensor(ys)
loss = self._train_model(x, y, self.opt, self.criterion, batch_size=20)
losses.append([float(each) for each in loss])
# print(f'text={text}, label={label}, sector={sector}, y={y[0][0]}, loss={float(loss[0])}')
# plot_losses(losses)
self.save(i)
return losses
def _train_model(self, X, Y, opt, criterion, batch_size):
# X: [batch_size, 768]
# Y: [batch_size, 1]
net = self.net
net.train()
losses = []
for beg_i in range(0, X.size(0), batch_size):
x_batch = X[beg_i:beg_i + batch_size, :]
y_batch = Y[beg_i:beg_i + batch_size, :]
x_batch = Variable(x_batch)
y_batch = Variable(y_batch)
opt.zero_grad()
# (1) Forward
y_hat = net(x_batch)
# (2) Compute diff
loss = criterion(y_hat, y_batch)
# (3) Compute gradients
loss.backward()
# (4) update weights
opt.step()
losses.append(loss.data.numpy())
print(losses)
return losses
def save(self, surfix):
torch.save(self.net.state_dict(), self.PATH + str(surfix))
def load(self, surfix):
print(f"model load from {self.PATH + str(surfix)}")
self.net.load_state_dict(torch.load(self.PATH + str(surfix)))