This repository has been archived by the owner on Feb 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrain.py
executable file
·188 lines (163 loc) · 9.43 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
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
#!/usr/bin/python
import traceback
import matplotlib
matplotlib.use('Agg')
import pylab
import tensorflow as tf
from tensorflow.models.rnn import rnn, rnn_cell
import numpy as np
import random
import json
import itertools
import math
import manage_data
import export_to_octave
import time
def train(parameters, model, trainData, testingData, start, minutes):
print('Launching training.')
# accuracy_summary = tf.scalar_summary("cost", model["cost"])
# merged = tf.merge_all_summaries()
init = tf.initialize_all_variables()
saver = tf.train.Saver(tf.all_variables())
# Launch the graph
# config=tf.ConfigProto(log_device_placement=True)
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.80, allocator_type = 'BFC')
config = tf.ConfigProto(gpu_options=gpu_options)
start_time = time.time()
with tf.Session(config=config) as sess:
if start:
saver.restore(sess, start)
else:
sess.run(init)
# writer = tf.train.SummaryWriter("logs", sess.graph)
iter = 1
step = 1
trainErrorTrend = []
testErrorTrend = []
now = time.time()
# Training for a specific number of minutes
last_losses = []
last_loss = None
while now - start_time < 60 * minutes:
targetInd = random.randint(0, 21)
print('Creating input data for the target: ' + str(targetInd))
if last_loss:
print "Time elapsed: ", now - start_time, ", last_loss: ", last_loss / parameters['batch_size']
# Choosing the target to track
trainingData = manage_data.makeInputForTargetInd(trainData, targetInd)
#export_to_octave.save('training_data_d.mat', 'trainingData', trainingData)
# parameters['learning_rate'] = parameters['learning_rate'] * parameters['decay']
(batch_xsp, batch_ysp) = manage_data.getNextTrainingBatchSequences(trainingData, step - 1,
parameters['batch_size'], parameters['n_steps'], parameters['n_peers'])
#export_to_octave.save('batch_xs_before_reshape.mat', 'batch_xs_before_reshape', batch_xsp)
# Reshape data to get batch_size sequences of n_steps elements with n_input values
batch_xs = batch_xsp.reshape((parameters['batch_size'], parameters['n_steps'], parameters['n_input']))
batch_ys = batch_ysp.reshape((parameters['batch_size'], parameters['n_output']))
# Saving the previous good model.
if step % parameters['display_step'] == 0:
saver.save(sess, 'soccer-model-prev')
# Fit training using batch data
## 'optimizer'
sess.run([model['optimizer']], feed_dict = {
model['x']: batch_xs,
model['y']: batch_ys,
model['istate']: np.asarray(model['rnn_cell'].zero_state(parameters['batch_size'],
tf.float32).eval()),
model['lr']: parameters['learning_rate'],
model['keep_prob']: parameters['keep_prob']
})
if step % parameters['display_step'] == 0:
saver.save(sess, 'soccer-model')
testData = manage_data.makeInputForTargetInd(testingData, np.random.randint(0, 22))
test_len = parameters['batch_size']
testTarget = random.randint(0, 21)
predictedBatch = random.randint(0, test_len-1)
# For debugging, exporting a couple of arrays to Octave.
#export_to_octave.save('batch_xs.mat', 'batch_xs', batch_xs)
#export_to_octave.save('batch_ys.mat', 'batch_ys', batch_ys)
# Calculate batch error as mean distance
[error, prediction] = sess.run([tf.stop_gradient(model['cost']), tf.stop_gradient(model['pred'])], feed_dict = {
model['x']: batch_xs,
model['y']: batch_ys,
model['istate']: np.asarray(model['rnn_cell'].zero_state(parameters['batch_size'],
tf.float32).eval()),
model['keep_prob']: parameters['keep_prob']
})
trainErrorTrend.append(error)
print "Test target: " + str(testTarget)
print "Batch: " + str(predictedBatch)
if (False):
pylab.clf()
pylab.plot(batch_xsp[predictedBatch,:,0,0], batch_xsp[predictedBatch,:,0,1],
[batch_xsp[predictedBatch,parameters['n_steps']-1,0,0],
batch_xsp[predictedBatch,parameters['n_steps']-1,0,0] +
prediction[predictedBatch,0]],
[batch_xsp[predictedBatch,parameters['n_steps']-1,0,1],
batch_xsp[predictedBatch,parameters['n_steps']-1,0,1] +
prediction[predictedBatch,1]],
[batch_xsp[predictedBatch,parameters['n_steps']-1,0,0],
batch_xsp[predictedBatch,parameters['n_steps']-1,0,0] +
batch_ysp[predictedBatch,0]],
[batch_xsp[predictedBatch,parameters['n_steps']-1,0,1],
batch_xsp[predictedBatch,parameters['n_steps']-1,0,1] +
batch_ysp[predictedBatch,1]]);
pylab.savefig('prediction_train_' + str(iter) + '.png')
# Calculate batch loss
n_mixtures = parameters['n_mixtures']
#export_to_octave.save('prediction.mat', 'prediction', prediction)
# Printing out the weights and the predictions.
print "Weights: " + str(prediction[:, 0:n_mixtures])
print "Prediction sigmas: " + str(prediction[:, n_mixtures : n_mixtures * 3])
print "Prediction mus: " + str(prediction[:, n_mixtures * 3 : n_mixtures * 5])
print "Prediction rhos: " + str(prediction[:, n_mixtures * 5 : n_mixtures * 6])
print "Reality: " + str(batch_ys)
#"{:.6f}".format(error) + \
print "Iter " + str(iter * parameters['batch_size']) + ", Minibatch Loss= " + \
"{:.6f}".format(error) + \
", Learning rate= " + \
"{:.5f}".format(parameters['learning_rate'])
test_xp, test_yp = manage_data.getNextTrainingBatchSequences(testData, testTarget, test_len,
parameters['n_steps'],
parameters['n_peers'])
test_x = test_xp.reshape((test_len, parameters['n_steps'], parameters['n_input']))
test_y = test_yp.reshape((test_len, parameters['n_output']))
#export_to_octave.save('test_xp.mat', 'test_xp', test_x)
#export_to_octave.save('test_yp.mat', 'test_yp', test_y)
[testError, prediction] = sess.run([tf.stop_gradient(model['cost']), tf.stop_gradient(model['pred'])],
feed_dict={model['x']: test_x,
model['y']: test_y,
model['istate']: np.asarray(model['rnn_cell'].zero_state(parameters['batch_size'],
tf.float32).eval()),
model['keep_prob']: parameters['keep_prob']})
##writer.add_summary(summary_str, iter)
testErrorTrend.append(testError)
last_losses.append(testError)
# Taking the minimum of the 10 last testing losses.
if (len(last_losses) > 10):
last_losses.pop(0)
last_loss = min(last_losses)
print "Testing Error:", testError
print "Testing Error Normalized:", testError / parameters['batch_size']
print "Last loss:", last_loss / parameters['batch_size']
export_to_octave.save('train_error.mat', 'train_error', trainErrorTrend)
export_to_octave.save('test_error.mat', 'test_error', testErrorTrend)
#export_to_octave.save('test_prediction.mat', 'test_prediction', prediction)
if (False):
pylab.clf()
pylab.plot(test_xp[predictedBatch,:,0,0], test_xp[predictedBatch,:,0,1],
[test_xp[predictedBatch,parameters['n_steps']-1,0,0],
prediction[predictedBatch,0]],
[test_xp[predictedBatch,parameters['n_steps']-1,0,1],
prediction[predictedBatch,1]],
[test_xp[predictedBatch,parameters['n_steps']-1,0,0],
test_yp[predictedBatch,0]],
[test_xp[predictedBatch,parameters['n_steps']-1,0,1],
test_yp[predictedBatch,1]]);
pylab.savefig('prediction' + str(iter) + '.png')
iter += 1
step += 1
now = time.time()
saver.save(sess, 'soccer-model', global_step=iter)
print "Optimization Finished!"
# Returning the last loss value for hyper parameter search
return last_loss