用Tensorflow框架搭建神经网络
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
def addlayer(inputs, insize, outsize, activation_function=None):
Weights = tf.Variable(tf.random_normal([insize, outsize])) # 定义权值矩阵,一般初始值为随机数比赋全0要好
biases = tf.Variable(tf.zeros(outsize) + 0.1) # 在机器学习中,biases的推荐值不为0,所以我们这里是在0向量的基础上又加了0.1
Wx_plus_b = tf.matmul(inputs, Weights) + biases # 此处矩阵乘的顺序一定要注意!!!
if activation_function is None: # 注意此处使用is
output = Wx_plus_b
else:
output = activation_function(Wx_plus_b)
return output
'''构建数据'''
x_data = np.linspace(-1, 1, 300, dtype=np.float32)[:, np.newaxis]
noise = np.random.normal(0, 0.05, x_data.shape).astype(np.float32)
y_data = np.square(x_data) - 0.5 + noise
'''定义一个输入层(1个神经元),隐藏层(10个神经元),输出层(1个神经元)的神经网络'''
xs = tf.placeholder(tf.float32, [None, 1]) # 输入只有一个特征
ys = tf.placeholder(tf.float32, [None, 1])
l1 = addlayer(xs, 1, 10, activation_function=tf.nn.relu) # 隐层的输入为1(特征数),输出定义为10个神经元
prediction = addlayer(l1, 10, 1, None) # 输出层为1,不需要激活函数
loss = tf.reduce_mean(tf.reduce_sum(tf.square(prediction-ys), reduction_indices=[1]))
# tf.reduce_sum()这一步相当于降维了(原来的二维数组变为了一维数组),最后可以求一维数组的平均值
# reduction_indices=[1]表示结果压缩的方向,1表示列方向压缩
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
'''初始化'''
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
fig = plt.figure() #生成一个图片框
ax = fig.add_subplot(1,1,1) #做连续性的画图
ax.scatter(x_data, y_data)
plt.ion()
plt.show() #只能show一次
for i in range(1000):
sess.run(train_step, feed_dict={xs: x_data, ys: y_data})
if i % 100 == 0:
# print("loss=",sess.run(loss, feed_dict={xs: x_data, ys: y_data}))
try:
ax.lines.remove(lines[0])
except Exception:
pass
prediction_value = sess.run(prediction, feed_dict={xs: x_data})
lines = ax.plot(x_data, prediction_value, 'r-', lw=5)
plt.pause(0.1)
- 加速神经网络——优化器
W += -Learning rate * dx
Momentum:
m = b1*m - Learning rate dx
W+= m
AdaGrad
v += dx^2
W+ = -Learning rate * dx / √v
RMSProp
v = b1v +(1-b1)dx^2
W+ = -Learning rate * dx / √v
Adam
m = b1m -(1-b1) dx
v = b2v +(1-b2)*dx^2
W+ = -Learning rate * m / √v
- GredientDescentOptimizer