模拟非线性回归,给定一些二维点,y = x^2 + noise,用梯度下降进行训练,实线前向传播神经网络。
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
#生成随机变量
x_data = np.linspace(-1, 1, 200)#生成200个随机点,范围为-1 --> 1
x_data = x_data.reshape((200, 1))#维度设置为(200, 1)
noise = np.random.normal(0, 0.02, x_data.shape)#生成干扰项
y_data = np.square(x_data) + noise
x = tf.placeholder(tf.float32, [200, 1])
y = tf.placeholder(tf.float32, [200, 1])
#定义神经网络中间层权值
weights_l1 = tf.Variable(tf.random_normal([1, 10]))#10个神经元
biases_l1 = tf.Variable(tf.zeros([1, 10]))
wx_plust_b_l1 = tf.matmul(x, weights_l1) + biases_l1
l1 = tf.nn.tanh(wx_plust_b_l1)#双曲正切函数作为激活函数
#定义输出层
weights_l2 = tf.Variable(tf.random_normal([10, 1]))#输出层1个神经元
biases_l2 = tf.Variable(t