-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActivity7.py
324 lines (228 loc) · 9.46 KB
/
Activity7.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# --------------------------------
# %tensorflow_version 1.x
import io
import numpy as np
import tensorflow as tf
import tensorflow.python.util.deprecation as deprecation
deprecation._PRINT_DEPRECATION_WARNINGS = False
# --------------------------------
# from google.colab import drive
# drive.mount('/content/drive')
# --------------------------------
# with open('drive/My Drive/_ USI/Deep Learning Lab/datasets/montecristo.txt', 'r') as f:
# book = f.read()
with open('datasets/montecristo.txt', 'r') as f:
book = f.read()
book = book.lower()
# --------------------------------
### Characters distribution
import pandas
from collections import Counter
from collections import OrderedDict
import string
char_counts = Counter(book)
char_counts_byletter = OrderedDict(sorted(char_counts.items()))
print(f'Characters count ordered alphabetically: {char_counts_byletter}')
df_char_counts_byletter = pandas.DataFrame.from_dict(char_counts_byletter, orient='index')
df_char_counts_byletter.plot(kind='bar')
char_counts_alphabet = dict((i, char_counts_byletter[i]) for i in list(string.ascii_lowercase))
print(f'Alphabet count: {char_counts_alphabet}')
df_char_counts_alphabet = pandas.DataFrame.from_dict(char_counts_alphabet, orient='index')
df_char_counts_alphabet.plot(kind='bar')
top = 20
print(f'Top {top} most common characters')
char_counts.most_common()[:top]
# --------------------------------
#### Handle text to numerical conversion
vocab = sorted(set(book))
char2idx = {u:i for i, u in enumerate(vocab)}
idx2char = np.array(vocab)
def text_to_num(text):
return np.array([char2idx[c] for c in text])
def num_to_text(nums):
return ''.join(idx2char[np.array(nums)])
# book = book.lower() # already done before analysis
book_to_num = text_to_num(book)
# --------------------------------
def generate_batches(source, batch_size, sequence_length):
block_length = len(source) // batch_size
batches = []
for i in range(0, block_length, sequence_length):
batch=[]
for j in range(batch_size):
start = j * block_length + i
end = min(start + sequence_length, j * block_length + block_length)
batch.append(source[start:end])
batches.append(np.array(batch, dtype=int))
return batches
# --------------------------------
# ## Little example
# example_text = 'Mi chiamo Marco e sono un gattino.'.lower()
# example_num = text_to_num(example_text)
# print(example_text)
# print(example_num)
# print(generate_batches(example_num, 3, 2))
# --------------------------------
#### Model parameters
batch_size = 16
sequence_length = 256
k = len(char_counts) # Input dimension (unique characters in the text)
hidden_units = 256 # Number of recurrent units
learning_rate = 1e-2
n_epochs = 5
# --------------------------------
### Creating dataset for training
bts = generate_batches(book_to_num, batch_size, sequence_length)
print('Number of batches', len(bts)) # ceiling(len(text) / batch_size / sequence_length)
print('Batch size', len(bts[0]))
print('Sequence length', len(bts[0][0]))
# # Just to notice that last batch is incomplete
# for i in range(len(bts)):
# for j in range(batch_size):
# if len(bts[i][j]) != 256:
# print(len(bts[i][j]), i, j)
bts = np.array(bts[:-1]) # removing last batch because incomplete
print('\nbts shape: ' , bts.shape)
data_X = bts
data_Y = np.copy(data_X)
for batch in range(np.shape(bts)[0]):
for sequence in range(np.shape(bts)[1]):
for character in range(np.shape(bts)[2] - 1):
data_Y[batch][sequence][character] = data_X[batch][sequence][character+1]
data_Y[batch][sequence][np.shape(bts)[2] - 1] = 0 # last character has no target
print('data_X shape: ', data_X.shape)
print('data_Y shape: ', data_Y.shape)
# --------------------------------
### Model definition
seed = 0
tf.reset_default_graph()
tf.set_random_seed(seed=seed)
X_int = tf.placeholder(shape=[None, None], dtype=tf.int64)
Y_int = tf.placeholder(shape=[None, None], dtype=tf.int64)
lengths = tf.placeholder(shape=[None], dtype=tf.int64)
batch_size_tf = tf.shape(X_int)[0]
max_len = tf.shape(X_int)[1] # TODO
# One-hot encoding X_int
X = tf.one_hot(X_int, depth=k) # shape: (batch_size, max_len, k)
# One-hot encoding Y_int
Y = tf.one_hot(Y_int, depth=k) # shape: (batch_size, max_len, k)
# Recurrent Neural Network
basic_cell = tf.nn.rnn_cell.BasicRNNCell(num_units=hidden_units)
# Long-Short Term Memory Neural Network
rnn_layers = [tf.nn.rnn_cell.LSTMCell(size) for size in [256, 256]]
multi_rnn_cell = tf.nn.rnn_cell.MultiRNNCell(rnn_layers)
lstm_cell = tf.nn.rnn_cell.LSTMCell(num_units=hidden_units)
init_state = lstm_cell.zero_state(batch_size_tf, dtype=tf.float32)
current_state = lstm_cell.zero_state(batch_size_tf, dtype=tf.float32)
# rnn_outputs shape: (batch_size, max_len, hidden_units)
rnn_outputs, final_state = tf.nn.dynamic_rnn(lstm_cell, X, sequence_length=lengths, initial_state=current_state)
# rnn_outputs_flat shape: ((batch_size * max_len), hidden_units)
rnn_outputs_flat = tf.reshape(rnn_outputs, [-1, hidden_units])
# Weights and biases for the output layer
Wout = tf.Variable(tf.truncated_normal(shape=(hidden_units, k), stddev=0.1))
bout = tf.Variable(tf.zeros(shape=[k]))
# Z shape: ((batch_size * max_len), k)
Z = tf.matmul(rnn_outputs_flat, Wout) + bout
Y_flat = tf.reshape(Y, [-1, k]) # shape: ((batch_size * max_len), k)
# Creates a mask to disregard padding
mask = tf.sequence_mask(lengths, dtype=tf.float32)
mask = tf.reshape(mask, [-1]) # shape: (batch_size * max_len)
# Network prediction
pred = tf.squeeze(tf.random.categorical(Z, 1)) * tf.cast(mask, dtype=tf.int64)
pred = tf.reshape(pred, [-1, max_len]) # shape: (batch_size, max_len)
hits = tf.reduce_sum(tf.cast(tf.equal(pred, Y_int), tf.float32))
hits = hits - tf.reduce_sum(1 - mask) # Disregards padding
# Accuracy: correct predictions divided by total predictions
accuracy = hits/tf.reduce_sum(mask)
# Loss definition (masking to disregard padding)
loss = tf.nn.softmax_cross_entropy_with_logits_v2(labels=Y_flat, logits=Z)
loss = tf.reduce_sum(loss*mask)/tf.reduce_sum(mask)
optimizer = tf.train.AdamOptimizer(learning_rate)
train = optimizer.minimize(loss)
# --------------------------------
### Training
print('\n\n --- TRAINING --- \n')
session = tf.Session()
session.run(tf.global_variables_initializer())
batches_number = np.shape(data_X)[0]
losses = np.zeros((n_epochs, batches_number))
for e in range(1, n_epochs + 1):
cs = session.run(init_state, {X_int: data_X[0], Y_int: data_Y[0]}) # initial state
for b in range(batches_number):
c_input = data_X[b]
c_target = data_Y[b]
ls = list([np.shape(c_input)[1]] * np.shape(c_input)[0])
feed = {X_int: data_X[b],
Y_int: data_Y[b],
lengths: ls,
current_state.c: cs.c,
current_state.h: cs.h}
l, _, cs = session.run([loss, train, final_state], feed)
print(f'Epoch {e}, Batch {b}. \t Loss: {l}')
losses[e-1][b] = l # saving losses
# --------------------------------
### Loss plot
import matplotlib
import matplotlib.pyplot as plt
print('losses shape', np.shape(losses))
colors = ['#616BB0', '#74C49D', '#FFFF00', '#B02956', '#B3BAFF']
# Total loss
ys = losses.reshape(-1)
xs = np.arange(len(ys))
plt.plot(xs, ys, '-', c=colors[0], label='training loss over all epochs')
plt.legend()
plt.show()
# By epochs
for e in range(len(losses)):
ys_e = losses[e]
xs_e = np.arange(len(ys_e))
plt.plot(xs_e, ys_e, '-', c=colors[0], label=f'training loss (epoch {e+1})')
plt.legend()
plt.show()
# By epochs all together
for e in range(len(losses)):
ys_e = losses[e]
xs_e = np.arange(len(ys_e))
plt.plot(xs_e, ys_e, '-', c=colors[e], label=f'training loss (epoch {e+1})')
plt.legend()
plt.show()
# --------------------------------
### Model saving
saver = tf.train.Saver()
saver.save(session, 'models/Activity7Model_1.ckpt')
# --------------------------------
### Text generation
import random
import itertools
for n in range(20):
ri = random.randrange(sum(char_counts.values()))
starting_char = next(itertools.islice(char_counts.elements(), ri, None))
gen_input = [text_to_num(starting_char)] # starting character
gen_lengths = [1] # generation is done character by character
cs = session.run(init_state, {X_int: gen_input}) # initial state
gen_text = [gen_input[0][0]] # store the generated text
for i in range(255):
cs, gen_input = session.run([final_state, pred], {X_int: gen_input, lengths: gen_lengths, current_state: cs})
gen_text.append(gen_input[0][0])
print(f'\n\n------- EXAMPLE {n+1} -------\n')
print(num_to_text(gen_text))
# --------------------------------
### Restore from model
# https://stackoverflow.com/questions/33759623/tensorflow-how-to-save-restore-a-model
# https://stackoverflow.com/questions/40442098/saving-and-restoring-a-trained-lstm-in-tensor-flow
saver = tf.train.Saver()
with tf.Session() as sess:
saver.restore(sess, 'models/Activity7Model_1.ckpt')
print('Model restored.')
ri = random.randrange(sum(char_counts.values()))
starting_char = next(itertools.islice(char_counts.elements(), ri, None))
gen_input = [text_to_num(starting_char)] # starting character
gen_lengths = [1] # generation is done character by character
cs = session.run(init_state, {X_int: gen_input}) # initial state
gen_text = [gen_input[0][0]] # store the generated text
for i in range(255):
cs, gen_input = session.run([final_state, pred], {X_int: gen_input, lengths: gen_lengths, current_state: cs})
gen_text.append(gen_input[0][0])
print(f'\n\n------- EXAMPLE -------\n')
print(num_to_text(gen_text))
# --------------------------------