-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpar_train.py
307 lines (257 loc) · 8.67 KB
/
par_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
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
from mpi4py import MPI
import tensorflow as tf
import numpy as np
import sys
import resource
import os
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
full_layers = [3]
train_batch = 10
epochs = 30
learning_rate = 0.1
input_shape = [600]
output_size = 2
threads = size
inter_threads = 0
intra_threads = 0
filename = None
valid_pct = 0.1
test_pct = 0.1
epoch = 0
error_batch = False
merge_every = 0
top = 1
if 0 == inter_threads:
inter_threads = threads
if 0 == intra_threads:
intra_threads = threads
# grab data
os.chdir('/Users/michael/Documents/brown/kobe/data')
npzfile = np.load('Flint_2012_e1_PCA.npz')
all_time = npzfile['all_time']
all_velocities = npzfile['all_velocities']
all_neural = npzfile['all_neural']
T = int(all_time) - 30
del all_time
d_neural = 30 * all_neural.shape[0]
d_velocities = all_velocities.shape[0]
def neural(ind):
neur = np.zeros((ind.size, d_neural))
for i0 in range(ind.size):
s_idx = range(ind[i0], ind[i0] + 30)
neur[i0, :] = all_neural[:, s_idx].flatten()
return neur
def velocities(ind):
return all_velocities[:, ind + 29].T
full_dat = neural(np.arange(3000))
full_lab = velocities(np.arange(3000))
valid_dat = neural(np.arange(3000, 4000))
valid_lab = velocities(np.arange(3000, 4000))
test_dat = neural(np.arange(4000, 5000))
test_lab = velocities(np.arange(4000, 5000))
input_size = 1
for i in input_shape:
input_size *= i
# set up network
def weight_variable(shape, saved_state, index):
if saved_state is None:
initial = tf.truncated_normal(shape, stddev=0.1)
else:
initial = saved_state[0][index]
return tf.Variable(initial)
def bias_variable(shape, saved_state, index):
if saved_state is None:
initial = tf.constant(0.1, shape=shape)
else:
initial = saved_state[1][index]
return tf.Variable(initial)
def create_full_layer(in_size, out_size, layer_list, weight_list,
bias_list, saved_state):
if saved_state is None:
weight_list.append(tf.Variable(
tf.random_normal([in_size, out_size], stddev=1.0 / in_size)))
bias_list.append(tf.Variable(
tf.random_normal([out_size], stddev=1.0 / in_size)))
else:
index = len(weight_list)
weight_list.append(tf.Variable(saved_state[0][index]))
bias_list.append(tf.Variable(saved_state[1][index]))
temp_w = len(weight_list)
temp_b = len(bias_list)
temp_l = len(layer_list)
layer_list.append(tf.nn.sigmoid(tf.matmul(layer_list[temp_l - 1], weight_list[temp_w - 1]) + bias_list[temp_b - 1]))
def populate_graph(
full_layers,
learning_rate,
input_shape,
saved_state):
weights = []
biases = []
layers = []
x = tf.placeholder(tf.float32, [None, input_size])
y_ = tf.placeholder(tf.float32, [None, output_size])
layers.append(x)
layers.append(tf.reshape(x, [-1] + input_shape))
full_layers = [input_size] + full_layers
for i in range(len(full_layers) - 1):
create_full_layer(full_layers[i], full_layers[i + 1], layers,
weights, biases, saved_state)
if saved_state is None:
W = tf.Variable(tf.random_normal([full_layers[-1], output_size], stddev=1.0 / full_layers[-1]))
b = tf.Variable(tf.random_normal([output_size], stddev=1.0 / full_layers[-1]))
else:
index = len(weights)
W = tf.Variable(saved_state[0][index])
b = tf.Variable(saved_state[1][index])
weights.append(W)
biases.append(b)
w_holder = [tf.placeholder(tf.float32, w.get_shape()) for w in weights]
b_holder = [tf.placeholder(tf.float32, b.get_shape()) for b in biases]
w_assign = [w.assign(p) for w, p in zip(weights, w_holder)]
b_assign = [b.assign(p) for b, p in zip(biases, b_holder)]
y = tf.matmul(layers[-1], W) + b
mse_loss = tf.reduce_mean(tf.squared_difference(y_, y), name='mse')
train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(mse_loss)
init = tf.initialize_all_variables()
sess = tf.Session(
config=tf.ConfigProto(
inter_op_parallelism_threads=inter_threads,
intra_op_parallelism_threads=intra_threads))
mse_val = tf.reduce_sum(tf.squared_difference(y_, y))
sess.run(init)
ops = {
"sess": sess,
"x": x,
"y_": y_,
"weights": weights,
"biases": biases,
"w_holder": w_holder,
"b_holder": b_holder,
"w_assign": w_assign,
"b_assign": b_assign,
"train_step": train_step,
"mse_loss": mse_loss,
"mse_val": mse_val,
}
return ops
def run_graph(
data,
labels,
train_batch,
ops,
saved_state):
global epoch
sess = ops["sess"]
x = ops["x"]
y_ = ops["y_"]
weights = ops["weights"]
biases = ops["biases"]
w_holder = ops["w_holder"]
b_holder = ops["b_holder"]
w_assign = ops["w_assign"]
b_assign = ops["b_assign"]
train_step = ops["train_step"]
mse_loss = ops["mse_loss"]
mse_val = ops["mse_val"]
# use saved state to assign saved weights and biases
if saved_state is not None:
feed_dict = {}
for d, p in zip(saved_state[0], w_holder):
feed_dict[p] = d
for d, p in zip(saved_state[1], b_holder):
feed_dict[p] = d
sess.run(w_assign + b_assign, feed_dict=feed_dict)
number_of_batches = int(len(data) / train_batch)
min_batches = comm.allreduce(number_of_batches, MPI.MIN)
if number_of_batches == 0:
number_of_batches = 1
for i in range(number_of_batches):
lo = i * train_batch
hi = (i + 1) * train_batch
batch_xs = data[lo:hi]
batch_ys = labels[lo:hi]
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
if (i < min_batches) and (merge_every >= 1) and (i % merge_every == 0):
r_weights = sess.run(weights)
r_biases = sess.run(biases)
for r in r_weights:
comm.Allreduce(MPI.IN_PLACE, r, MPI.SUM)
r /= size
for r in r_biases:
comm.Allreduce(MPI.IN_PLACE, r, MPI.SUM)
r /= size
feed_dict = {}
for d, p in zip(r_weights, w_holder):
feed_dict[p] = d
for d, p in zip(r_biases, b_holder):
feed_dict[p] = d
sess.run(w_assign + b_assign, feed_dict=feed_dict)
# average as soon as we're done with all batches so the error and
# mse_val reflect the current epoch
r_weights = sess.run(weights)
r_biases = sess.run(biases)
for r in r_weights:
comm.Allreduce(MPI.IN_PLACE, r, MPI.SUM)
r /= size
for r in r_biases:
comm.Allreduce(MPI.IN_PLACE, r, MPI.SUM)
r /= size
feed_dict = {}
for d, p in zip(r_weights, w_holder):
feed_dict[p] = d
for d, p in zip(r_biases, b_holder):
feed_dict[p] = d
sess.run(w_assign + b_assign, feed_dict=feed_dict)
sum_error = 0.0
if error_batch:
for i in range(number_of_batches):
lo = i * train_batch
hi = (i + 1) * train_batch
batch_xs = data[lo:hi]
batch_ys = labels[lo:hi]
sum_error += sess.run(mse_loss, feed_dict={x: batch_xs, y_: batch_ys})
else:
sum_error = sess.run(mse_loss, feed_dict={x: data, y_: labels})
sum_error_all = comm.allreduce(sum_error)
batch_mse = 0.0
if error_batch:
test_batch_count = len(test_dat) / train_batch
if test_batch_count == 0:
test_batch_count = 1
for i in range(test_batch_count):
lo = i * train_batch
hi = (i + 1) * train_batch
batch_xs = test_dat[lo:hi]
batch_ys = test_lab[lo:hi]
batch_mse += sess.run(mse_val, feed_dict={x: batch_xs, y_: batch_ys})
else:
batch_mse = sess.run(mse_val, feed_dict={x: test_dat, y_: test_lab})
batch_mse = comm.allreduce(batch_mse, MPI.SUM)
count = comm.allreduce(len(test_dat), MPI.SUM)
batch_mse = float(batch_mse) / count
if 0 == rank:
print(epoch + 1, batch_mse, sum_error_all)
sys.stdout.flush()
return r_weights, r_biases
if 0 == rank:
print("epoch,mse_val,error")
data_threshold = int(len(full_dat) / 2)
active_dat = full_dat
active_lab = full_lab
inactive_dat = np.empty([0] + list(full_dat.shape[1:]), full_dat.dtype)
inactive_lab = np.empty([0] + list(full_lab.shape[1:]), full_lab.dtype)
saved_state = None
ops = populate_graph(
full_layers,
learning_rate,
input_shape,
saved_state)
for epoch in range(epochs):
saved_state = run_graph(
active_dat,
active_lab,
train_batch,
ops,
saved_state)