【发布时间】:2019-01-15 13:01:43
【问题描述】:
这是我为 tensorflow 训练、验证和测试初始化的变量。
index_in_epoch = 0;
perm_array = np.arange(x_train.shape[0])
np.random.shuffle(perm_array)
# function to get the next batch
def get_next_batch(batch_size):
global index_in_epoch, x_train, perm_array
start = index_in_epoch
index_in_epoch += batch_size
if index_in_epoch > x_train.shape[0]:
np.random.shuffle(perm_array) # shuffle permutation array
start = 0 # start next epoch
index_in_epoch = batch_size
end = index_in_epoch
return x_train[perm_array[start:end]], y_train[perm_array[start:end]]
# parameters
n_steps = seq_len-1
n_inputs = x_train.shape[2]#4
n_neurons = 200
n_outputs = y_train.shape[1]#4
n_layers = 2
learning_rate = 0.001
batch_size = 50
n_epochs = 100#200
train_set_size = x_train.shape[0]
test_set_size = x_test.shape[0]
tf.reset_default_graph()
X = tf.placeholder(tf.float32, [None, n_steps, n_inputs])
y = tf.placeholder(tf.float32, [None, n_outputs])
# use LSTM Cell with peephole connections
layers = [tf.contrib.rnn.LSTMCell(num_units=n_neurons,
activation=tf.nn.leaky_relu, use_peepholes = True)
for layer in range(n_layers)]
multi_layer_cell = tf.contrib.rnn.MultiRNNCell(layers)
rnn_outputs, states = tf.nn.dynamic_rnn(multi_layer_cell, X, dtype=tf.float32)
stacked_rnn_outputs = tf.reshape(rnn_outputs, [-1, n_neurons])
stacked_outputs = tf.layers.dense(stacked_rnn_outputs, n_outputs)
outputs = tf.reshape(stacked_outputs, [-1, n_steps, n_outputs])
outputs = outputs[:,n_steps-1,:] # keep only last output of sequence
loss = tf.reduce_mean(tf.square(outputs - y)) # loss function = mean squared error
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
training_op = optimizer.minimize(loss)
这是我训练和验证模型并收集值以通过张量板显示的方式:
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for iteration in range(int(n_epochs*train_set_size/batch_size)):
x_batch, y_batch = get_next_batch(batch_size) # fetch the next training batch
writer = tf.summary.FileWriter("outputLogs", sess.graph)
sess.run(training_op, feed_dict={X: x_batch, y: y_batch})
writer.close()
if iteration % int(5*train_set_size/batch_size) == 0:
mse_train = loss.eval(feed_dict={X: x_train, y: y_train})
mse_valid = loss.eval(feed_dict={X: x_valid, y: y_valid})
print('%.2f epochs: MSE train/valid = %.10f/%.10f'%(
iteration*batch_size/train_set_size, mse_train, mse_valid))
save_path = saver.save(sess, "models\\model"+str(iteration)+".ckpt")
但是在运行命令之后:tensorboard --logdir outputLogs 我只得到了图表,而不是所有其他值图表,比如我在训练时可以显示的损失、错误或其他变量。见下图:
请帮我可视化所有可变参数或输入,以便我可以看到张量板上的内容,并使训练对我来说可行。
【问题讨论】:
标签: python python-3.x tensorflow visualization tensorboard