Tensorfow 之 结果可视化

安装 中输出结果可视化模块 Matplotlib

import matplotlib.pyplot as plt

使用上篇博客的例子,在里面添加了可视化的部分,就可以将这先乏味的数据通通图像更直观的查看了。
首先需要的是构建图形,用散点图描述真实数据之间的关系。
Tensorfow 之 结果可视化_第1张图片

每隔50次训练刷新一次图形,用红色、宽度为5的线来显示我们的预测数据和输入之间的关系,并暂停0.1s

主要代码:

with tf.Session() as sess:
    sess.run(init)
    #先生成图片框
    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)#做连续性的画图
    #plot上真实数据
    ax.scatter(x_data,y_data)
    plt.ion()
    #打印输出
    plt.show()
    for i in range(1000):
        sess.run(train_step, feed_dict={x_input:x_data,y_input:y_data})
        if i% 50:
            try:
                #抹去上面一条线
                ax.lines.remove(lines[0])
            except Exception:
                pass
            #显示出预测数据
            prediction_value = sess.run(layer_output,feed_dict={x_input:x_data})
            #通过曲线形式plot上去 宽度为5的红色的线
            lines = ax.plot(x_data, prediction_value, 'r-', lw=5)
            #暂停0.1秒
            plt.pause(0.1)

            print sess.run(loss,feed_dict={x_input:x_data,y_input:y_data})

最后的效果图

Tensorfow 之 结果可视化_第2张图片
Tensorfow 之 结果可视化_第3张图片
Tensorfow 之 结果可视化_第4张图片
Tensorfow 之 结果可视化_第5张图片
可以看出来,预测曲线越来越区近真实曲线了。

你可能感兴趣的:(tensorflow)