news 2026/4/18 9:16:22

BP神经网络遗传算法寻优代码模型解析

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
BP神经网络遗传算法寻优代码模型解析

bp神经网络遗传算法寻优代码模型,注释清楚,可以运行,

最近在研究优化算法,发现BP神经网络结合遗传算法来寻优真的超有趣!今天就来给大家分享一下相关的代码模型,并且穿插着讲讲其中的门道。

首先呢,我们来看一下整体的代码框架。这里用Python来实现,先导入必要的库:

import numpy as np import matplotlib.pyplot as plt import random

numpy用于数值计算,matplotlib用来绘图,random则是为了生成随机数,方便后续遗传算法中的一些操作。

接下来定义BP神经网络的结构和相关参数:

# 定义输入层、隐藏层和输出层的节点数 input_layer_size = 2 hidden_layer_size = 3 output_layer_size = 1 # 学习率 alpha = 0.1

这里我们设置输入层有2个节点,隐藏层有3个节点,输出层有1个节点,学习率为0.1。学习率控制着每次权重更新的幅度,太小会导致收敛慢,太大可能会错过最优解。

然后是BP神经网络的前向传播函数:

def sigmoid(z): return 1 / (1 + np.exp(-z)) def forward_propagation(X, theta1, theta2): a1 = np.hstack([np.ones((X.shape[0], 1)), X]) z2 = a1.dot(theta1.T) a2 = sigmoid(z2) a2 = np.hstack([np.ones((a2.shape[0], 1)), a2]) z3 = a2.dot(theta2.T) h = sigmoid(z3) return h, a1, z2, a2, z3

sigmoid函数是BP神经网络中常用的激活函数,它能将输入值映射到0到1之间。forward_propagation函数实现了前向传播的过程,从输入层经过隐藏层到输出层,依次计算每个节点的输出。

接着是计算代价函数(这里用均方误差):

def cost_function(X, y, theta1, theta2): m = X.shape[0] h, _, _, _, _ = forward_propagation(X, theta1, theta2) J = (1 / (2 * m)) * np.sum((h - y) ** 2) return J

代价函数衡量了预测值与真实值之间的误差,我们希望通过优化权重来使代价函数最小化。

再来看反向传播函数,这可是BP神经网络的核心:

def back_propagation(X, y, theta1, theta2, h, a1, z2, a2, z3): m = X.shape[0] delta3 = h - y delta2 = delta3.dot(theta2) * (a2 * (1 - a2)) delta2 = delta2[:, 1:] theta1_grad = (1 / m) * delta2.T.dot(a1) theta2_grad = (1 / m) * delta3.T.dot(a2) return theta1_grad, theta2_grad

反向传播通过计算误差的梯度,来更新权重。这里根据输出层和隐藏层的误差,逐步计算出对权重的梯度。

然后是更新权重的函数:

def update_weights(theta1, theta2, theta1_grad, theta2_grad): theta1 = theta1 - alpha * theta1_grad theta2 = theta2 - alpha * theta2_grad return theta1, theta2

根据梯度和学习率来更新权重。

现在进入遗传算法部分。首先初始化种群:

# 初始化种群 def initialize_population(population_size, theta1_size, theta2_size): population = [] for _ in range(population_size): theta1 = np.random.rand(hidden_layer_size, input_layer_size + 1) theta2 = np.random.rand(output_layer_size, hidden_layer_size + 1) population.append([theta1, theta2]) return population

随机生成一定数量的权重组合作为初始种群。

bp神经网络遗传算法寻优代码模型,注释清楚,可以运行,

然后是计算适应度函数,这里用代价函数来衡量:

def fitness_function(population, X, y): fitness = [] for individual in population: theta1, theta2 = individual J = cost_function(X, y, theta1, theta2) fitness.append(1 / J) # 因为要最大化适应度,所以用1/代价函数 return fitness

适应度越高表示个体越优。

接着是选择操作,这里用轮盘赌选择:

def roulette_wheel_selection(population, fitness): total_fitness = sum(fitness) selection_probabilities = [fit / total_fitness for fit in fitness] selected_index = np.random.choice(len(population), p=selection_probabilities) return population[selected_index]

轮盘赌选择根据个体的适应度比例来选择个体。

交叉操作:

def crossover(parent1, parent2): theta1_crossover_point = random.randint(0, parent1[0].shape[0] - 1) theta2_crossover_point = random.randint(0, parent1[1].shape[0] - 1) child1_theta1 = np.vstack([parent1[0][:theta1_crossover_point, :], parent2[0][theta1_crossover_point:, :]]) child1_theta2 = np.vstack([parent1[1][:theta2_crossover_point, :], parent2[1][theta2_crossover_point:, :]]) child2_theta1 = np.vstack([parent2[0][:theta1_crossover_point, :], parent1[0][theta1_crossover_point:, :]]) child2_theta2 = np.vstack([parent2[1][:theta2_crossover_point, :], parent1[1][theta2_crossover_point:, :]]) return [child1_theta1, child1_theta2], [child2_theta1, child2_theta2]

交叉操作交换父母个体的部分权重信息。

变异操作:

def mutation(individual, mutation_rate): theta1, theta2 = individual for i in range(theta1.shape[0]): for j in range(theta1.shape[1]): if random.random() < mutation_rate: theta1[i, j] = np.random.rand() for i in range(theta2.shape[0]): for j in range(theta2.shape[1]): if random.random() < mutation_rate: theta2[i, j] = np.random.rand() return [theta1, theta2]

变异操作以一定概率随机改变权重值,防止算法陷入局部最优。

最后是遗传算法的主循环:

# 遗传算法主循环 def genetic_algorithm(X, y, population_size, generations, mutation_rate): population = initialize_population(population_size, (hidden_layer_size, input_layer_size + 1), (output_layer_size, hidden_layer_size + 1)) best_fitness = -np.inf best_individual = None for gen in range(generations): fitness = fitness_function(population, X, y) for i in range(population_size // 2): parent1 = roulette_wheel_selection(population, fitness) parent2 = roulette_wheel_selection(population, fitness) child1, child2 = crossover(parent1, parent2) child1 = mutation(child1, mutation_rate) child2 = mutation(child2, mutation_rate) population[i * 2] = child1 population[i * 2 + 1] = child2 current_best_fitness = max(fitness) if current_best_fitness > best_fitness: best_fitness = current_best_fitness best_individual = population[np.argmax(fitness)] print(f"Generation {gen}: Best Fitness = {best_fitness}") return best_individual

在主循环中,不断进行选择、交叉和变异操作,更新种群,最终找到最优的权重组合。

完整的代码运行起来后,就能通过BP神经网络结合遗传算法来对给定的数据进行寻优啦!

通过这样的代码模型,我们可以看到BP神经网络和遗传算法是如何相互协作,一步步找到最优解的。是不是很神奇?希望这篇分享能让大家对这个有趣的优化算法组合有更清晰的了解!

以上就是今天的全部内容啦,代码中的每一步都是为了实现更好的寻优效果,大家可以根据实际需求调整参数,进一步探索这个模型的魅力!

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/18 3:30:09

Emotion2Vec+ Large是否适合儿童语音?年龄适应性实测报告

Emotion2Vec Large是否适合儿童语音&#xff1f;年龄适应性实测报告 1. 引言&#xff1a;儿童语音情感识别的现实挑战 随着智能教育、儿童心理评估和人机交互技术的发展&#xff0c;对儿童语音的情感识别需求日益增长。传统语音情感识别模型大多基于成人语料训练&#xff0c;…

作者头像 李华
网站建设 2026/4/18 3:27:58

三菱FX3U 16仓位配方程序开发记录

三菱FX3U&#xff0c;用ST语言与梯形图&#xff0c;混合编写的16仓位的配方程序&#xff0c;程序大小约12984步&#xff0c;可以配1到16种不同的产品&#xff0c;16种配方可以根据自己的需求随意设置配方数量与产品数量&#xff0c;可以用条形码设置配方数据与生产数量&#xf…

作者头像 李华
网站建设 2026/4/18 3:30:16

电动汽车Simulink仿真模型的奇妙世界

电动汽车 simulink仿真模型&#xff0c; 可进行整车动力性仿真测试&#xff08;最高车速&#xff0c;最大爬坡&#xff0c;加入时间)和NEDC工况能耗测试&#xff08;电耗)。 由驾驶员模型、VCU控制制模型、电机 电池系统模型(电机系统和电池系统已根据供应商提供的方案数据进行…

作者头像 李华
网站建设 2026/4/18 3:31:29

分享| 2026人工智能训练师报考指南

在这个技术迅猛发展的时代&#xff0c;你有没有发现一个变化&#xff1f;一段文字能生成视频&#xff0c;CT影像可自动诊断&#xff0c;城市交通正由算法调度……人工智能已成为经济发展核心驱动力。而人工智能训练工程师作为变革的关键角色&#xff0c;已经一步一步成为职场新…

作者头像 李华
网站建设 2026/4/18 3:48:28

大模型在创新设计推理任务中的表现

大模型在创新设计推理任务中的表现 关键词:大模型、创新设计推理、表现评估、技术原理、应用场景 摘要:本文聚焦于大模型在创新设计推理任务中的表现。首先介绍了研究的背景,包括目的、预期读者、文档结构和相关术语。接着阐述了大模型与创新设计推理的核心概念及联系,分析…

作者头像 李华