news 2026/4/9 6:59:51

Day 37 MLP神经网络的训练

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Day 37 MLP神经网络的训练

@浙大疏锦行

1.cuda 的检查

import torch torch.cuda if torch.cuda.is_available(): print("CUDA可用!") device_count = torch.cuda.device_count() print(f"可用的CUDA设备数量:{device_count}") current_device = torch.cuda.current_device() print(f"当前使用的CUDA设备索引:{current_device}") device_name = torch.cuda.get_device_name(current_device) print(f"当前CUDA设备的名称:{device_name}") cuda_version = torch.version.cuda print(f"CUDA版本:{cuda_version}") else: print("CUDA不可用。")

2.简单神经网络的流程

a.数据预处理

from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split import numpy as np iris = load_iris() X = iris.data y = iris.target X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=42) print(X_train.shape) print(y_train.shape) print(X_test.shape) print(y_test.shape) from sklearn.preprocessiong import MinMaxScaler scaler = MinMaxScaler() X_train = scaler.fit_transform(X_train) X_test = scaler.transform(X_test) X_train = torch.FloatTensor(X_train) y_train = torch.LongTensor(y_train) X_test = torch.FloatTensor(X_test) y_test = torch.LongTensor(y_test)

b.模型的定义

i.继承nn.Module类

ii.定义每一个层

iii.定义前向传播流程

import torch import torch.nn as nn import torch.optim as optim class MLP(nn.Module): def__init__(self): super(MLP,self).__init__() self.fc1 = nn.Linear(4,10) self.relu = nn.ReLU() self.fc2 = nn.Linear(10,3) def forward(self,x): out = self.fc1(x) out = self.relu(out) out = self.fc2(out) return out model = MLP()

c.定义损失函数和优化器

criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(model.parameters(), 1r=0.01)

d.定义训练流程

num_epochs = 20000 losses = [] for epoch in range(num_epochs): outputs = model.forward(X_train) loss = criterison(outputs,y_train) optimizer.zero_grad() loss.backward() optimizer.step() losses.append(loss.item()) if(epoch + 1) % 100 == 0: print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f})

e.可视化loss过程

import matplotlib.pyplot as plt plt.plot(range(num_epochs),losses) plt.xlabel('Epoch') plt.ylabel('Loss') plt.title('Training Loss over Epochs') plt.show()
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/31 17:57:29

Flutter 测试驱动开发的基本流程

欢迎大家加入开源鸿蒙跨平台开发者社区,一起共建开源鸿蒙跨平台生态。### Flutter 测试驱动开发(TDD)实践指南 测试驱动开发(TDD)是一种软件开发方法,强调在编写功能代码之前先编写测试用例。通过这种方式…

作者头像 李华
网站建设 2026/4/7 9:16:38

[UUCTF 2022 新生赛]ezpop

1.打开先看代码<?php //flag in flag.php error_reporting(0); class UUCTF{public $name;public $key;public $basedata;public $ob;function __construct($str){$this->name$str;}function __wakeup(){if($this->key"UUCTF"){$this->obunserialize(ba…

作者头像 李华
网站建设 2026/3/30 8:31:32

盛水最多的容器(滑动窗口 双指针)

这道题当然可以暴力求解&#xff0c;O(N^2),但是有时候并不会通过&#xff0c;因此要想一个时间复杂度为O&#xff08;N&#xff09;的方法。如果说用滑动窗口肯定会有人会有疑问&#xff0c;这怎么用&#xff1f;下面直接说解法&#xff1a;首先left与right分别指向数组的两边…

作者头像 李华
网站建设 2026/4/7 6:19:55

深度探究Span:.NET内存布局与零拷贝原理及实践

深度探究Span&#xff1a;.NET内存布局与零拷贝原理及实践 在.NET开发中&#xff0c;高效的内存管理至关重要&#xff0c;尤其在处理高性能、低延迟的应用场景时。Span<T> 类型应运而生&#xff0c;它为开发者提供了一种灵活且高效的内存操作方式&#xff0c;能够显著提升…

作者头像 李华
网站建设 2026/4/7 17:50:09

helm 部署 elasticsearch 栈

说明:本文使用的 chart 仓库名字为 elastic/cloud-on-k8s 地址为 elastic/cloud-on-k8s。 1、添加 repo 源 helm repo add elastic https://helm.elastic.co helm repo update2、安装 eck-operator ⚠️说明:ECK Operator(Elastic Cloud on Kubernetes)本身安装的东西其…

作者头像 李华
网站建设 2026/3/20 10:26:15

Qt定时器线程使用全解析

1.Qt定时器线程使用全解析在Qt框架中&#xff0c;定时器&#xff08;如QTimer&#xff09;的“使用线程”特指其所属线程&#xff08;即创建该定时器的线程&#xff09;必须是由QThread启动并管理、且运行了事件循环&#xff08;exec()&#xff09;的线程。这一规则涉及线程的创…

作者头像 李华