-
Notifications
You must be signed in to change notification settings - Fork 0
/
rnn.py
191 lines (170 loc) · 8.91 KB
/
rnn.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
import time
import torch
import torch.nn as nn
import numpy as np
import os
from torch.autograd import Variable
from torchviz import make_dot
from torch.nn import functional as F
from utils import string_to_tensor
from models import Model
# https://github.com/spro/char-rnn.pytorch/blob/master/model.py
class RNN(nn.Module):
def __init__(self, input_size, hidden_size, output_size, model="lstm", n_layers=1):
super(RNN, self).__init__()
self.model = model.lower()
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.n_layers = n_layers
self.encoder = nn.Embedding(input_size, hidden_size)
if self.model == "gru":
self.rnn = nn.GRU(hidden_size, hidden_size, n_layers)
elif self.model == "lstm":
self.rnn = nn.LSTM(hidden_size, hidden_size, n_layers)
self.decoder = nn.Linear(hidden_size, output_size)
def forward(self, input, hidden):
# input is of shape (batch_size, 1) where each input[x, 0] is the word index
# char RNN so we generate one character at a time.
batch_size = input.size(0)
encoded = self.encoder(input)
output, hidden = self.rnn(encoded.view(1, batch_size, -1), hidden)
output = self.decoder(output.view(batch_size, -1))
return output, hidden
def init_hidden(self, batch_size, device=torch.device("cpu")):
if self.model == "lstm":
return (Variable(torch.zeros(self.n_layers, batch_size,
self.hidden_size).to(device)),
Variable(torch.zeros(self.n_layers, batch_size,
self.hidden_size).to(device)))
return Variable(torch.zeros(self.n_layers, batch_size,
self.hidden_size).to(device))
class GenerativeRNN(Model):
def __init__(self, args):
Model.__init__(self, args)
self.initial_probs_tensor = []
self.initial_probs = dict(zip(self.indexes, np.ones(self.num_characters) * self.pseudo_count))
self.model = RNN(self.num_characters, self.hidden_size, self.num_characters, "lstm", self.layers)
self.model.to(self.device)
self.optimizer = torch.optim.Adam(self.model.parameters(), lr=self.learning_rate)
self.criterion = nn.CrossEntropyLoss()
def fit(self, train_dataloader, valid_dataloader, verbose=True, logger=None, save_model=True, weights=None,
**kwargs):
start_time = time.time()
self.model.train()
self.train_loss_history, self.valid_loss_history = [], []
# fit initial distribution of starting characters with la place smoothing
self.initial_probs = dict(zip(self.indexes, np.ones(self.num_characters) * self.pseudo_count))
for inp, _ in train_dataloader:
for char_index in inp[:, 0]:
self.initial_probs[char_index.item()] += 1
dataset_length = len(train_dataloader.dataset)
smoothing_count = self.num_characters * self.pseudo_count
for char_index in self.initial_probs.keys():
self.initial_probs[char_index] = self.initial_probs[char_index] / (dataset_length + smoothing_count)
# initial distribution to sample from
self.initial_probs_tensor = torch.Tensor(list(self.initial_probs.values())).to(self.device)
for epoch in range(1, self.epochs + 1):
self.model.train()
train_loss = 0
for i, (inp, target) in enumerate(train_dataloader):
batch_size, seq_length = inp.shape[0], inp.shape[1]
hidden = self.model.init_hidden(batch_size, self.device)
self.model.zero_grad()
loss = 0
for c in range(seq_length):
output, hidden = self.model(inp[:, c].to(self.device), hidden)
loss += self.criterion(output.view(batch_size, -1),
target[:, c].to(self.device)) # mean cross entropy loss
loss.backward()
self.optimizer.step()
train_loss += loss.item() * batch_size
train_loss /= len(train_dataloader.dataset)
self.train_loss_history.append(train_loss)
self.model.eval()
if valid_dataloader:
valid_loss = self.evaluate(valid_dataloader, verbose=False, logger=logger)
self.valid_loss_history.append(valid_loss)
if verbose:
print("epoch {0}, train neg log prob: {1:.4f}, test neg log probability {2:.4f}, time: {3:.2f} sec".format(
epoch, train_loss, valid_loss, time.time() - start_time), file=logger)
if epoch % self.save_epochs == 0 and save_model:
path = os.path.join(self.base_log, self.name, "{0}_checkpoint_{1}.pt".format(self.model_type, epoch))
self.save_model(path, epoch=epoch, loss=loss, initial_probs=True)
if self.early_stopping:
super().early_stop_iteration(loss, valid_loss, epoch, logger)
if self.early_stopping.early_stop:
break
def evaluate(self, dataloader, verbose=False, logger=None, weights=None, **kwargs):
total_loss = 0
for inp, target in dataloader:
batch_size, seq_length = inp.shape[0], inp.shape[1]
for starting_char_index in inp[:, 0]:
total_loss += -np.log(self.initial_probs[starting_char_index.item()]) #neg log probability of starting character
hidden = self.model.init_hidden(batch_size, self.device)
for c in range(seq_length):
output, hidden = self.model(inp[:, c].to(self.device), hidden)
total_loss += (self.criterion(output.view(batch_size, -1),
target[:, c].to(self.device)) * batch_size)
total_loss = total_loss.item() / len(dataloader.dataset)
if verbose:
print('total loss: {0:.4f}'.format(total_loss), file=logger)
return total_loss
def sample(self, num_samples, length, to_string=True, **kwargs):
hidden = self.model.init_hidden(num_samples, self.device)
input = torch.multinomial(input=self.initial_probs_tensor,
num_samples=num_samples, replacement=True)
predicted_strings = input.reshape(1,
num_samples).long()
sampled_probabilities = torch.stack([self.initial_probs_tensor for _ in range(num_samples)])
sampled_probabilities = sampled_probabilities.reshape(1, num_samples, self.num_characters)
for i in range(1, length):
output, hidden = self.model(input.view(num_samples,
1).to(self.device), hidden)
output = F.softmax(output, dim=-1)
input = torch.Tensor([torch.multinomial(input=prob, num_samples=1,
replacement=True)[0] for prob in
output]).long().to(self.device)
sampled_probabilities = torch.cat([sampled_probabilities, output.reshape(1, num_samples, self.num_characters)])
predicted_strings = torch.cat([predicted_strings, input.reshape(1, num_samples)])
sampled_probabilities = sampled_probabilities.permute(1, 0, 2)
predicted_strings = predicted_strings.permute(1, 0).cpu().detach().numpy()
if to_string:
sampled_strings = []
for string in predicted_strings:
sampled_strings.append("".join([self.int_to_character[index] for index in string]))
return sampled_strings
else:
return sampled_probabilities.cpu().detach().numpy()
def show_model(self, logger=None, **kwargs):
print(self.model, file=logger)
def plot_model(self, save_fig_dir, show=False, **kwargs):
hidden = self.model.init_hidden(1, self.device)
out, _ = self.model(string_to_tensor("S",
self.character_to_int).to(self.device), hidden)
graph = make_dot(out)
if save_fig_dir is not None:
graph.format = "png"
graph.render(save_fig_dir)
if show:
graph.view()
def save_model(self, path, **kwargs):
d = dict()
d['model_state_dict'] = self.model.state_dict()
d['optimizer_state_dict'] = self.optimizer.state_dict()
if 'initial_probs' in kwargs:
d['initial_probs'] = self.initial_probs
if 'epoch' in kwargs:
d['epoch'] = kwargs['epoch']
if 'loss' in kwargs:
d['loss'] = kwargs
torch.save(d, path)
def load_model(self, path, **kwargs):
saved_dict = torch.load(path)
self.model.load_state_dict(saved_dict["model_state_dict"])
self.optimizer.load_state_dict(saved_dict["optimizer_state_dict"])
if 'initial_probs' in kwargs:
self.initial_probs = saved_dict['initial_probs']
self.initial_probs_tensor = torch.Tensor(list(self.initial_probs.values()))
def plot_history(self, save_fig_dir, **kwargs):
super().plot_history(save_fig_dir=save_fig_dir, **kwargs)