-
Notifications
You must be signed in to change notification settings - Fork 8
/
cnn_pytorch.py
232 lines (181 loc) · 8.79 KB
/
cnn_pytorch.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
232
from __future__ import print_function, division
import os
import sys
import time
import pickle
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from torch.utils.data import DataLoader, TensorDataset
from sklearn.model_selection import KFold
import data_helpers
# for obtaining reproducible results
np.random.seed(0)
torch.manual_seed(0)
use_cuda = torch.cuda.is_available()
print('use_cuda = {}\n'.format(use_cuda))
mode = "nonstatic"
mode = "static"
use_pretrained_embeddings = False
use_pretrained_embeddings = True
print('MODE = {}'.format(mode))
print('EMBEDDING = {}\n'.format("pretrained" if use_pretrained_embeddings else "random"))
X, Y, vocabulary, vocabulary_inv_list = data_helpers.load_data()
vocab_size = len(vocabulary_inv_list)
sentence_len = X.shape[1]
num_classes = int(max(Y)) +1 # added int() to convert np.int64 to int
print('vocab size = {}'.format(vocab_size))
print('max sentence len = {}'.format(sentence_len))
print('num of classes = {}'.format(num_classes))
ConvMethod = "in_channel__is_embedding_dim"
ConvMethod = "in_channel__is_1"
class CNN(nn.Module):
def __init__(self, kernel_sizes=[3,4,5], num_filters=100, embedding_dim=300, pretrained_embeddings=None):
super(CNN, self).__init__()
self.kernel_sizes = kernel_sizes
self.embedding = nn.Embedding(vocab_size, embedding_dim)
self.embedding.weight.data.copy_(torch.from_numpy(pretrained_embeddings))
self.embedding.weight.requires_grad = mode=="nonstatic"
if use_cuda:
self.embedding = self.embedding.cuda()
conv_blocks = []
for kernel_size in kernel_sizes:
# maxpool kernel_size must <= sentence_len - kernel_size+1, otherwise, it could output empty
maxpool_kernel_size = sentence_len - kernel_size +1
if ConvMethod == "in_channel__is_embedding_dim":
conv1d = nn.Conv1d(in_channels = embedding_dim, out_channels = num_filters, kernel_size = kernel_size, stride = 1)
else:
conv1d = nn.Conv1d(in_channels = 1, out_channels = num_filters, kernel_size = kernel_size*embedding_dim, stride = embedding_dim)
component = nn.Sequential(
conv1d,
nn.ReLU(),
nn.MaxPool1d(kernel_size = maxpool_kernel_size)
)
if use_cuda:
component = component.cuda()
conv_blocks.append(component)
if 0:
conv_blocks.append(
nn.Sequential(
conv1d,
nn.ReLU(),
nn.MaxPool1d(kernel_size = maxpool_kernel_size)
).cuda()
)
self.conv_blocks = nn.ModuleList(conv_blocks) # ModuleList is needed for registering parameters in conv_blocks
self.fc = nn.Linear(num_filters*len(kernel_sizes), num_classes)
def forward(self, x): # x: (batch, sentence_len)
x = self.embedding(x) # embedded x: (batch, sentence_len, embedding_dim)
if ConvMethod == "in_channel__is_embedding_dim":
# input: (batch, in_channel=1, in_length=sentence_len*embedding_dim),
# output: (batch, out_channel=num_filters, out_length=sentence_len-...)
x = x.transpose(1,2) # needs to convert x to (batch, embedding_dim, sentence_len)
else:
# input: (batch, in_channel=embedding_dim, in_length=sentence_len),
# output: (batch, out_channel=num_filters, out_length=sentence_len-...)
x = x.view(x.size(0), 1, -1) # needs to convert x to (batch, 1, sentence_len*embedding_dim)
x_list= [conv_block(x) for conv_block in self.conv_blocks]
out = torch.cat(x_list, 2)
out = out.view(out.size(0), -1)
feature_extracted = out
out = F.dropout(out, p=0.5, training=self.training)
return F.softmax(self.fc(out), dim=1), feature_extracted
def evaluate(model, x_test, y_test):
inputs = Variable(x_test)
preds, vector = model(inputs)
preds = torch.max(preds, 1)[1]
if use_cuda:
preds = preds.cuda()
#eval_acc = sum(preds.data == y_test) / len(y_test) # pytorch 0.3
eval_acc = (preds.data == y_test).sum().item() / len(y_test) # pytorch 0.4
return eval_acc, vector.cpu().data.numpy()
embedding_dim = 300
num_filters = 100
kernel_sizes = [3,4,5]
batch_size = 50
def load_pretrained_embeddings():
pretrained_fpath_saved = os.path.expanduser("models/googlenews_extracted-python{}.pl".format(sys.version_info.major))
if os.path.exists(pretrained_fpath_saved):
with open(pretrained_fpath_saved, 'rb') as f:
embedding_weights = pickle.load(f)
else:
print('- Error: file not found : {}\n'.format(pretrained_fpath_saved))
print('- Please run the code "python utils.py" to generate the file first\n\n')
sys.exit()
# embedding_weights is a dictionary {word_index:numpy_array_of_300_dim}
out = np.array(list(embedding_weights.values())) # added list() to convert dict_values to a list for use in python 3
#np.random.shuffle(out)
print('embedding_weights shape:', out.shape)
# pretrained embeddings is a numpy matrix of shape (num_embeddings, embedding_dim)
return out
if use_pretrained_embeddings:
pretrained_embeddings = load_pretrained_embeddings()
else:
pretrained_embeddings = np.random.uniform(-0.01, -0.01, size=(vocab_size, embedding_dim))
def train_test_one_split(cv, train_index, test_index):
x_train, y_train = X[train_index], Y[train_index]
x_test, y_test = X[test_index], Y[test_index]
x_train = torch.from_numpy(x_train).long()
y_train = torch.from_numpy(y_train).long()
dataset_train = TensorDataset(x_train, y_train)
#train_loader = DataLoader(dataset_train, batch_size=batch_size, shuffle=True, num_workers=4, pin_memory=True)
train_loader = DataLoader(dataset_train, batch_size=batch_size, shuffle=True, num_workers=4, pin_memory=False)
x_test = torch.from_numpy(x_test).long()
y_test = torch.from_numpy(y_test).long()
if use_cuda:
x_test = x_test.cuda()
y_test = y_test.cuda()
model = CNN(kernel_sizes, num_filters, embedding_dim, pretrained_embeddings)
if cv==0:
print("\n{}\n".format(str(model)))
if use_cuda:
model = model.cuda()
parameters = filter(lambda p: p.requires_grad, model.parameters())
optimizer = torch.optim.Adam(parameters, lr=0.0002)
loss_fn = nn.CrossEntropyLoss()
for epoch in range(10):
tic = time.time()
model.train()
for i, (inputs, labels) in enumerate(train_loader):
inputs, labels = Variable(inputs), Variable(labels)
if use_cuda:
inputs, labels = inputs.cuda(), labels.cuda()
preds, _ = model(inputs)
if use_cuda:
preds = preds.cuda()
loss = loss_fn(preds, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if 0: # this does not improve the performance (even worse) (it was used in Kim's original paper)
constrained_norm = 1 # 3 original parameter
if model.fc.weight.norm().data[0] > constrained_norm:
model.fc.weight.data = model.fc.weight.data * constrained_norm / model.fc.weight.data.norm()
model.eval()
eval_acc, sentence_vector = evaluate(model, x_test, y_test)
#print('[epoch: {:d}] train_loss: {:.3f} acc: {:.3f} ({:.1f}s)'.format(epoch, loss.data[0], eval_acc, time.time()-tic) )
print('[epoch: {:d}] train_loss: {:.3f} acc: {:.3f} ({:.1f}s)'.format(epoch, loss.item(), eval_acc, time.time()-tic) ) # pytorch 0.4 and later
return eval_acc, sentence_vector
def do_cnn():
cv_folds = 10
kf = KFold(n_splits=cv_folds, shuffle=True, random_state=0)
acc_list = []
tic = time.time()
sentence_vectors, y_tests = [], []
for cv, (train_index, test_index) in enumerate(kf.split(X)):
acc, sentence_vec = train_test_one_split(cv, train_index, test_index)
print('cv = {} train size = {} test size = {}\n'.format(cv, len(train_index), len(test_index)))
acc_list.append(acc)
sentence_vectors += sentence_vec.tolist()
y_tests += Y[test_index].tolist()
print('\navg acc = {:.3f} (total time: {:.1f}s)\n'.format(sum(acc_list)/len(acc_list), time.time()-tic))
# save extracted sentence vectors in case that we can reuse it for other purpose (e.g. used as input to an SVM classifier)
# each vector can be used as a fixed-length dense vector representation of a sentence
np.save('models/sentence_vectors.npy', np.array(sentence_vectors))
np.save('models/sentence_vectors_y.npy', np.array(y_tests))
def main():
do_cnn()
if __name__ == "__main__":
main()