tensorflow 非线性回归

#encoding:utf-8
#encoding:utf-8
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt


# 神经层函数参数, 输入值, 输入的大小, 输出的大小,激励函数(默认为空)
def add_layer(inputs, in_size, out_size, activation_function=None):
    # 生成初始参数时,随机变量回比全部为0要好很多,所以weights为一个in_size
    # out_size列的随机变量矩阵

    Weights = tf.Variable(tf.random_normal([in_size, out_size]))
    # 在机器学习中biases的推荐值不为0,所以在0的基础上加了0.1
    biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
    # Wx_plus_b,即神经网络未激活的值。其中tf.matmul()是矩阵的乘法
    Wx_plus_b = tf.matmul(inputs, Weights) + biases
    # activation_function 激励函数为None,输出就是当前的预测值Wx_plus_b
    # 不为空时就把Wx_plus_b传到activation_function()函数中得到
    if activation_function == None:
        outputs = Wx_plus_b
    else:
        outputs = activation_function(Wx_plus_b)

    return outputs


# 导入数据

x_data = np.linspace(-1, 1, 300, dtype=np.float32)[:, np.newaxis]

noise = np.random.normal(0, 0.05, x_data.shape).astype(np.float32)
y_data = np.square(x_data) - 0.5 + noise

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

# matplotlib 可视化
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.scatter(x_data, y_data)
plt.ion()#plt.ion()用于连续显示
plt.show()
# 搭建网络
# 隐藏层 10个神经元
l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)  # tf自带激励函数tf.nn.relu
# 输出层
prediction = add_layer(l1, 10, 1, activation_function=None)

# 误差 二者差的平方和再取平均
loss = tf.reduce_mean(tf.square(ys - prediction))
# loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction), reduction_indices=[1]))

# 梯度下降优化器  学习率为0.1 最小话损失函数
train_op = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    #     print sess.run(loss1, feed_dict= {xs:x_data, ys:y_data})
    #     print sess.run(loss, feed_dict= {xs:x_data, ys:y_data})
    for i in range(1000):
        # 训练模型
        sess.run(train_op, feed_dict={xs: x_data, ys: y_data})
#        每隔50次训练刷新一次图形,用红色、宽度为5的线来显示我们的预测数据和输入之间的关系,
#并暂停0.1s。
        if i % 50 == 0:
            try:
                ax.lines.remove(lines[0])
            except Exception:
                pass
            prediction_value = sess.run(prediction, feed_dict={xs: x_data, ys: y_data})
            lines = ax.plot(x_data, prediction_value, 'r-', lw=5)
            plt.pause(0.1)
            # 每隔50输出误差
#             print sess.run(loss, feed_dict= { xs :x_data, ys : y_data})






画出的图
tensorflow 非线性回归_第1张图片
每训练50次输出的误差

0.685976
0.0200331
0.00953159
0.00765344
0.00719356
0.00694252
0.00672491
0.00648086
0.0062244
0.00598285
0.00572546
0.0054809
0.00524732
0.0050034
0.00477788
0.00457842
0.00440171
0.00424059
0.00409868
0.00397596

你可能感兴趣的:(tensorflow)