2020_10_30
神经网络:BP算法
误差逆传播算法(Error BackPropagation,简称BP),是多层神经网络中最成功的神经网络学习算法。
Python
import numpy as np
import matplotlib.pyplot as plt
x = np.array([2, 5], dtype=float)
y_true = np.array([3, 6, 4], dtype=float)
# sigmond
def sigmoid(x):
s = 1 / (1 + np.exp(-x))
return s
def sigmoid_derivative(x):
s = 1 / (1 + np.exp(-x))
ds = s * (1 - s)
return ds
def BP(x, y, num):
epochs = 20000
learning_rate = 0.3
I_num = x.shape[0]
H_num = num
O_num = y.shape[0]
V = np.random.rand(I_num, H_num) - 0.5
dV = np.random.rand(I_num, H_num)
W = np.random.rand(H_num, O_num) - 0.5
dW = np.random.rand(H_num, O_num)
mse_record = np.ones((1, epochs))
for step in range(epochs):
# Forward
H_in = np.dot(x, V) # 1