智能优化算法:鸟群算法-附代码
发布时间:2024-07-22 07:56:27 作者:佚名
鸟群算法(Particle Swarm Optimization,PSO)是一种基于群体
智能的
优化算法,可以用于优化BP神经网络的参数。下面是使用PSO
算法优化BP神经网络的
代码示例:
```
python
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# 加载数据集
iris=load_iris()
X=iris.data
y=iris.target
# 数据预处理
scaler=StandardScaler()
X=scaler.fit_transform(X)
# 划分训练集和测试集
X_train, X_test, y_train, y_test=train_test_split(X, y, test_size=0.3, random_state=42)
# BP神经网络
class BPNN:
def __init__(self, input_size, hidden_size, output_size):
self.input_size=input_size
self.hidden_size=hidden_size
self.output_size=output_size
self.W1=np.random.randn(self.input_size, self.hidden_size)
self.b1=np.zeros((1, self.hidden_size))
self.W2=np.random.randn(self.hidden_size, self.output_size)
self.b2=np.zeros((1, self.output_size))
def sigmoid(self, x):
return 1 / (1 + np.exp(-x))
def forward(self, X):
self.z1=np.dot(X, self.W1) + self.b1
self.a1=self.sigmoid(self.z1)
self.z2=np.dot(self.a1, self.W2) + self.b2
self.a2=self.sigmoid(self.z2)
return self.a2
def loss(self, X, y):
y_pred=self.forward(X)
L=0.5 * np.sum((y - y_pred) ** 2)
return L
def accuracy(self, X, y):
y_pred=self.predict(X)
acc=np.mean(y_pred==y)
return acc
def predict(self, X):
y_pred=np.argmax(self.forward(X), axis=1)
return y_pred
# PSO
算法
class PSO:
def __init__(self, n_particles, input_size, hidden_size, output_size, max_iter):
self.n_particles=n_particles
self.input_size=input_size
self.hidden_size=hidden_size
self.output_size=output_size
self.max_iter=max_iter
self.particles=[]
self.gbest_loss=float('inf')
self.gbest_W1=None
self.gbest_b1=None
self.gbest_W2=None
self.gbest_b2=None
for i in range(self.n_particles):
particle={}
particle['W1']=np.random.randn(self.input_size, self.hidden_size)
particle['b1']=np.zeros((1, self.hidden_size))
particle['W2']=np.random.randn(self.hidden_size, self.output_size)
particle['b2']=np.zeros((1, self.output_size))
particle['v_W1']=np.zeros((self.input_size, self.hidden_size))
particle['v_b1']=np.zeros((1, self.hidden_size))
particle['v_W2']=np.zeros((self.hidden_size, self.output_size))
particle['v_b2']=np.zeros((1, self.output_size))
particle['pbest_loss']=float('inf')
particle['pbest_W1']=None
particle['pbest_b1']=None
particle['pbest_W2']=None
particle['pbest_b2']=None
self.particles.append(particle)
def sigmoid(self, x):
return 1 / (1 + np.exp(-x))
def forward(self, X, W1, b1, W2, b2):
z1=np.dot(X, W1) + b1
a1=self.sigmoid(z1)
z2=np.dot(a1, W2) + b2
a2=self.sigmoid(z2)
return a2
def loss(self, X, y, W1, b1, W2, b2):
y_pred=self.forward(X, W1, b1, W2, b2)
L=0.5 * np.sum((y - y_pred) ** 2)
return L
def update(self, X, y):
for i in range(self.n_particles):
particle=self.particles[i]
W1=particle['W1']
b1=particle['b1']
W2=particle['W2']
b2=particle['b2']
v_W1=particle['v_W1']
v_b1=particle['v_b1']
v_W2=particle['v_W2']
v_b2=particle['v_b2']
pbest_loss=particle['pbest_loss']
pbest_W1=particle['pbest_W1']
pbest_b1=particle['pbest_b1']
pbest_W2=particle['pbest_W2']
pbest_b2=particle['pbest_b2']
c1=2
c2=2
r1=np.random.rand()
r2=np.random.rand()
v_W1=0.5 * v_W1 + c1 * r1 * (pbest_W1 - W1) + c2 * r2 * (self.gbest_W1 - W1)
v_b1=0.5 * v_b1 + c1 * r1 * (pbest_b1 - b1) + c2 * r2 * (self.gbest_b1 - b1)
v_W2=0.5 * v_W2 + c1 * r1 * (pbest_W2 - W2) + c2 * r2 * (self.gbest_W2 - W2)
v_b2=0.5 * v_b2 + c1 * r1 * (pbest_b2 - b2) + c2 * r2 * (self.gbest_b2 - b2)
W1=W1 + v_W1
b1=b1 + v_b1
W2=W2 + v_W2
b2=b2 + v_b2
loss=self.loss(X, y, W1, b1, W2, b2)
if loss < pbest_loss:
particle['pbest_loss']=loss
particle['pbest_W1']=W1
particle['pbest_b1']=b1
particle['pbest_W2']=W2
particle['pbest_b2']=b2
if loss < self.gbest_loss:
self.gbest_loss=loss
self.gbest_W1=W1
self.gbest_b1=b1
self.gbest_W2=W2
self.gbest_b2=b2
particle['W1']=W1
particle['b1']=b1
particle['W2']=W2
particle['b2']=b2
particle['v_W1']=v_W1
particle['v_b1']=v_b1
particle['v_W2']=v_W2
particle['v_b2']=v_b2
def train(self, X, y):
for i in range(self.max_iter):
self.update(X, y)
print('Iteration:', i, 'Loss:', self.gbest_loss)
# 训练模型
input_size=X_train.shape[1]
hidden_size=10
output_size=len(np.unique(y_train))
n_particles=10
max_iter=100
pso=PSO(n_particles, input_size, hidden_size, output_size, max_iter)
pso.train(X_train, y_train)
# 测试模型
y_pred=np.argmax(pso.forward(X_test, pso.gbest_W1, pso.gbest_b1, pso.gbest_W2, pso.gbest_b2), axis=1)
acc=np.mean(y_pred==y_test)
print('Accuracy:', acc)
```