用Python构建如下一个简单BP神经网络
第一步FP:先前向得到一个总的损失函数;
第二步BP:再反向修正各个w权重值
Python原理代码:
import numpy as np
'''
构建一个隐藏层的BP神经网络
输入层:两个神经元l1、l2
隐藏层:h1/h2/h3
输出层:o1/o2
'''
# 加载数据
def loadDataSet():
w = [0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65] #所有权重
b = [0.35, 0.65]#偏置率
l = [5, 10] #输入特征
return w, b, l
# 激活函数
def sigmoid(z):
return 1.0 / (1 + np.exp(-z))
w, b, l = loadDataSet()
def f1(w, b, l):
'''
:param w:
:param b:
:param l:
:return:
'''
h1 = sigmoid(w[0] * l[0] + w[1] * l[1] + b[0])
h2 = sigmoid(w[2] * l[0] + w[3] * l[1] + b[0])
h3 = sigmoid(w[4] * l[0] + w[5] * l[1] + b[0])
o1 = sigmoid(w[6] * h1 + w[8] * h2 + w[10] * h3 + b[1])
o2 = sigmoid(w[7] * h1 + w[9] * h2 + w[11] * h3 + b[1])
c1 = 0.1 # 目标值
c2 = 0.99
t1 = -(c1 - o1) * o1 * (1 - o1)
t2 = -(c2 - o2) * o2 * (1 - o2)
w[6] = w[6] - 0.5 * (t1 * h1)
w[8] = w[8] - 0.5 * (t1 * h2)
w[10] = w[10] - 0.5 * (t1 * h3)
w[7] = w[7] - 0.5 * (t2 * h1)
w[9] = w[9] - 0.5 * (t2 * h2)
w[11] = w[11] - 0.5 * (t2 * h3)
w[0] = w[0] - 0.5 * (t1 * w[6] + t2 * w[7]) * h1 * (1 - h1) * l[0]
w[1] = w[1] - 0.5 * (t1 * w[6] + t2 * w[7]) * h1 * (1 - h1) * l[1]
w[2] = w[2] - 0.5 * (t1 * w[8] + t2 * w[9]) * h2 * (1 - h2) * l[0]
w[3] = w[3] - 0.5 * (t1 * w[8] + t2 * w[9]) * h2 * (1 - h2) * l[1]
w[4] = w[4] - 0.5 * (t1 * w[10] + t2 * w[11]) * h3 * (1 - h3) * l[0]
w[5] = w[5] - 0.5 * (t1 * w[10] + t2 * w[11]) * h3 * (1 - h3) * l[1]
return o1, o2, w
def t_test():
w, b, l = loadDataSet()
for i in range(1001):
r1, r2, w = f1(w, b, l)
print("第{}次迭代后,结果值为:({},{}),权重更新为:{}".format(i, r1, r2, w))
t_test()
TensorFlow代码:
import tensorflow as tf
import numpy as np
#输入数据和输出数据
x_data = np.linspace(-1, 1, 300)[:, np.newaxis] #X值等差数列,在1~-1之间分成300份
noise = np.random.normal(0, 0.05, x_data.shape)#添加噪点
y_data = np.square(x_data) - 0.5 + noise#Y=X²-0.5
#定义变量
xs = tf.placeholder(tf.float32, [None, 1])
ys = tf.placeholder(tf.float32, [None, 1])
def add_layer(inputs, in_size, out_size, activation_function=None):
weights = tf.Variable(tf.random_normal([in_size, out_size]))
biases = tf.Variable(tf.zeros([out_size]) + 0.1)
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
#前向
h1 = add_layer(xs, 1, 20, activation_function=tf.nn.relu)#激活函数 relu,即 max(features, 0)。即将矩阵中每行的小于0的值都偏置为0
prediction = add_layer(h1, 20, 1, activation_function=None)
#后向
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction), reduction_indices=[1]))
train_op = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for i in range(1000):
sess.run(train_op, feed_dict={xs:x_data, ys:y_data})
if i % 50 == 0:
print(sess.run(loss, feed_dict={xs:x_data, ys:y_data}))#每迭代50次输出一个损失值