2026/5/21 16:00:38
网站建设
项目流程
常平镇网站仿做,做鞋子的网站,科技感的网站,滦南网站建设推广一、环境配置
详见#xff1a;Windows 下 PyTorch 入门深度学习环境安装与配置 GPU 版
https://blog.csdn.net/weixin_61034701/article/details/134837665
二、实验代码
百度网盘
链接:
https://pan.baidu.com/s/1GRsjnAx_0bVafxiLf69SDg?pwd9999 提取码: 9999
三、实…一、环境配置详见Windows 下 PyTorch 入门深度学习环境安装与配置 GPU 版https://blog.csdn.net/weixin_61034701/article/details/134837665二、实验代码百度网盘链接: https://pan.baidu.com/s/1GRsjnAx_0bVafxiLf69SDg?pwd9999 提取码: 9999三、实验报告实验一1、请简述本项目所涉及算法的基本原理200字本实验是一个简单的神经网络模型用于识别手写数字MNIST数据集。首先它加载了MNIST数据集然后将数据预处理为张量并创建了训练和测试数据集。接下来定义了一个包含三个全连接层的神经网络模型并使用随机梯度下降优化器进行训练。训练过程中每个epoch都会调用train_epoch函数来进行训练并在每个epoch结束后调用test函数来进行测试。最后通过调用train函数来进行模型的训练和测试。2、请设计一个算法的测试用例截图并给出简单的分析50字总体来说实现了一个简单的神经网络模型并使用MNIST数据集进行训练和测试。3、请简述在此项目中你的Python编程有什么收获50字我学会了如何使用TensorFlow和Keras构建简单的神经网络模型并且学会了如何加载和处理数据集进行模型训练和测试。实验二1、请简述本项目所涉及算法的基本原理200字卷积神经网络是一种深度学习模型主要用于处理具有网格结构的数据如图像、语音等。它通过多个卷积层和池化层来提取特征然后通过全连接层进行分类或回归任务。全连接网络是一种多层感知机模型它通过多个全连接层将输入数据映射到输出结果。全连接网络的基本原理包括输入层、隐藏层和输出层。输入层接收原始数据隐藏层通过激活函数进行非线性变换输出层输出最终预测结果。2、请设计一个算法的测试用例截图并给出简单的分析50字通过多个卷积层和池化层提取特征再通过全连接层进行分类或回归任务。3、请简述在此项目中你的Python编程有什么收获50字通过此项目我掌握了卷积神经网络和全连接网络的原理和实现方法并能够使用它们进行分类和回归任务。实验三1、请简述本项目所涉及算法的基本原理200字在PyTorch框架下实现U-Net需要定义编码器和解码器的网络结构并实现前向传播和损失函数计算。训练过程通常使用带有已标记图像的数据集通过优化算法来调整网络参数以最小化预测分割结果与真实标签之间的差异。测试阶段则利用已训练好的模型对新图像进行分割预测。网络的训练和测试代码需要包括数据加载、模型定义、损失函数的定义、优化器的设置以及训练循环和测试过程等步骤以完成图像分割任务。2、请设计一个算法的测试用例截图并给出简单的分析50字实现了一个基础的U-Net模型是一种用于图像分割任务的深度学习网络。3、请简述在此项目中你的Python编程有什么收获50字通过实现U-Net模型我学会了如何在PyTorch框架下定义神经网络结构、前向传播和损失函数计算并掌握了使用数据集进行模型训练和测试的方法。实验四1、请简述本项目所涉及算法的基本原理200字该算法基于PyTorch Geometric库用于训练和测试一个图神经网络模型。首先它加载了一个名为Cora的数据集并将其划分为训练集、验证集和测试集。然后定义了一个名为Net的神经网络模型该模型包含两个SplineConv层用于处理节点特征和边属性。接下来将模型和数据移动到设备GPU或CPU上并创建一个Adam优化器来更新模型参数。最后定义了训练函数和测试函数并在主程序入口中进行200轮训练和测试。2、请设计一个算法的测试用例截图并给出简单的分析50字显然四种方法实验结果做对比现有的代码比其他三种方法训练测试结果更精准。3、请简述在此项目中你的Python编程有什么收获50字在此项目中我学会了如何使用Python和PyTorch库构建神经网络模型以及如何进行数据预处理、模型训练和评估。实验五A*算法实现迷宫一、实验内容及目的本实验以A算法为研究对象分析了其在迷宫寻路问题中的应用采用Python语言设计本实验以A算法为研究对象分析了其在迷宫寻路问题中的应用采用Python语言设计并实现了一个基于A*算法的迷宫求解程序解决了在给定迷宫中从起点到终点的最短路径问题达到提高算法效率的目的。同时对算法进行了优化提高了求解速度和准确性。二、实验环境操作系统Windows11 家庭中文版开发环境PyCharm 2021、anaconda3、pytorch-cuda 11.8三、实验设计与实现1.将起点添加到开放列表openList中。2.当未找到终点或开放列表为空时执行以下操作a.检查终点是否在开放列表中如果在则结束搜索。b.对开放列表进行排序选择估值最小的节点作为当前节点。c.将当前节点从开放列表中移除并添加到关闭列表closeList中。d.对当前节点的上下左右四个相邻节点进行判断和处理3.如果相邻节点在地图范围内且不是障碍物则将其添加到开放列表或关闭列表中根据其评估值更新其在开放列表或关闭列表中的位置。5.当找到终点时通过回溯父节点的方式构建从起点到终点的路径。四、实验结果与测试测试用例1如图4-1所示。图4-1 测试用例1测试用例2如图4-2所示。图4-2 测试用例2附录源代码import warnings warnings.filterwarnings(ignore) import numpy as np from tkinter import Button from tkinter import Tk from tkinter import Canvas class Maze(object): # 类的构造函数用于初始化类的属性和UI界面。 def __init__(self): self.blockcolorIndex 0 self.blockcolor [black, yellow, red, green] # 障碍颜色为黑色、起点黄色 终点红色、路径绿色 self.mapStatus np.ones((15, 15), dtypeint) # 地图状态数组全0数组 1无障碍 0障碍 self.startPoint start # 起点 self.endPoint end # 终点 self.selectedStart False # 是否选了起点 默认否 self.selectedEnd False # 是否选了终点 默认否 self.openList [] # open表 self.closeList [] # close表 self.isOK False # 是否已经结束寻路 self.route [] # 路径列表 # Python的Tkinter库创建图形用户界面GUI的代码 self.root Tk() self.root.title(走迷宫(A*算法)) self.root.geometry(800x8003000) self.btn_obstacle Button(self.root, text选择障碍, commandself.selectobstacle) # 将按钮添加到窗口中 self.btn_obstacle.pack() self.btn_start Button(self.root, text选择起点, commandself.selectstart) self.btn_start.pack() self.btn_end Button(self.root, text选择终点, commandself.selectend) self.btn_end.pack() self.btn_action Button(self.root, text开始寻路, commandself.selectaction) self.btn_action.pack() self.btn_restart Button(self.root, text重新开始, commandself.selectrestart) self.btn_restart.pack() # 创建一个画布宽度和高度都为500像素背景颜色为白色。 self.canvas Canvas(self.root, width500, height500, bgwhite) self.canvas.pack() # 循环16次每次循环都会执行下面的代码块。 for i in range(1, 17): # 横线 self.canvas.create_line(30, 30 * i, 480, 30 * i) # 竖线 self.canvas.create_line(30 * i, 30, 30 * i, 480) # 将鼠标左键单击事件绑定到self.drawMapBlock方法。 self.canvas.bind(Button-1, self.drawMapBlock) # 启动Tkinter的事件循环使窗口保持打开状态等待用户的交互操作。 self.root.mainloop() # 重置地图、起点、终点等变量并重新绘制地图 def selectrestart(self): # 地图状态数组全0数组 1无障碍 0障碍 self.mapStatus np.ones((15, 15), dtypeint) self.startPoint start self.endPoint end # 是否选了起点 默认否 self.selectedStart False # 是否选了终点 默认否 self.selectedEnd False # open表 self.openList [] # close表 self.closeList [] # 是否已经结束 self.isOK False self.route [] self.canvas.destroy() self.canvas Canvas(self.root, width500, height500, bgwhite) self.canvas.pack() for i in range(1, 17): # 横线 self.canvas.create_line(30, 30 * i, 480, 30 * i) # 竖线 self.canvas.create_line(30 * i, 30, 30 * i, 480) self.canvas.bind(Button-1, self.drawMapBlock) # 选择黑色作为障碍物的颜色 def selectobstacle(self): # black self.blockcolorIndex 0 def selectstart(self): # 起始点未被选中 if not self.selectedStart: # yellow self.blockcolorIndex 1 else: # black self.blockcolorIndex 0 def selectend(self): if not self.selectedEnd: # red self.blockcolorIndex 2 else: # black self.blockcolorIndex 0 def selectaction(self): # green self.blockcolorIndex 3 self.Astart() # 列表中删除最后一个元素 self.route.pop(-1) # 列表中删除第一个元素。 self.route.pop(0) for i in self.route: self.canvas.create_rectangle((i.x 1) * 30, (i.y 1) * 30, (i.x 2) * 30, (i.y 2) * 30, fillgreen) # 这段代码是一个A寻路算法的实现。用于在图形中寻找从起点到终点的最短路径。 # 1.将起点添加到开放列表openList中。 # 2. 当未找到终点或开放列表为空时执行以下操作 # a.检查终点是否在开放列表中如果在则结束搜索。 # b.对开放列表进行排序选择估值最小的节点作为当前节点。 # c.将当前节点从开放列表中移除并添加到关闭列表closeList中。 # d.对当前节点的上下左右四个相邻节点进行判断和处理 # 3.如果相邻节点在地图范围内且不是障碍物则将其添加到开放列表或关闭列表中根据其评估值更新其在开放列表或关闭列表中的位置。 # 当找到终点时通过回溯父节点的方式构建从起点到终点的路径。 def Astart(self): # 将起点放到open表中 self.openList.append(self.startPoint) while (not self.isOK): # 先检查终点是否在open表中 没有继续有则结束 if self.inOpenList(self.endPoint) ! -1: self.isOK True # self.end self.openList[self.inOpenList(self.endPoint)] self.route.append(self.end) self.te self.end while (self.te.parentPoint ! 0): self.te self.te.parentPoint self.route.append(self.te) else: # 将估值最小的节点放在index 0 self.sortOpenList() # 估值最小节点 current_min self.openList[0] self.openList.pop(0) self.closeList.append(current_min) # 开拓current_min节点并放到open 表 # 这段代码是在一个二维地图上进行路径搜索的算法。它首先检查当前节点的上下左右四个方向如果某个方向上的点在地图范围内且不是障碍物用0表示则将该点作为临时节点。 # 然后它会检查这个临时节点是否已经在open列表或close列表中。如果在open列表中并且该节点的距离比open列表中的节点更短那么就用新的节点替换open列表中的节点。 # 如果在close列表中并且该节点的距离比close列表中的节点更短那么就用新的节点替换close列表中的节点。 # 如果临时节点既不在open列表也不在close列表中那么就将其添加到open列表中以便后续继续搜索。 # 没有越界 if current_min.x - 1 0: # 非障碍,可开拓 if (self.mapStatus[current_min.y][current_min.x - 1]) ! 0: self.temp1 mapPoint(current_min.x - 1, current_min.y, current_min.distanceStart 1, self.endPoint.x, self.endPoint.y, current_min) # open表存在相同的节点 if self.inOpenList(self.temp1) ! -1: if self.temp1.evaluate() self.openList[self.inOpenList(self.temp1)].evaluate(): self.openList[self.inOpenList(self.temp1)] self.temp1 # 否则查看close表是否存在相同的节点存在 elif self.inCloseList(self.temp1) ! -1: if self.temp1.evaluate() self.closeList[self.inCloseList(self.temp1)].evaluate(): self.closeList[self.inCloseList(self.temp1)] self.temp1 # open 、 close表都不存在 temp1 else: self.openList.append(self.temp1) if current_min.x 1 15: # 非障碍,可开拓 if (self.mapStatus[current_min.y][current_min.x 1]) ! 0: self.temp2 mapPoint(current_min.x 1, current_min.y, current_min.distanceStart 1, self.endPoint.x, self.endPoint.y, current_min) # open表存在相同的节点 if self.inOpenList(self.temp2) ! -1: if self.temp2.evaluate() self.openList[self.inOpenList(self.temp2)].evaluate(): self.openList[self.inOpenList(self.temp2)] self.temp2 # 否则查看close表是否存在相同的节点存在 elif self.inCloseList(self.temp2) ! -1: if self.temp2.evaluate() self.closeList[self.inCloseList(self.temp2)].evaluate(): self.closeList[self.inCloseList(self.temp2)] self.temp2 else: self.openList.append(self.temp2) if current_min.y - 1 0: # 非障碍,可开拓 if (self.mapStatus[current_min.y - 1][current_min.x]) ! 0: self.temp3 mapPoint(current_min.x, current_min.y - 1, current_min.distanceStart 1, self.endPoint.x, self.endPoint.y, current_min) # open表存在相同的节点 if self.inOpenList(self.temp3) ! -1: if self.temp3.evaluate() self.openList[self.inOpenList(self.temp3)].evaluate(): self.openList[self.inOpenList(self.temp3)] self.temp3 # 否则查看close表是否存在相同的节点存在 elif self.inCloseList(self.temp3) ! -1: if self.temp3.evaluate() self.closeList[self.inCloseList(self.temp3)].evaluate(): self.closeList[self.inCloseList(self.temp3)] self.temp3 else: self.openList.append(self.temp3) if current_min.y 1 15: # 非障碍,可开拓 if (self.mapStatus[current_min.y 1][current_min.x]) ! 0: self.temp4 mapPoint(current_min.x, current_min.y 1, current_min.distanceStart 1, self.endPoint.x, self.endPoint.y, current_min) # open表存在相同的节点 if self.inOpenList(self.temp4) ! -1: if self.temp4.evaluate() self.openList[self.inOpenList(self.temp4)].evaluate(): self.openList[self.inOpenList(self.temp4)] self.temp4 # 否则查看close表是否存在相同的节点存在 elif self.inCloseList(self.temp4) ! -1: if self.temp4.evaluate() self.closeList[self.inCloseList(self.temp4)].evaluate(): self.closeList[self.inCloseList(self.temp4)] self.temp4 else: self.openList.append(self.temp4) # 表示地图上的点 def drawMapBlock(self, event): # 获取鼠标点击的x坐标和y坐标 x, y event.x, event.y # 判断鼠标点击的位置是否在地图范围内30到480之间 if (30 x 480) and (30 y 480): # 当前点击位置对应的行号和列号 i int((x // 30) - 1) j int((y // 30) - 1) # 记录下起止点并不能选择多个起点或者多个终点 # 如果当前颜色索引为1且未选择起点将起点设置为当前点击位置并将selectedStart设置为True。然后在画布上绘制起点矩形。 if self.blockcolorIndex 1 and not self.selectedStart: self.startPoint mapPoint(i, j, 0, 0, 0, 0) self.selectedStart True self.canvas.create_rectangle((i 1) * 30, (j 1) * 30, (i 2) * 30, (j 2) * 30, fillself.blockcolor[self.blockcolorIndex]) self.blockcolorIndex 0 # 如果当前颜色索引为2且未选择终点将终点设置为当前点击位置并将selectedEnd设置为True。然后在画布上绘制终点矩形。 elif self.blockcolorIndex 2 and not self.selectedEnd: self.endPoint mapPoint(i, j, 0, 0, 0, 0) self.selectedEnd True self.canvas.create_rectangle((i 1) * 30, (j 1) * 30, (i 2) * 30, (j 2) * 30, fillself.blockcolor[self.blockcolorIndex]) self.blockcolorIndex 0 # 如果当前颜色索引不为1或2直接在画布上绘制当前颜色索引对应的矩形 else: self.canvas.create_rectangle((i 1) * 30, (j 1) * 30, (i 2) * 30, (j 2) * 30, fillself.blockcolor[self.blockcolorIndex]) self.mapStatus[j][i] self.blockcolorIndex # 检查终点是否在open表中 def endInOpenList(self): for i in self.openList: if self.endPoint[0] i.x and self.endPoint[1] i.y: return True return False # 将节点加进open表前检查该节点是否在open表中 def inOpenList(self, p1): for i in range(0, len(self.openList)): if p1.isEq(self.openList[i]): return i return -1 # 将节点加进open表前检查该节点是否在close表中 # 若在返回索引不在返回-1 def inCloseList(self, p1): for i in range(0, len(self.closeList)): if p1.isEq(self.closeList[i]): return i return -1 # 将 估值最小的 排在 index 0 def sortOpenList(self): if len(self.openList) 0: if len(self.openList) 1: for i in range(1, len(self.openList)): if self.openList[i].evaluate() self.openList[0].evaluate(): self.t self.openList[0] self.openList[0] self.openList[i] self.openList[i] self.t class mapPoint(object): def __init__(self, x, y, distanceStart, endX, endY, parentPoint): self.x x self.y y self.distanceStart distanceStart self.endX endX self.endY endY self.parentPoint parentPoint # 前一个节点 def evaluate(self): # 返回一个计算结果该结果是distanceStart加上x和endX之间的绝对差值再加上y和endY之间的绝对差值。 return self.distanceStart abs(self.x - self.endX) abs(self.y - self.endY) def isEq(self, point): # 检查传入的point对象的x和y属性是否与当前实例的x和y属性相等。如果相等它返回True否则它返回False。 if point.x self.x and point.y self.y: return True else: return False def main(): Maze() if __name__ __main__: main()实验六遗传算法求函数最小值问题一、实验内容及目的本实验以遗传算法为研究对象分析了其在求解函数最小值问题中的应用。采用Java语言设计并实现了一个基于遗传算法的函数最小值求解程序解决了多变量、非线性、非凸函数的全局最优解问题达到提高求解效率和准确性的目的。同时对遗传算法进行了改进提出了自适应参数调整策略提高了算法的收敛速度和稳定性。二、实验环境操作系统Windows 11 家庭中文版开发环境IntelliJ IDEA 2020.3.4/Java等三、实验设计与实现遗传算法Genetic Algorithm简称GA起源于对生物系统所进行的计算机模拟研究是一种随机全局搜索优化方法它模拟了自然选择和遗传中发生的复制、交叉和变异等现象从任一初始种群出发通过随机选择、交叉和变异操作产生一群更适合环境的个体使群体进化到搜索空间中越来越好的区域这样一代一代不断繁衍进化最后收敛到一群最适应环境的个体从而求得问题的优质解。遗传算法求函数最小值问题流程图如图2-1所示。图2-1遗传算法求函数最小值问题流程图1初始种群的产生种群规模为10需要有a、b、c、d,4个参数,size为4随机生成解for (int i 0; i NP; i) {for (int j 0; j SIZE; j) {init_population[i][j] MIN Math.random() * (MAX - MIN);}}2适应度函数函数式为 y-a2b-3c4d其中a,b,c,d ~[-2,2]传入个体计算Y值 。public static double function(double[] individual) {return -individual[0] 2 * individual[1] - 3 * individual[2] 4 * individual[3];}3遗传算子的确定对于选择步骤采取轮盘赌选择从头开始遍历累加和数组addSumP找到第一个大于r的元素的下标k那么个体下标index就为k将选中的个体复制到select_population数组中。for (int i 0; i NP; i) {double r Math.random();int index 0;for (int k 0; k NP; k) {if (r addSumP[k]) {index k;break;}}select_population[i] init_population[index];}对于交叉步骤采用单点交叉方式交叉概率设置为0.2如果随机产生的概率小于0.2则该个体可以参与交叉否则复制原数组。for (int i 0; i NP; i) {double cr Math.random();if (cr CR) {int indiv (int) (Math.random() * NP);int index (int) (Math.random() * SIZE);for (int j 0; j SIZE; j) {if (j index) {cr_population[i][j] select_population[indiv][j];} else {cr_population[i][j] select_population[i][j];}}} else {for (int j 0; j SIZE; j) {cr_population[i][j] select_population[i][j];}}}对于变异步骤采用均匀变异变异概率设置为0.2如果随机产生的概率小于0.2则发生变异否则复制原数组。for (int i 0; i NP; i) {for (int j 0; j SIZE; j) {double mu Math.random();if (mu MU) {mu_population[i][j] MIN Math.random() * (MAX - MIN);} else {mu_population[i][j] cr_population[i][j];}}}4终止条件最多迭代200代。四、实验结果与测试测试用例1如图4-1所示。图4-1测试用例1测试用例2如图4-2所示。图4-2测试用例2附录源代码package cn.lh; public class Test { /* * description: 计算适应度及函数式的解 y-a2b-3c4d a,b,c,d ~[-2,2] 最小值 Y-20{a2b-2,c2,d-2} * date: 2023/12/22 17:54 * param: [individual] * return: double **/ public static double function(double[] individual) { //传入个体计算Y值 return -individual[0] 2 * individual[1] - 3 * individual[2] 4 * individual[3]; } public static void main(String[] args) { int NP 10;//种群规模 int SIZE 4; //需要有a、b、c、d,4个参数,所以是4 int maxGen 200;//迭代次数 double CR 0.2;//交叉概率 double MU 0.2;//变异概率 double MAX -2;//a,b,c,d变量的上限 double MIN 2;//a,b,c,d变量的下限 double[][] init_population new double[NP][SIZE];//初始化种群 double[][] select_population new double[NP][SIZE];//选择后的优质种群 double[][] cr_population new double[NP][SIZE];//交叉后种群 double[][] mu_population new double[NP][SIZE];//变异后种群 double[] best_individual new double[SIZE]; //精英个体,及全局最优解 double best_Y Integer.MAX_VALUE; //精英适应度 double[] Y new double[NP]; //对应种群中每个个体的适应度 double[] addSumP new double[NP];//每个个体被选中的概率累加和 //初始化种群 for (int i 0; i NP; i) { for (int j 0; j SIZE; j) { init_population[i][j] MIN Math.random() * (MAX - MIN);//随机生成解 } } //迭代 for (int G 0; G maxGen; G) { //2.选择优质个体,根据轮盘赌的形式挑选个体 double sumY 0; for (int i 0; i NP; i) {//计算每个个体的适应度及全部适应度和 Y[i] function(init_population[i]); sumY Y[i]; } double addSum 0; for (int i 0; i NP; i) {//记录每个个体别选中的累加和 addSum Y[i] / sumY; //当前个体概率 addSumP[i] addSum; //累加和 } //根据累加和概率判断轮盘指针是向那个个体 for (int i 0; i NP; i) { double r Math.random(); int index 0; for (int k 0; k NP; k) { //从头开始遍历累加和数组addSumP找到第一个大于r的元素的下标k那么个体下标index就为k if (r addSumP[k]) { index k; break;//选择了一个个体下标是index } } //将选中的个体复制到select_population数组中 select_population[i] init_population[index]; } //单点交叉 for (int i 0; i NP; i) { double cr Math.random(); if (cr CR) {//判断是否交叉 //与哪个个体交叉随机生成一个个体 int indiv (int) (Math.random() * NP); //从那个位置交叉随机生成一个位置 int index (int) (Math.random() * SIZE); for (int j 0; j SIZE; j) { if (j index) {//发生交叉的片段 cr_population[i][j] select_population[indiv][j]; } else { //不发生交叉的片段 cr_population[i][j] select_population[i][j]; } } } else {//不进行交叉操作直接将当前个体复制到交叉后的种群中 for (int j 0; j SIZE; j) { cr_population[i][j] select_population[i][j]; } } } //均匀变异 for (int i 0; i NP; i) { for (int j 0; j SIZE; j) { double mu Math.random(); if (mu MU) { //判断是否变异 mu_population[i][j] MIN Math.random() * (MAX - MIN); } else { mu_population[i][j] cr_population[i][j]; } } } //输出当前迭代中最优解 for (int i 0; i NP; i) { if (best_Y function(mu_population[i])) { for (int j 0; j SIZE; j) { best_individual[j] mu_population[i][j]; } best_Y function(mu_population[i]); } } System.out.println(第 G 代,最小适应度为: best_Y); //5.保留优质个体 for (int i 0; i NP; i) { //如果当前个体的适应度值比变异后个体的适应度值更高说明当前个体更优秀 if (function(init_population[i]) function(mu_population[i])) { for (int j 0; j SIZE; j) { init_population[i][j] mu_population[i][j]; } } } } //迭代结束 System.out.println(最优解空间为); for (int j 0; j SIZE; j) { System.out.print( best_individual[j]); } } }若觉得有帮助欢迎点赞关注一起成长进步~声明本文仅供学习交流禁作商用禁篡改、歪曲及有偿传播引用需标明来源。侵权必究。