-
Notifications
You must be signed in to change notification settings - Fork 3
/
lstm_net_counting_chars.py
286 lines (199 loc) · 6.67 KB
/
lstm_net_counting_chars.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
from datetime import datetime
import torch
from torch import nn, optim
from torchtext import data
from torchtext.data import BucketIterator
from data_gen_utils import gen_df
from dataframe_dataset import DataFrameDataset
import numpy as np
import random
# set random seeds for reproducibility
torch.manual_seed(12)
torch.cuda.manual_seed(12)
np.random.seed(12)
random.seed(12)
# check if cuda is enabled
USE_GPU=1
# Device configuration
device = torch.device('cuda' if (torch.cuda.is_available() and USE_GPU) else 'cpu')
def tokenize(text):
# simple tokenizer
words = text.lower().split()
return words
def accuracy(preds, y):
"""
Returns accuracy per batch, i.e. if you get 8/10 right, this returns 0.8, NOT 8
"""
# get max values along rows
_, indices = preds.max(dim=1)
# values, indices = torch.max(tensor, 0)
correct = (indices == y).float() # convert into float for division
acc = correct.sum()/len(correct)
return acc
# gen the trainning data
min_seq_len = 100
max_seq_len = 300
# numer of tokenes in vocab to generate, max 10
# it is equal the number of classes
seq_tokens = 10
n_train = 1000
n_valid = 200
train_df = gen_df(n=n_train, min_seq_len=min_seq_len,
max_seq_len=max_seq_len, seq_tokens=seq_tokens)
valid_df = gen_df(n=n_valid, min_seq_len=min_seq_len,
max_seq_len=max_seq_len, seq_tokens=seq_tokens)
print(train_df)
print(valid_df)
TEXT = data.Field(sequential=True, lower=True, tokenize=tokenize,fix_length=None)
LABEL = data.Field(sequential=False, use_vocab=False, is_target=True)
fields = {"text": TEXT, "label": LABEL}
train_ds = DataFrameDataset(train_df, fields)
valid_ds = DataFrameDataset(valid_df, fields)
# numericalize the words
TEXT.build_vocab(train_ds, min_freq=1)
print(TEXT.vocab.freqs.most_common(20))
vocab = TEXT.vocab
vocab_size = len(vocab)
batch_size = 4
train_iter = BucketIterator(
train_ds,
batch_size=batch_size,
sort_key=lambda x: len(x.text),
sort_within_batch=True,
device=device)
valid_iter = BucketIterator(
valid_ds,
batch_size=batch_size,
sort_key=lambda x: len(x.text),
sort_within_batch=True,
device=device)
#hidden size
n_hid=200
# embed size
n_embed=10
# number of layers
n_layers=1
class SeqLSTM(nn.Module):
"""
LSTM example for long sequence
"""
def __init__(self, vocab_size, output_size, embed_size, hidden_size, num_layers=1):
super().__init__()
self.embed_size = embed_size
self.hidden_size = hidden_size
self.output_size = output_size
self.num_layers = num_layers
self.embed = nn.Embedding(vocab_size, embed_size)
#after the embedding we can add dropout
self.drop = nn.Dropout(0.1)
self.lstm = nn.LSTM(embed_size, hidden_size,
num_layers, batch_first=False)
self.linear = nn.Linear(hidden_size, output_size)
def forward(self, seq):
# Embed word ids to vectors
len_seq, bs = seq.shape
w_embed = self.embed(seq)
w_embed = self.drop(w_embed)
# https://github.com/bentrevett/pytorch-sentiment-analysis/blob/master/2%20-%20Upgraded%20Sentiment%20Analysis.ipynb
output, (hidden, cell) = self.lstm(w_embed)
# use dropout
# hidden = self.drop(hidden[-1,:,:])
# hidden has size [1,batch,hid dim]
# this does .squeeze(0) now hidden has size [batch, hid dim]
last_output = output[-1, :, :]
# last_output = self.drop(last_output)
out = self.linear(last_output)
return out
# gen the trainning
min_seq_len = 100
max_seq_len = 300
# numer of tokenes in vocab to generate, max 10
# it is equal the number of classes
seq_tokens = 10
n_train = 1000
n_valid = 200
train_df = gen_df(n=n_train, min_seq_len=min_seq_len,
max_seq_len=max_seq_len, seq_tokens=seq_tokens)
valid_df = gen_df(n=n_valid, min_seq_len=min_seq_len,
max_seq_len=max_seq_len, seq_tokens=seq_tokens)
print(train_df)
print(valid_df)
TEXT = data.Field(sequential=True, lower=True, tokenize=tokenize,fix_length=None)
LABEL = data.Field(sequential=False, use_vocab=False, is_target=True)
fields = {"text": TEXT, "label": LABEL}
train_ds = DataFrameDataset(train_df, fields)
valid_ds = DataFrameDataset(valid_df, fields)
# numericalize the words
TEXT.build_vocab(train_ds, min_freq=1)
#hidden size
n_hid=200
# embed size
n_embed=20
# number of layers
n_layers=1
print("-"*80)
print(f'n_train={n_train}, n_valid={n_valid}')
print(f'min_seq_len={min_seq_len}, max_seq_len={max_seq_len}')
print(f'model params')
print(f'vocab={vocab_size}, output={seq_tokens}')
print(f'n_layers={n_layers}, n_hid={n_hid} embed={n_embed}')
model = SeqLSTM(vocab_size=vocab_size, output_size=seq_tokens,
embed_size=n_embed, hidden_size=n_hid)
model.to(device)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters())
batch_size = 16
train_iter = BucketIterator(
train_ds,
batch_size=batch_size,
sort_key=lambda x: len(x.text),
sort_within_batch=True,
device=device)
valid_iter = BucketIterator(
valid_ds,
batch_size=batch_size,
sort_key=lambda x: len(x.text),
sort_within_batch=True,
device=device)
epoch_loss = 0
epoch_acc = 0
epoch = 60
for e in range(epoch):
start_time = datetime.now()
# train loop
model.train()
for batch_idx, batch in enumerate(train_iter):
# get the inputs
inputs, labels = batch
# move data to device (GPU if enabled, else CPU do nothing)
inputs, labels = inputs.to(device), labels.to(device)
model.zero_grad()
#optimizer.zero_grad()
# get model output
predictions = model(inputs)
# prediction are [batch, out_dim]
# batch.label are [1,batch] <- should be mapped to output vector
loss = criterion(predictions, labels)
epoch_loss += loss.item()
# do backward and optimization step
loss.backward()
optimizer.step()
# mean epoch loss
epoch_loss = epoch_loss / len(train_iter)
time_elapsed = datetime.now() - start_time
# evaluation loop
model.eval()
for batch_idx, batch in enumerate(valid_iter):
inputs, labels = batch
inputs, labels = inputs.to(device), labels.to(device)
# get model output
predictions = model(inputs)
# compute batch validation accuracy
acc = accuracy(predictions, labels)
epoch_acc += acc
epoch_acc = epoch_acc/len(valid_iter)
# show summary
print(
f'Epoch {e}/{epoch} loss={epoch_loss} acc={epoch_acc} time={time_elapsed}')
epoch_loss = 0
epoch_acc = 0