B站“深度学习框架Tensorflow学习与应用”,P8 3-1
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
x_data = np.linspace(-0.5,0.5,200)[:,np.newaxis] #x_data.shape=(200,1)
noise = np.random.normal(loc=0,scale=0.02,size=x_data.shape)
y_data = np.square(x_data) + noise
x = tf.placeholder(tf.float32, [None, 1])
y = tf.placeholder(tf.float32, [None, 1])
# 定义神经网络中间层
weight_l1 = tf.Variable(tf.random.normal([1, 10]))
bias_l1 = tf.Variable(tf.zeros([1, 10]))
input_l1 = tf.matmul(x, weight_l1) + bias_l1
output_li = tf.nn.tanh(input_l1)
# 定义神经网络输出层
weight_l2 = tf.Variable(tf.random_normal([10, 1])) # l1输出为10列,l2输出为1
bias_l2 = tf.Variable(tf.zeros([1, 1]))
input_l2 = tf.matmul(output_li, weight_l2) + bias_l2
output_l2 = tf.nn.tanh(input_l2)
# define loss function
loss = tf.reduce_mean(tf.square(y-output_l2))
# define optimizer with Gradient Descent
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for step in range(1, 2001):
sess.run(train_step, feed_dict={x:x_data,y:y_data})
y_hat = sess.run(output_l2, feed_dict={x:x_data})
plt.figure()
plt.scatter(x_data, y_data, c='b')
plt.plot(x_data, y_hat, 'r-')
plt.show()
结果: