-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
282 lines (217 loc) · 8.94 KB
/
model.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
import torch
import numpy as np
from torch import nn
from tqdm import tqdm
from typing import Union, List
from matplotlib import pyplot as plt
from torch.nn import functional as F
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
import utils
'''
Contains all relevent torch.nn.Modules and the main
GENOME model (G.N.M. Generative Neural Model). This is a model that
can generate text based on samples of sentences.
The Trainer class just has all relevant functions for training the model.
'''
class Runner:
'''
Class:
The main predictive trainer for GNM ("Generative Neural Model" or GENOME).
Args:
corpus:
- The set of words that the model can understand.
- Required to convert arbitrary text into indexes to
be fed into the model.
name:
- The name of the file which saves the model parameters.
'''
def __init__(self, corpus, name='GNM_model'):
# For plotting the model loss
self.losses = []
self.epochs = None
self.name = name
self.corpus = corpus
self.vocab_size = len(corpus)
self.model = GNMModel(self.vocab_size)
def generate_tokens(
self,
x: str,
chunk_size=10,
training=False
) -> Union[torch.Tensor, List[str]]:
'''
Function:
Generates a set of words to complete a phrase x: (see below).
Args:
x:
- A starting phrase to complete.
chunk_size: (default: 10)
- The number of words to generate.
training: (default: False)
- Whether or not the model should generate in training mode.
'''
encoded_samples = x
if not training:
self.model.eval()
self.model.cpu()
encoded_samples = torch.tensor(np.array([utils.text2idx(x, self.corpus, autoadd=False)])).long().cpu()
generated_tokens = []
states = self.model.init_states(len(encoded_samples[0]), device='cpu')
for _ in range(chunk_size):
output, states = self.model(encoded_samples, states)
probs = F.softmax(output[0][-1], dim=0).cpu().detach().numpy()
idx = np.random.choice(len(output[0][-1]), p=probs)
generated_tokens.append(idx)
if not training:
return ' '.join([self.corpus[idx] for idx in generated_tokens]).lstrip().rstrip()
return torch.tensor(generated_tokens)
def fit_dataset(
self,
dataset: Dataset,
lr: float=0.01,
epochs: int=10,
batch_size: int=8,
chunk_size: int=5,
save_checkpoint: bool=True
) -> None:
'''
Function:
- Fits the model to a specified dataset (see Args below).
- Begins by initializing training parameters (optimizer, loss function (criterion),
and LSTM states) and begins training.
Args:
dataset:
- A class which inherits from the Dataset class (see imports). This class
is loaded into a DataLoader (see imports) which can split data into batches
for training.
- See (dataset.py) for template dataset.
lr: (default: 0.01)
- The "learning rate", which is the magnitude at which the optimizer should update
the model parameters.
- A large learning rate tends to over-shoot and a low learning rate tends to
never converge.
- See (https://www.jeremyjordan.me/nn-learning-rate/) for more info on
setting the learning rate.
epochs: (default: 10)
- How many times the model should train itself on a set of batches
(see batch_size).
batch_size: (default: 8)
- How many batches of data the model will look at in one go.
- Lower batch sizes tends to take slightly longer but MAY lead to better
results. Larger batch sizes may be quicker but the model may fail to generalize.
IT DEPENDS ON THE TYPE OF PROBLEM.
chunk_size: (default: 5)
- The number of words the model should generate.
save_checkpoint: (default: True)
- If the model should save the model parameters for later use.
'''
print('Initializing training...')
criterion = nn.CrossEntropyLoss()
ld = DataLoader(dataset, batch_size=batch_size, num_workers=0, pin_memory=True)
optimizer = torch.optim.Adam(self.model.parameters(), lr=lr, betas=(0.99, 0.999))
batch_steps = len(dataset) // batch_size
total_steps = batch_steps * epochs
pbar = tqdm(range(1, epochs + 1))
for i in pbar:
'''
states (h, c):
h : hidden state.
c : state at time: t.
'''
(h, c) = self.model.init_states(chunk_size)
for j, (x, y) in enumerate(ld):
optimizer.zero_grad()
x = x.float().to(self.model.device)
output, (h, c) = self.model(x, (h, c))
h = h.detach()
c = c.detach()
loss = criterion(
output.transpose(1, 2),
y
.float()
.to(self.model.device)
.long()
).float()
loss.backward()
optimizer.step()
loss = loss.cpu().detach().numpy()
# Shorten step size for tqdm
step_size = float(f'{(1/total_steps):.3f}')
pbar.update(step_size)
pbar.set_description(f'Batch: {j + 1}/{batch_steps} Loss: {loss:.3f}')
self.epochs = i
self.losses.append(loss)
if save_checkpoint:
self.set_device('cpu')
self.save(self.name)
def set_device(self, device: torch.device):
self.model.set_device(device)
def save(self, path: str):
torch.save(self.model.state_dict(), path)
def load(self, path: str):
self.model.load_state_dict(torch.load(path, map_location=torch.device('cpu')))
def plot_loss(self):
plt.plot(range(1, self.epochs + 1), self.losses)
plt.show()
class GNMModel(nn.Module):
'''
The main parent module
'''
def __init__(self, corpus_length):
super().__init__()
self.lstm_size = 64
self.n_lstm_layers = 100
self.dim = int(1.6 * np.sqrt(corpus_length))
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Word index -> Vector space
self.embedding = nn.Embedding(corpus_length, self.dim)
# Probability multiplies with on a word
self.attn_head = Attn(self.dim, self.lstm_size)
# Variant of
self.lstm = nn.LSTM(
input_size=self.lstm_size,
hidden_size=self.lstm_size,
num_layers=self.n_lstm_layers * 2,
dropout=.6
)
self.fc = nn.Linear(self.lstm_size, corpus_length)
# Prevent overfitting
self.dropout = nn.Dropout(.4)
if torch.backends.mps.is_built():
self.device = torch.device('mps')
def forward(self, x: torch.Tensor, prev_state) -> torch.Tensor:
# Get the 'word' vectors which are representations of an actual word index.
embeddings = self.embedding(x.int()).float()
attn = self.attn_head(embeddings)
output, state = self.lstm(attn, prev_state)
logits = self.fc(output)
return logits, state
def init_states(self, seq_length, device=None) -> tuple:
zero_state = torch.zeros(
self.n_lstm_layers * 2,
seq_length,
self.lstm_size,
device=device if device else str(self.device)
)
return (zero_state, zero_state)
def set_device(self, device: torch.device):
self.to(device)
self.device = device
class Attn(nn.Module):
'''
Class:
Outputs probability distributions multiplied with embeddings.
The Attn layer computes the importance of a word based on a softmax function.
'''
def __init__(self, n_in, n_out):
super().__init__()
self._in = n_in
self._out = n_out
self.fc = nn.Linear(n_in, 1, bias=False)
self.ext1 = nn.Linear(n_in, n_out, bias=False)
def forward(self, embeds: torch.Tensor):
weights_norm = F.softmax(embeds).float()
attn_probs = torch.multiply(weights_norm, embeds)
ext: torch.Tensor = self.ext1(attn_probs)
return ext