-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathtrain.py
151 lines (107 loc) · 4.66 KB
/
train.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
import warnings
warnings.filterwarnings('ignore')
import tensorflow as tf
import numpy as np
import getopt
import sys
import os
from dnc.dnc import DNC
from feedforward_controller import FeedforwardController
def llprint(message):
sys.stdout.write(message)
sys.stdout.flush()
def generate_data(batch_size, length, size):
input_data = np.zeros((batch_size, 2 * length + 1, size), dtype=np.float32)
target_output = np.zeros((batch_size, 2 * length + 1, size), dtype=np.float32)
sequence = np.random.binomial(1, 0.5, (batch_size, length, size - 1))
input_data[:, :length, :size - 1] = sequence
input_data[:, length, -1] = 1 # the end symbol
target_output[:, length + 1:, :size - 1] = sequence
return input_data, target_output
def binary_cross_entropy(predictions, targets):
return tf.reduce_mean(
-1 * targets * tf.log(predictions) - (1 - targets) * tf.log(1 - predictions)
)
if __name__ == '__main__':
dirname = os.path.dirname(__file__)
ckpts_dir = os.path.join(dirname , 'checkpoints')
tb_logs_dir = os.path.join(dirname, 'logs')
batch_size = 1
input_size = output_size = 6
sequence_max_length = 10
words_count = 15
word_size = 10
read_heads = 1
learning_rate = 1e-4
momentum = 0.9
from_checkpoint = None
iterations = 100000
options,_ = getopt.getopt(sys.argv[1:], '', ['checkpoint=', 'iterations='])
for opt in options:
if opt[0] == '--checkpoint':
from_checkpoint = opt[1]
elif opt[0] == '--iterations':
iterations = int(opt[1])
graph = tf.Graph()
with graph.as_default():
with tf.Session(graph=graph) as session:
llprint("Building Computational Graph ... ")
optimizer = tf.train.RMSPropOptimizer(learning_rate, momentum=momentum)
ncomputer = DNC(
FeedforwardController,
input_size,
output_size,
2 * sequence_max_length + 1,
words_count,
word_size,
read_heads,
batch_size
)
# squash the DNC output between 0 and 1
output, _ = ncomputer.get_outputs()
squashed_output = tf.clip_by_value(tf.sigmoid(output), 1e-6, 1. - 1e-6)
loss = binary_cross_entropy(squashed_output, ncomputer.target_output)
summeries = []
gradients = optimizer.compute_gradients(loss)
for i, (grad, var) in enumerate(gradients):
if grad is not None:
summeries.append(tf.histogram_summary(var.name + '/grad', grad))
gradients[i] = (tf.clip_by_value(grad, -10, 10), var)
apply_gradients = optimizer.apply_gradients(gradients)
summeries.append(tf.scalar_summary("Loss", loss))
summerize_op = tf.merge_summary(summeries)
no_summerize = tf.no_op()
summerizer = tf.train.SummaryWriter(tb_logs_dir, session.graph)
llprint("Done!\n")
llprint("Initializing Variables ... ")
session.run(tf.initialize_all_variables())
llprint("Done!\n")
if from_checkpoint is not None:
llprint("Restoring Checkpoint %s ... " % (from_checkpoint))
ncomputer.restore(session, ckpts_dir, from_checkpoint)
llprint("Done!\n")
last_100_losses = []
for i in xrange(iterations + 1):
llprint("\rIteration %d/%d" % (i, iterations))
random_length = np.random.randint(1, sequence_max_length + 1)
input_data, target_output = generate_data(batch_size, random_length, input_size)
summerize = (i % 100 == 0)
take_checkpoint = (i != 0) and (i % iterations == 0)
loss_value, _, summary = session.run([
loss,
apply_gradients,
summerize_op if summerize else no_summerize
], feed_dict={
ncomputer.input_data: input_data,
ncomputer.target_output: target_output,
ncomputer.sequence_length: 2 * random_length + 1
})
last_100_losses.append(loss_value)
summerizer.add_summary(summary, i)
if summerize:
llprint("\n\tAvg. Logistic Loss: %.4f\n" % (np.mean(last_100_losses)))
last_100_losses = []
if take_checkpoint:
llprint("\nSaving Checkpoint ... "),
ncomputer.save(session, ckpts_dir, 'step-%d' % (i))
llprint("Done!\n")