粒子群优化算法(PSO)是一种基于种群的全局优化算法,用于寻找函数的全局最优解。它基于模拟鸟群或鱼群寻找食物的行为的思想。群体中的每个粒子代表一个候选解,并根据自身的经验和相邻的经验更新其在搜索空间中的位置。表现最佳的粒子,即全局最佳粒子,用于指导整个群体的搜索。
反向传播算法(BP)是一种用于训练人工神经网络的监督学习算法。它是一种基于梯度的优化方法,用于调整网络的权重,以最小化预测输出与实际目标之间的误差。误差被计算并通过网络反向传播,从而使得权重可以在下一次迭代中被更新,以减少误差。BP被用于各种应用,例如图像识别、自然语言处理和控制系统。
下面将使用sk包的PSO来优化BP神经网络。
1.load library
import numpy as np
from sko.PSO import PSO
import matplotlib.pyplot as plt
2.Define the activation function and its derivative
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(x):
return x * (1 - x)
3.Define the BP neural network
class BP_Neural_Network:
def __init__(self, input_size, hidden_size, output_size):
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
# Initialize the weight matrices
self.W1 = np.random.randn(self.input_size, self.hidden_size)
self.W2 = np.random.randn(self.hidden_size, self.output_size)
def forward(self, X):
self.z = np.dot(X, self.W1)
self.z2 = sigmoid(self.z)
self.z3 = np.dot(self.z2, self.W2)
o = sigmoid(self.z3)
return o
def backward(self, X, y, o):
self.o_error = y - o
self.o_delta = self.o_error * sigmoid_derivative(o)
self.z2_error = self.o_delta.dot(self.W2.T)
self.z2_delta = self.z2_error * sigmoid_derivative(self.z2)
self.W1 += X.T.dot(self.z2_delta)
self.W2 += self.z2.T.dot(self.o_delta)
def train(self, X, y):
o = self.forward(X)
self.backward(X, y, o)
def predict(self, X):
return self.forward(X)
4.定义目标函数
def obj_func(weights):
nn.W1 = weights[0:3 * 2].reshape((3, 2))
nn.W2 = weights[3 * 2:].reshape((2, 1))
y_pred = nn.predict(X_train)
return np.mean(np.power(y_train - y_pred, 2))
5.初始化BP神经网络
nn = BP_Neural_Network(input_size=3, hidden_size=2, output_size=1)
6.初始化输入输出数据
X_train = np.array([[0, 0, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]])
y_train = np.array([[0], [1], [1], [0]])
7.初始化PSO算法
pso = PSO(func=obj_func, n_dim=3 * 2 + 2, lb=[-10, -10, -10, -10, -10, -10, -10, -10], ub=[10, 10, 10, 10, 10, 10, 10, 10], pop=30, max_iter=100)
8.PSO优化BP权重
pso.run()
nn.W1 = pso.gbest_x[0:3 * 2].reshape((3, 2))
nn.W2 = pso.gbest_x[3 * 2:].reshape((2, 1))
9.使用优化后的BP进行预测
y_pred = nn.predict(X_train)
print('Predicted outputs:', y_pred)
Predicted outputs: [[5.19002550e-05]
[9.99948100e-01]
[9.99948101e-01]
[5.19000440e-05]]
plt.plot(pso.gbest_y_hist)
plt.xlabel('Iteration')
plt.ylabel('Mean Squared Error')
plt.show()
首发于知乎:https://www.zhihu.com/people/quan-fu-wu-zhuang-de-da-shi-xiong-73/posts