# coding:utf-8
'''
Student: Danny Hou
Date: 2017-06-14
Content:简单的神经网络练习
'''
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
# def add layer, it would be very convenient that you build a CNN when adding a layer
def add_layer(inputs,in_size,out_size,activation_function=None):
Weights = tf.Variable(tf.random_normal([in_size,out_size])) # 初始化weight,为什么不用0?据说是效果不好。
biases = tf.Variable(tf.zeros(shape=[1,out_size])+0.1) # 初始化又不为0,据说是推荐这个值!!
Wx_plus_b = tf.matmul(inputs,Weights)+biases
if activation_function is None:
outputs = Wx_plus_b
else:
outputs=activation_function(Wx_plus_b)
return outputs
# 开始捏造数据
X = np.linspace(-1,1,3000,dtype=np.float32)[:, np.newaxis] # newaxis 就是把横变成竖的吧,(3000,1)
noise = np.random.normal(0,0.05,X.shape).astype(np.float32) # 加点酌料,搞乱原本的数据,更接近现实
y = np.square(X) - 0.5 + noise # y已经被搞乱了 :-P
# 可以看下数据的情况
fig = plt.figure()
ax = fig.add_subplot(1,1,1) # 一行,一列,第一个
ax.scatter(X,y)
plt.ion() # 用于连续显示
plt.show()
# 定义占位符,我感觉是传入参数的定义,个人理解 :-P
xs = tf.placeholder(tf.float32,[None,1]) # None 代表N个,N到底是多少个?
ys = tf.placeholder(tf.float32,[None,1])
# 神经网络有多层,大致可以分为:输入层、隐藏层和输出层。
# 这里定义隐藏层,xs为接收到的输入矩阵,1就是只有一个神经元(一个特征?),10代表输出给隐藏层有10个神经元,激活函数用relu。
# xs为输入层
l1 = add_layer(xs,1,10,activation_function=tf.nn.relu)
# l2 = add_layer(l1,10,15,activation_function=tf.nn.relu)
# l3 = add_layer(l2,15,10,activation_function=tf.nn.relu)
# 定义输出层,
prediction = add_layer(l1, 10, 1, activation_function=None)
# 定义loss/cost function,由于这里的问题是回归问题,所以一般都会用MSE(应该没有写错吧)
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction),reduction_indices=[1]))
# 定义训练步骤
train_step = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(loss=loss)
# 所有变量和步骤都定义好了,开始初始化
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
# 定制训练次数并为模型传入对应的数据
for i in range(1000):
sess.run(train_step,feed_dict={xs:X,ys:y})
if i % 50 ==0:
print(sess.run(loss,feed_dict={xs:X,ys:y}))
try:
ax.lines.remove(lines[0])
except Exception:
pass
prediction_value = sess.run(prediction,feed_dict={xs:X})
lines = ax.plot(X,prediction_value,'r-',lw=5)
plt.pause(0.5)
loss
0.188886
0.0142108
0.0086079
0.00696789
0.00589828
0.00524938
0.00492084
0.00475221
0.00464395
0.00456124
0.00449256
0.00442923
0.0043689
0.00431515
0.00426623
0.00422012
0.00417505
0.00413331
0.00409412
0.00405706
拟合过程: