如何使用tensorboard - tensorflow制作散点图

时间:2016-12-28 05:49:35

标签: graph tensorflow scatter-plot tensorboard

现在,我正在学习张量流。 但是,我无法使用张量板绘制点图。

如果我有训练的样本数据,就像那样

train_X = numpy.asarray([3.3, 4.4, 5.5, 6.71, 6.93, 4.168, 9.779])
train_Y = numpy.asarray([1.7, 2.76, 2.09, 3.19, 1.694, 1.573, 3.366])

我想用张量板显示散点图。 我知道"将matplotlib.pyplot导入为plt"可以做到这一点。 但我可以使用控制台(putty)。所以不能使用这种方法。

我可以看到点图,就像使用张量板的散点图一样。

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

不是一个完整的答案,但我所做的是导入matplotlib,不用于显示器:

import matplotlib as mpl
mpl.use('Agg')  # No display
import matplotlib.pyplot as plt

然后将我的绘图绘制到缓冲区并将其保存为PNG:

# setting up the necessary tensors:
plot_buf_ph = tf.placeholder(tf.string)
image = tf.image.decode_png(plot_buf_ph, channels=4)
image = tf.expand_dims(image, 0)  # make it batched
plot_image_summary = tf.summary.image('some_name', image, max_outputs=1)

# later, to make the plot:
plot_buf = get_plot_buf()
plot_image_summary_ = session.run(
        plot_image_summary,
        feed_dict={plot_buf_ph: plot_buf.getvalue()})
summary_writer.add_summary(plot_image_summary_, global_step=iteration)

其中get_plot_buf是:

def get_plot_buf(self):
    plt.figure()

    # ... draw plot here ...

    buf = io.BytesIO()
    plt.savefig(buf, format='png')
    plt.close()

    buf.seek(0)
    return buf