# -*- coding:utf-8 -*-
__author__ = 'snake'
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
def test01_notliner():
# 使用numpy生成200个随机点
x_data = np.linspace(-0.5, 0.5, 200).reshape((200,1)) # x_data维度设置为(200, 1)
noise = np.random.normal(0, 0.02, x_data.shape) # 生成干扰项
y_data = np.square(x_data) + noise # 实际值
# 定义两个placeholder
x = tf.placeholder(tf.float32, [None, 1])
y = tf.placeholder(tf.float32, [None, 1])
# 定义神经网络中间层
# x * W + b = pre_y
Weights_L1 = tf.Variable(tf.random_normal([1, 10])) # 10个神经元
bases_L1 = tf.Variable(tf.zeros([1, 10]))
Wx_plus_b_L1 = tf.matmul(x, Weights_L1) + bases_L1
L1 = tf.nn.tanh(Wx_plus_b_L1) # 双曲正切函数作为激活函数
# 定义神经网络输出层
# 中间层的输出就是下一层的输入
Weights_L2 = tf.Variable(tf.random_normal([10, 1])) # 输出层1个神经元
bases_L2 = tf.Variable(tf.zeros([1, 1])) # 设置偏置值
Wx_plus_b_L2 = tf.matmul(L1, Weights_L2) + bases_L2
prediction = tf.nn.tanh(Wx_plus_b_L2) # 预测值
# 代价函数
loss = tf.reduce_mean(tf.square(y-prediction))
# 使用梯度下降法最小化loss,学习速率(步长)为0.1
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
with tf.Session() as sess:
# 初始化变量
sess.run(tf.global_variables_initializer()) # 初始化变量,在TensorFlow中用到Varibals就需要初始化
# 循环2000次
for _ in range(20000):
# 运行梯度下降训练,训练数据为x_data, 实际值为y_data:
# 实际上是监督学习:通过事先准备好的值来训练模型
sess.run(train_step, feed_dict={x: x_data, y: y_data})
# 获取预测值并画图
prediction_value = sess.run(prediction, feed_dict={x: x_data, y: y_data})
plt.figure()
plt.scatter(x_data, y_data)
plt.plot(x_data, prediction_value, 'r-', lw=5)
plt.show()
if __name__ == "__main__":
test01_notliner()
网友评论