-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrain.py
More file actions
147 lines (104 loc) · 4.61 KB
/
Copy pathTrain.py
File metadata and controls
147 lines (104 loc) · 4.61 KB
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
import tensorflow as tf
tf.enable_eager_execution()
import numpy as np
import os
import time
import pickle
#path_to_file = tf.keras.utils.get_file('shakespeare.txt', 'https://storage.googleapis.com/download.tensorflow.org/data/shakespeare.txt')
dialogues = []
with open('all_d.list', 'rb') as d_file:
dialogues = pickle.load(d_file)
text = ''.join(dialogues)
vocab = sorted(set(text))
print('Length of text: {} characters'.format(len(text)))
# Creating a mapping from unique characters to indices
char2idx = {u:i for i, u in enumerate(vocab)}
idx2char = np.array(vocab)
text_as_int = np.array([char2idx[c] for c in text])
# The maximum length sentence we want for a single input in characters
seq_length = 100
# Create training examples / targets
chunks = tf.data.Dataset.from_tensor_slices(text_as_int).batch(seq_length+1, drop_remainder=True)
def split_input_target(chunk):
input_text = chunk[:-1]
target_text = chunk[1:]
return input_text, target_text
dataset = chunks.map(split_input_target)
# Batch size
BATCH_SIZE = 64
# Buffer size to shuffle the dataset
# (TF data is designed to work with possibly infinite sequences,
# so it doesn't attempt to shuffle the entire sequence in memory. Instead,
# it maintains a buffer in which it shuffles elements).
BUFFER_SIZE = 10000
dataset = dataset.shuffle(BUFFER_SIZE).batch(BATCH_SIZE, drop_remainder=True)
class Model(tf.keras.Model):
def __init__(self, vocab_size, embedding_dim, units):
super(Model, self).__init__()
self.units = units
self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim)
if tf.test.is_gpu_available():
self.gru = tf.keras.layers.CuDNNGRU(self.units,
return_sequences=True,
recurrent_initializer='glorot_uniform',
stateful=True)
else:
self.gru = tf.keras.layers.GRU(self.units,
return_sequences=True,
recurrent_activation='sigmoid',
recurrent_initializer='glorot_uniform',
stateful=True)
self.fc = tf.keras.layers.Dense(vocab_size)
def call(self, x):
embedding = self.embedding(x)
# output at every time step
# output shape == (batch_size, seq_length, hidden_size)
output = self.gru(embedding)
# The dense layer will output predictions for every time_steps(seq_length)
# output shape after the dense layer == (seq_length * batch_size, vocab_size)
prediction = self.fc(output)
# states will be used to pass at every step to the model while training
return prediction
# Length of the vocabulary in chars
vocab_size = len(vocab)
# The embedding dimension
embedding_dim = 256
# Number of RNN units
units = 1024
model = Model(vocab_size, embedding_dim, units)
# Using adam optimizer with default arguments
optimizer = tf.train.AdamOptimizer()
# Using sparse_softmax_cross_entropy so that we don't have to create one-hot vectors
def loss_function(real, preds):
return tf.losses.sparse_softmax_cross_entropy(labels=real, logits=preds)
model.build(tf.TensorShape([BATCH_SIZE, seq_length]))
#model.summary()
# Directory where the checkpoints will be saved
checkpoint_dir = './training_checkpoints'
# Name of the checkpoint files
checkpoint_prefix = os.path.join(checkpoint_dir, "ckpt")
EPOCHS = 5
# Training loop
for epoch in range(EPOCHS):
start = time.time()
# initializing the hidden state at the start of every epoch
# initally hidden is None
hidden = model.reset_states()
for (batch, (inp, target)) in enumerate(dataset):
with tf.GradientTape() as tape:
# feeding the hidden state back into the model
# This is the interesting step
predictions = model(inp)
loss = loss_function(target, predictions)
grads = tape.gradient(loss, model.variables)
optimizer.apply_gradients(zip(grads, model.variables))
if batch % 100 == 0:
print('Epoch {} Batch {} Loss {:.4f}'.format(epoch + 1,
batch,
loss))
# saving (checkpoint) the model every 5 epochs
if (epoch + 1) % 5 == 0:
model.save_weights(checkpoint_prefix)
print('Epoch {} Loss {:.4f}'.format(epoch + 1, loss))
print('Time taken for 1 epoch {} sec\n'.format(time.time() - start))
model.save_weights(checkpoint_prefix)