利用 y = x ^2 + noise 生成数据,并利用数据对一个简单的只具有单层隐层(10个神经元)的神经网络进行训练。输入输出个一个神经元。代码如下
import os
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
os.environ["TF_CPP_MIN_LOG_LEVEL"] = '2' # 只显示 warning 和 Error
x_data = np.linspace(0, 1, 200)[:, np.newaxis] # np.newaxis表示在第二维度增加一维
noise = np.random.normal(0, 0.01, x_data.shape) #高斯分布, 0 表示均值, 0.1 表示标准差 最后是shape
y_data = noise - np.square(x_data)
# 构建神经网络
# 1 10 1
# 输入层1个神经元
# 中间隐层10个神经元
# 输出层1个神经元
# 定义两个占位符
x = tf.placeholder(tf.float32, [None, 1], name='x')
y = tf.placeholder(tf.float32, [None, 1], name='y')
#
# 定义权值矩阵, 输入层与隐层之间的权值矩阵
input_2_hidden_w = tf.Variable(tf.truncated_normal([1,10]))
hidden_bias = tf.Variable(tf.zeros([1,10]))
hidden_out = tf.matmul(x,input_2_hidden_w) + hidden_bias
activation_hidden_out = tf.nn.tanh(hidden_out)
# 定义权值矩阵, 隐层与输出层之间的权值矩阵
hidden_2_output_w = tf.Variable(tf.truncated_normal([10,1]))
out_bias = tf.Variable(tf.zeros([1