-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_cnn.py
167 lines (152 loc) · 5.8 KB
/
train_cnn.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
# coding: utf-8
from utils import load_data,load_word_emb,embeddedTensorFromSentence, normalizeString
import numpy as np
from model import EncoderRNN, AttnDecoderRNN
import torch
from torch import optim
import torch.nn as nn
from test import evaluate, showAttention
import matplotlib.pyplot as plt
plt.switch_backend('TkAgg')
import matplotlib.ticker as ticker
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score,confusion_matrix
import pickle
device = torch.device("cpu")
N_word=100
B_word=6
hidden_size = 256
max_length = 1000
SOS_token = 0
CLASS_size = 6
word_emb = load_word_emb('../glove/glove.%dB.%dd.txt'%(B_word,N_word))
full_table, classes_, weight_tensor = load_data(device)
train_df, test_df = train_test_split(full_table, test_size=0.2)
CLASS_size = len(classes_)
class_index = range(CLASS_size)
class_dict = dict(zip(classes_, class_index))
import time
import math
def asMinutes(s):
m = math.floor(s / 60)
s -= m * 60
return '%dm %ds' % (m, s)
def timeSince(since, percent):
now = time.time()
s = now - since
es = s / (percent)
rs = es - s
return '%s (- %s)' % (asMinutes(s), asMinutes(rs))
def showPlot(points):
plt.figure()
fig, ax = plt.subplots()
# this locator puts ticks at regular intervals
loc = ticker.MultipleLocator(base=0.2)
ax.yaxis.set_major_locator(loc)
plt.plot(points)
plt.show()
def train(input_tensor, target_tensor, encoder, decoder, encoder_optimizer, decoder_optimizer, criterion, max_length=max_length):
encoder_hidden = encoder.initHidden()
input_length = len(input_tensor)
encoder_outputs = torch.zeros(max_length, encoder.hidden_size, device=device)
for ei in range(input_length):
encoder_output, encoder_hidden = encoder(input_tensor[ei],encoder_hidden)
encoder_outputs[ei] += encoder_output[0, 0]
decoder_hidden = encoder_hidden
decoder_output, decoder_hidden, decoder_attention = decoder(decoder_hidden, encoder_outputs)
topv, topi = decoder_output.topk(1)
decoder_input = topi.squeeze().detach()
loss = criterion(decoder_output, torch.max(target_tensor, 1)[1])
loss.backward()
encoder_optimizer.step()
decoder_optimizer.step()
return loss.item()
def trainIters(encoder, decoder, n_iters, print_every=1000, plot_every=100, learning_rate=0.01):
start = time.time()
plot_losses = []
print_loss_total = 0 # Reset every print_every
plot_loss_total = 0 # Reset every plot_every
encoder_optimizer = optim.SGD(encoder.parameters(), lr=learning_rate)
decoder_optimizer = optim.SGD(decoder.parameters(), lr=learning_rate)
criterion = nn.NLLLoss()#weight = weight_tensor)
for iter in range(1, n_iters + 1):
#print(iter)
sentence = train_df.iloc[iter - 1]["description"]
sentence = normalizeString(sentence)
input_tensor = embeddedTensorFromSentence(sentence,device,word_emb,N_word)
target_class = train_df.iloc[iter - 1]["department_new"]
class_index = []
for i in range(CLASS_size):
class_index.append(0)
class_index[class_dict[target_class]] = 1
#import pdb; pdb.set_trace();
print(class_index)
target_tensor = torch.tensor(class_index,dtype = torch.long ,device=device).view(1,CLASS_size)
loss = train(input_tensor, target_tensor, encoder,
decoder, encoder_optimizer, decoder_optimizer, criterion)
print_loss_total += loss
plot_loss_total += loss
if iter % print_every == 0:
print_loss_avg = print_loss_total / print_every
print_loss_total = 0
print('%s (%d %d%%) %.4f' % (timeSince(start, iter / n_iters),
iter, iter / n_iters * 100, print_loss_avg))
if iter % plot_every == 0:
plot_loss_avg = plot_loss_total / plot_every
plot_losses.append(plot_loss_avg)
plot_loss_total = 0
showPlot(plot_losses)
def evaluateTest(encoder,decoder):
test_size = test_df.shape[0]
y_true = []
y_pred = []
for iter in range(0, test_size + 1):
sentence = test_df.iloc[iter - 1]["description"]
sentence = normalizeString(sentence)
input_tensor = embeddedTensorFromSentence(sentence,device,word_emb,N_word)
target_class = test_df.iloc[iter - 1]["department_new"]
class_index = []
target_index = class_dict[target_class]
print(target_index)
y_true.append(target_index)
output, attention = evaluate(encoder, decoder, input_tensor,max_length,device)
#import pdb;pdb.set_trace();
topv, topi = output.topk(1)
y_pred.append(topi.numpy()[0][0])
#import pdb; pdb.set_trace();
cnf_matrix = confusion_matrix(y_true, y_pred)
print("Accuarcy")
print(accuracy_score(y_true, y_pred))
print(cnf_matrix)
#if __name__ == " __main__":
#import pdb;pdb.set_trace();
encoder = EncoderRNN(N_word, hidden_size).to(device)
decoder = AttnDecoderRNN(hidden_size, CLASS_size, dropout_p=0.1, max_length=max_length).to(device)
n_iterations = train_df.shape[0]
#trainIters(encoder, decoder, n_iterations, print_every=50, plot_every=10)
import pdb;pdb.set_trace();
trainIters(encoder, decoder, 1, print_every=50, plot_every=10)
sentence = train_df.iloc[0]["description"]
sentence = normalizeString(sentence)
input_tensor = embeddedTensorFromSentence(sentence,device,word_emb,N_word)
target_class = train_df.iloc[0]["department_new"]
class_index = []
target_index = class_dict[target_class]
print(target_index)
#y_true.append(target_index)
output, attention = evaluate(encoder, decoder, input_tensor,max_length,device)
#import pdb;pdb.set_trace();
topv, topi = output.topk(1)
#import pdb;pdb.set_trace();
#torch.save(encoder.state_dict(), "encoder")
#torch.save(decoder.state_dict(), "decoder")
#encoder = torch.load("encoder")
#decoder = torch.load("decoder")
#desc1 = full_table.iloc[0]["description"]
#dep1 = full_table.iloc[0]["department"]
#input_tensor = embeddedTensorFromSentence(desc1,device,word_emb,N_word)
#print(classes_)
#evaluateTest(encoder,decoder)
#import pdb;pdb.set_trace();
#output, attention = evaluate(encoder, decoder, input_tensor,max_length,device)
#showAttention(desc2, attention)