利用算法优化神经代码(python)粒子群与遗传算法相耦合优化bp网络的权值阈值(实战案例)

废话不多说直接上教程!!!!

1:导入所需的库

import numpy as np
from sklearn.datasets import make_regression
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
import random

2:创建一个简单的BP神经网络,其中包括输入层、一个隐藏层和一个输出层:

class NeuralNetwork:
    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.weights_input_hidden = np.random.randn(self.input_size, self.hidden_size)
        self.bias_hidden = np.zeros((1, self.hidden_size))
        self.weights_hidden_output = np.random.randn(self.hidden_size, self.output_size)
        self.bias_output = np.zeros((1, self.output_size))

    def sigmoid(self, x):
        return 1 / (1 + np.exp(-x))

    def forward(self, X):
        # 前向传播
        self.hidden_layer = self.sigmoid(np.dot(X, self.weights_input_hidden) + self.bias_hidden)
        self.output_layer = np.dot(self.hidden_layer, self.weights_hidden_output) + self.bias_output

        return self.output_layer

3:定义粒子群算法和遗传算法来优化神经网络的权重和阈值:

        粒子群算法:

class Particle:
    def __init__(self, position):
        self.position = position
        self.velocity = np.zeros_like(position)
        self.best_position = position
        self.best_fitness = np.inf

class ParticleSwarmOptimizer:
    def __init__(self, n_particles, n_iterations, learning_rate, neural_network):
        self.n_particles = n_particles
        self.n_iterations = n_iterations
        self.learning_rate = learning_rate
        self.neural_network = neural_network

        self.particles = [Particle(np.concatenate((np.ravel(self.neural_network.weights_input_hidden),
                                                    np.ravel(self.neural_network.bias_hidden),
                                                    np.ravel(self.neural_network.weights_hidden_output),
                                                    np.ravel(self.neural_network.bias_output)))) for _ in range(self.n_particles)]

        self.global_best_position = self.particles[0].position
        self.global_best_fitness = np.inf

    def optimize(self, X, y):
        for i in range(self.n_iterations):
            for particle in self.particles:
                # 更新速度和位置
                particle.velocity = particle.velocity + self.learning_rate * (particle.best_position - particle.position) + self.learning_rate * (self.global_best_position - particle.position)
                particle.position = particle.position + particle.velocity

                # 将位置分解为权重和偏置项
                weights_input_hidden_size = self.neural_network.input_size * self.neural_network.hidden_size
                bias_hidden_size = self.neural_network.hidden_size
                weights_hidden_output_size = self.neural_network.hidden_size * self.neural_network.output_size
                bias_output_size = self.neural_network.output_size

                weights_input_hidden_end_index = weights_input_hidden_size
                bias_hidden_end_index = weights_input_hidden_end_index + bias_hidden_size
                weights_hidden_output_end_index = bias_hidden_end_index + weights_hidden_output_size
                bias_output_end_index = weights_hidden_output_end_index + bias_output_size

                particle_weights_input_hidden = particle.position[:weights_input_hidden_end_index].reshape(self.neural_network.input_size, self.neural_network.hidden_size)
                particle_bias_hidden = particle.position[weights_input_hidden_end_index:bias_hidden_end_index].reshape(1, self.neural_network.hidden_size)
                particle_weights_hidden_output = particle.position[bias_hidden_end_index:weights_hidden_output_end_index].reshape(self.neural_network.hidden_size, self.neural_network.output_size)
                particle_bias_output = particle.position[weights_hidden_output_end_index:bias_output_end_index].reshape(1, self.neural_network.output_size)

                # 更新神经网络的权重和偏置项
                self.neural_network.weights_input_hidden = particle_weights_input_hidden
                self.neural_network.bias_hidden = particle_bias_hidden
                self.neural_network.weights_hidden_output = particle_weights_hidden_output
                self.neural_network.bias_output = particle_bias_output

                # 计算粒子适应度
                y_pred = self.neural_network.forward(X)
                fitness = np.mean((y - y_pred) ** 2)

                if fitness < particle.best_fitness:
                    particle.best_fitness = fitness
                    particle.best_position = particle.position

                if fitness < self.global_best_fitness:
                    self.global_best_fitness = fitness
                    self.global_best_position = particle.position

            print(f"Iteration {i+1}/{self.n_iterations}: Best Fitness: {self.global_best_fitness}")

        # 更新神经网络的权重和偏置项为全局最佳值
        weights_input_hidden_end_index = self.neural_network.input_size * self.neural_network.hidden_size
        bias_hidden_end_index = weights_input_hidden_end_index + self.neural_network.hidden_size
        weights_hidden_output_end_index = bias_hidden_end_index + self.neural_network.hidden_size * self.neural_network.output_size
        bias_output_end_index = weights_hidden_output_end_index + self.neural_network.output_size

        self.neural_network.weights_input_hidden = self.global_best_position[:weights_input_hidden_end_index].reshape(self.neural_network.input_size, self.neural_network.hidden_size)
        self.neural_network.bias_hidden = self.global_best_position[weights_input_hidden_end_index:bias_hidden_end_index].reshape(1, self.neural_network.hidden_size)
        self.neural_network.weights_hidden_output = self.global_best_position[bias_hidden_end_index:weights_hidden_output_end_index].reshape(self.neural_network.hidden_size, self.neural_network.output_size)
        self.neural_network.bias_output = self.global_best_position[weights_hidden_output_end_index:bias_output_end_index].reshape(1, self.neural_network.output_size)

        遗传算法:

class GeneticAlgorithm:
    def __init__(self, population_size, n_generations, crossover_rate, mutation_rate, neural_network):
        self.population_size = population_size
        self.n_generations = n_generations
        self.crossover_rate = crossover_rate
        self.mutation_rate = mutation_rate
        self.neural_network = neural_network

        # 创建初始种群
        self.population = []
        for i in range(self.population_size):
            weights_input_hidden = np.random.randn(self.neural_network.input_size, self.neural_network.hidden_size)
            bias_hidden = np.zeros((1, self.neural_network.hidden_size))
            weights_hidden_output = np.random.randn(self.neural_network.hidden_size, self.neural_network.output_size)
            bias_output = np.zeros((1, self.neural_network.output_size))

            individual = np.concatenate((np.ravel(weights_input_hidden),
                                          np.ravel(bias_hidden),
                                          np.ravel(weights_hidden_output),
                                          np.ravel(bias_output)))
            self.population.append(individual)

        self.population_fitness = []

    def fitness(self, X, y):
        # 计算种群适应度
        for individual in self.population:
            weights_input_hidden_end_index = self.neural_network.input_size * self.neural_network.hidden_size
            bias_hidden_end_index = weights_input_hidden_end_index + self.neural_network.hidden_size
            weights_hidden_output_end_index = bias_hidden_end_index + self.neural_network.hidden_size * self.neural_network.output_size
            bias_output_end_index = weights_hidden_output_end_index + self.neural_network.output_size

            weights_input_hidden = individual[:weights_input_hidden_end_index].reshape(self.neural_network.input_size, self.neural_network.hidden_size)
            bias_hidden = individual[weights_input_hidden_end_index:bias_hidden_end_index].reshape(1, self.neural_network.hidden_size)
            weights_hidden_output = individual[bias_hidden_end_index:weights_hidden_output_end_index].reshape(self.neural_network.hidden_size, self.neural_network.output_size)
            bias_output = individual[weights_hidden_output_end_index:bias_output_end_index].reshape(1, self.neural_network.output_size)

            self.neural_network.weights_input_hidden = weights_input_hidden
            self.neural_network.bias_hidden = bias_hidden
            self.neural_network.weights_hidden_output = weights_hidden_output
            self.neural_network.bias_output = bias_output

            y_pred = self.neural_network.forward(X)
            fitness = np.mean((y - y_pred) ** 2)
            self.population_fitness.append(fitness)

    def selection(self):
        # 选择父代
        parents = []
        for i in range(self.population_size):
            parent1 = self.population[random.randint(0, self.population_size - 1)]
            parent2 = self.population[random.randint(0, self.population_size - 1)]

            if self.population_fitness[self.population.index(parent1)] < self.population_fitness[self.population.index(parent2)]:
                parents.append(parent1)
            else:
                parents.append(parent2)

        return parents

    def crossover(self, parent1, parent2):
        # 交叉操作
        child = np.zeros_like(parent1)

        for i in range(len(parent1)):
            if random.random() < self.crossover_rate:
                child[i] = parent1[i]
            else:
                child[i] = parent2[i]

        return child

    def mutation(self, child):
        # 变异操作
        for i in range(len(child)):
            if random.random() < self.mutation_rate:
                child[i] += np.random.normal(0, 1)

        return child

    def optimize(self, X, y):
        for i in range(self.n_generations):
            self.fitness(X, y)
            parents = self.selection()

            # 创建下一代种群
            new_population = []
            for j in range(self.population_size):
                parent1 = parents[j]
                parent2 = parents[(j + 1) % self.population_size]

                child = self.crossover(parent1, parent2)
                child = self.mutation(child)

                new_population.append(child)

            self.population = new_population
            self.population_fitness = []

            print(f"Iteration {i+1}/{self.n_generations}: Best Fitness: {min(self.population_fitness)}")

        # 更新神经网络的权重和偏置项为最佳个体
        weights_input_hidden_end_index = self.neural_network.input_size * self.neural_network.hidden_size
        bias_hidden_end_index = weights_input_hidden_end_index + self.neural_network.hidden_size
        weights_hidden_output_end_index = bias_hidden_end_index + self.neural_network.hidden_size * self.neural_network.output_size
        bias_output_end_index = weights_hidden_output_end_index + self.neural_network.output_size

        best_individual = self.population[self.population_fitness.index(min(self.population_fitness))]
        weights_input_hidden = best_individual[:weights_input_hidden_end_index].reshape(self.neural_network.input_size, self.neural_network.hidden_size)
        bias_hidden = best_individual[weights_input_hidden_end_index:bias_hidden_end_index].reshape(1, self.neural_network.hidden_size)
        weights_hidden_output = best_individual[bias_hidden_end_index:weights_hidden_output_end_index].reshape(self.neural_network.hidden_size, self.neural_network.output_size)
        bias_output = best_individual[weights_hidden_output_end_index:bias_output_end_index].reshape(1, self.neural_network.output_size)

        self.neural_network.weights_input_hidden = weights_input_hidden
        self.neural_network.bias_hidden = bias_hidden
        self.neural_network.weights_hidden_output = weights_hidden_output
        self.neural_network.bias_output = bias_output

4:创建一个数据集并使用这些算法来训练神经网络:

# 创建数据集
X, y = make_regression(n_samples=1000, n_features=10, noise=0.1, random_state=42)
scaler = StandardScaler()
X = scaler.fit_transform(X)
y = scaler.fit_transform(y.reshape(-1, 1))

# 将数据集分成训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 创建神经网络
neural_network = NeuralNetwork(input_size=X_train.shape[1], hidden_size=10, output_size=1)

# 使用粒子群算法优化神经网络
pso_optimizer = ParticleSwarmOptimizer(n_particles=20, n_iterations=50, learning_rate=0.5, neural_network=neural_network)
pso_optimizer.optimize(X_train, y_train)

# 使用遗传算法优化神经网络
ga_optimizer = GeneticAlgorithm(population_size=20, n_generations=50, crossover_rate=0.8, mutation_rate=0.05, neural_network=neural_network)
ga_optimizer.optimize(X_train, y_train)

# 测试神经网络在测试集上的表现
y_pred_pso = neural_network.forward(X_test)
mse_pso = np.mean((y_test - y_pred_pso) ** 2)
print(f"MSE using PSO: {mse_pso}")

y_pred_ga = neural_network.forward(X_test)
mse_ga = np.mean((y_test - y_pred_ga) ** 2)
print(f"MSE using GA: {mse_ga}")

6:使用粒子群算法和遗传算法来优化BP神经网络的权重和阈值,以预测回归数据。通过运行上述代码,将获得神经网络使用这些算法进行训练的MSE值

  • 7
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

即刻编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值