Python深度学习pytorch神经网络多层感知机简洁实现
作者:Supre_yuan 时间:2021-08-20 17:37:43
我们可以通过高级API更简洁地实现多层感知机。
import torch
from torch import nn
from d2l import torch as d2l
模型
与softmax回归的简洁实现相比,唯一的区别是我们添加了2个全连接层。第一层是隐藏层,它包含256个隐藏单元,并使用了ReLU激活函数。第二层是输出层。
net = nn.Sequential(nn.Flatten(),
nn.Linear(784, 256),
nn.ReLU(),
nn.Linear(256, 10))
def init_weights(m):
if type(m) == nn.Linear:
nn.init.normal_(m.weight, std=0.01)
net.apply(init_weights)
训练过程的实现与我们实现softmax回归时完全相同,这种模块化设计使我们能够将与和模型架构有关的内容独立出来。
batch_size, lr, num_epochs = 256, 0.1, 10 # 批量大小为256,学习率为0.1,类型为10
loss = nn.CrossEntropyLoss() # 使用交叉熵损失函数
trainer = torch.optim.SGD(net.parameters(), lr=lr) # 开始训练
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, trainer)
以上就是Python深度学习pytorch神经网络多层感知机简洁实现的详细内容,更多关于pytorch神经网络的资料请关注脚本之家其它相关文章!
来源:https://blog.csdn.net/weixin_43880225/article/details/120614502
标签:pytorch,神经网络,感知机
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
在Python 的线程中运行协程的方法
2021-03-10 18:06:38
![](https://img.aspxhome.com/file/2023/9/85289_0s.png)
Python通过RabbitMQ服务器实现交换机功能的实例教程
2023-08-24 01:15:19
![](https://img.aspxhome.com/file/2023/8/80658_0s.png)
Python实现PING命令的示例代码
2022-01-10 09:19:15
![](https://img.aspxhome.com/file/2023/4/89114_0s.png)
python程序 线程队列queue使用方法解析
2021-11-16 18:43:02
python 详解如何写flask文件下载接口
2023-05-04 21:07:06
![](https://img.aspxhome.com/file/2023/9/68929_0s.png)
Python只用40行代码编写的计算器实例
2023-07-30 07:16:40
![](https://img.aspxhome.com/file/2023/4/61224_0s.jpg)
关于php unset对json_encode的影响详解
2023-07-06 08:33:52
![](https://img.aspxhome.com/file/2023/6/55356_0s.png)
python编写朴素贝叶斯用于文本分类
2022-01-29 18:45:51
![](https://img.aspxhome.com/file/2023/3/98323_0s.jpg)
python实现将range()函数生成的数字存储在一个列表中
2021-12-19 08:01:51
![](https://img.aspxhome.com/file/2023/8/93498_0s.gif)
联动选择菜单(二级联动菜单 三 级联动菜单)
2023-06-26 22:37:55
python读取yaml文件后修改写入本地实例
2021-12-20 02:53:54
IE下修改<p>标签的innerHTML出错
2007-11-11 10:12:00
Django初步使用Celery处理耗时任务和定时任务问题
2023-10-15 07:41:59
![](https://img.aspxhome.com/file/2023/0/88010_0s.png)
如何设计广告的用户体验?
2007-12-20 13:12:00
![](https://img.aspxhome.com/file/UploadPic/200712/20/20071220131356270s.jpg)
python数据结构算法分析
2022-06-11 02:57:15
![](https://img.aspxhome.com/file/2023/8/72718_0s.png)
Python学习笔记之图片人脸检测识别实例教程
2021-12-03 06:17:28
![](https://img.aspxhome.com/file/2023/8/80438_0s.jpg)
如何使用VSCode愉快的写Python于调试配置步骤
2021-11-21 06:16:12
![](https://img.aspxhome.com/file/2023/4/104894_0s.png)
python3 tkinter实现点击一个按钮跳出另一个窗口的方法
2023-10-27 01:19:11
![](https://img.aspxhome.com/file/2023/2/102342_0s.jpg)
如何使用Python实现名片管理系统
2022-11-24 03:13:09
python实现TF-IDF算法解析
2021-06-02 03:27:51
![](https://img.aspxhome.com/file/2023/1/82961_0s.jpg)