如何使用python在Tensorboard上显示训练和预测值

时间:2018-09-06 08:47:12

标签: python python-3.x tensorflow tensorboard

我尝试过这样的事情:

with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer())

    merged = tf.summary.merge_all()
    writer = tf.summary.FileWriter('logs', sess.graph)
    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 

        sess.run(training_op, feed_dict={X: x_batch, y: y_batch}) 

        if iteration % int(1*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}) 
            mse_test = loss.eval(feed_dict={X: x_test, y: y_test})
            y_train_pred,summary1,outimage = sess.run([outputs,merged,out_img_sum], feed_dict={X: x_train,y:y_train})
            y_valid_pred,summary2 = sess.run([outputs,merged], feed_dict={X: x_valid,y:y_valid})
            y_test_pred,summary3 = sess.run([outputs,merged], feed_dict={X: x_test,y:y_test})
            writer.add_summary(summary1, iteration*batch_size/train_set_size)

我愿意在张量板上显示y_trainy_train_pred的值。我该怎么做?这些就像数组一样,我没有办法在Tensorboard上显示这些值的比较。请帮助我。

1 个答案:

答案 0 :(得分:1)

更新

是的,您可以沿x轴绘图。您在张量板上获得错误图像的原因是因为int(iteration*float(batch_size)/train_set_size)始终返回相同的值(根据您的要求为0.0001804630682330861)。我在下面根据您的情况创建了类似的代码(因为我没有您的数据)。而且效果很好。

import tensorflow as tf
import numpy as np

summary_writer = tf.summary.FileWriter('/tmp/test')

for iteration in range(5):
    y_train_preds = np.random.rand(10)
    summary = tf.Summary()
    for idx, value in enumerate(y_train_preds):
        summary.value.add(tag='y_train', simple_value=value)
        summary_writer.add_summary(summary, iteration*len(y_train_preds)+idx)

summary_writer.close()

张量板上的输出

enter image description here

唯一需要注意的一点是,确保add_summary()中的全局步长每次都应增加。

也许您可以尝试以下我已经更新了您的代码,供您尝试

with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer())

    merged = tf.summary.merge_all()
    writer = tf.summary.FileWriter('logs', sess.graph)
    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 

        sess.run(training_op, feed_dict={X: x_batch, y: y_batch}) 

        if iteration % int(1*train_set_size/batch_size) == 0:
            summary = tf.Summary()
            mse_train = loss.eval(feed_dict={X: x_train, y: y_train}) 
            mse_valid = loss.eval(feed_dict={X: x_valid, y: y_valid}) 
            mse_test = loss.eval(feed_dict={X: x_test, y: y_test})
            y_train_pred,summary1,outimage = sess.run([outputs,merged,out_img_sum], feed_dict={X: x_train,y:y_train})
            y_valid_pred,summary2 = sess.run([outputs,merged], feed_dict={X: x_valid,y:y_valid})
            y_test_pred,summary3 = sess.run([outputs,merged], feed_dict={X: x_test,y:y_test})
            for value in y_train:
                summary.value.add(tag='y_train', simple_value=value)
            for idx, value in enumerate(y_train_pred):
                summary.value.add(tag='y_train_pred', simple_value=value)
                writer.add_summary(summary, iteration*len(y_train_pred)+idx)
            writer.add_summary(summary1, int(iteration*float(batch_size)/train_set_size))

参考帖子:tensorboard with numpy array