神经网络预测结果可视化

用tensorflow建造一个简单的神经网络中建立了一个简单的三层(输入层,隐藏层,输出层)的神经网络。这篇文章主要说怎样将我们构建的神经网络训练的过程以及结果可视化。
用tensorflow建造一个简单的神经网络中建立神经网络的代码

import tensorflow as tf
import numpy as np


def add_layer(inputs, in_size, out_size, activation_function=None):
    Weights = tf.Variable(tf.random_uniform([in_size, out_size]))
    biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
    Wx_plus_b = tf.matmul(inputs, Weights) + biases
    if activation_function is None:
        outputs = Wx_plus_b
    else:
        outputs = activation_function(Wx_plus_b)
    return outputs

x_data = np.linspace(-1, 1, 300)[:, np.newaxis]
noise = np.random.normal(0, 0.05, x_data.shape)
y_data = np.square(x_data) - 0.5 + noise

xs = tf.placeholder(tf.float32, [None, 1])
ys = tf.placeholder(tf.float32, [None, 1])

l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)
prediction = add_layer(l1, 10, 1, activation_function=None)
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),
                                    reduction_indices=[1]))

train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

for i in range(1000):
    sess.run(train_step, feed_dict={xs:x_data, ys:y_data})

为了进行可视化首先需要导入一个python的可视化包

import matplotlib.pylab as plt

将x_data, y_data以散点图的形式展示出来:

# 生成图片框
fig = plt.figure()
# 我们想做一个连续性的画图需要用到add_subplot函数
ax = fig.add_subplot(1,1,1)
# plot真实的数据
ax.scatter(x_data, y_data)
plt.show()

运行结果为:
这里写图片描述
这就是我们输入的数据集的点
接下来我们要显示我们的预测数据,先定义预测数据:

 prediction_value = sess.run(prediction, feed_dict={xs:x_data})

把预测的数据用红色的曲线的形式展示出来,假设线的宽度为5

lines = ax.plot(x_data, prediction_value, 'r-', lw = 5)

如果想连续plot出线,就需要把之前的线去除掉,否则太多的线看不清最后的结果。在plot的途中暂停0.1秒再plot另一条线。
所以在这里我们先抹除掉第一条线,忽略第一次的错误,然后生成线后再抹除第二条线。

    if i % 50 == 0:
        # print(sess.run(loss, feed_dict={xs: x_data, ys:y_data}))
        try:
            ax.lines.remove(lines[0])
        except Exception:
            pass
        prediction_value = sess.run(prediction, feed_dict={xs:x_data})
        lines = ax.plot(x_data, prediction_value, 'r-', lw = 5)
        plt.pause(0.1)   

注意如果要连续的输入,show之后不暂停就需要ion函数。
最后的代码为:

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

def add_layer(inputs, in_size, out_size, activation_function=None):
    Weights = tf.Variable(tf.random_normal([in_size, out_size]))
    biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
    Wx_plus_b = tf.matmul(inputs, Weights) + biases
    if activation_function is None:
        outputs = Wx_plus_b
    else:
        outputs = activation_function(Wx_plus_b)
    return outputs

# Make up some real data
x_data = np.linspace(-1, 1, 300)[:, np.newaxis]
noise = np.random.normal(0, 0.05, x_data.shape)
y_data = np.square(x_data) - 0.5 + noise

##plt.scatter(x_data, y_data)
##plt.show()

# define placeholder for inputs to network
xs = tf.placeholder(tf.float32, [None, 1])
ys = tf.placeholder(tf.float32, [None, 1])
# add hidden layer
l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)
# add output layer
prediction = add_layer(l1, 10, 1, activation_function=None)

# the error between prediction and real data
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
# important step
sess = tf.Session()
# tf.initialize_all_variables() no long valid from
# 2017-03-02 if using tensorflow >= 0.12
if int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1:
    init = tf.initialize_all_variables()
else:
    init = tf.global_variables_initializer()
sess.run(init)

# plot the real data
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(x_data, y_data)
plt.ion()
plt.show()


for i in range(1000):
    # training
    sess.run(train_step, feed_dict={xs: x_data, ys: y_data})
    if i % 50 == 0:
        # to visualize the result and improvement
        try:
            ax.lines.remove(lines[0])
        except Exception:
            pass
        prediction_value = sess.run(prediction, feed_dict={xs: x_data})
        # plot the prediction
        lines = ax.plot(x_data, prediction_value, 'r-', lw=5)
        plt.pause(1)

运行代码我们可以看到神经网络的训练过程:
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
我们可以看出神经网络在学习过程中是逐渐靠近正确的数据点的。

猜你喜欢

转载自blog.csdn.net/katherine_hsr/article/details/79242438
今日推荐