首页 > 其他分享 >《动手学深度学习 Pytorch版》 4.3 多层感知机的简洁实现

《动手学深度学习 Pytorch版》 4.3 多层感知机的简洁实现

时间:2023-09-01 19:55:26浏览次数:48  
标签:4.3 nn iter 感知机 Pytorch train lr 256 Linear

import torch
from torch import nn
from d2l import torch as d2l

模型

net = nn.Sequential(nn.Flatten(),
                    nn.Linear(784, 256),
                    nn.ReLU(),  # 与 3.7 节相比多了一层
                    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)
Sequential(
  (0): Flatten(start_dim=1, end_dim=-1)
  (1): Linear(in_features=784, out_features=256, bias=True)
  (2): ReLU()
  (3): Linear(in_features=256, out_features=10, bias=True)
)
batch_size, lr, num_epochs = 256, 0.1, 10
loss = nn.CrossEntropyLoss(reduction='none')
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)

image

练习

(1)尝试添加不同数量的隐藏层(也可以修改学习率),怎样设置效果最好?

net2 = nn.Sequential(nn.Flatten(),
                    nn.Linear(784, 256),
                    nn.ReLU(),
                    nn.Linear(256, 128),
                    nn.ReLU(),
                    nn.Linear(128, 10))

def init_weights(m):
    if type(m) == nn.Linear:  # 使用正态分布中的随机值初始化权重
        nn.init.normal_(m.weight, std=0.01)

net2.apply(init_weights)

batch_size2, lr2, num_epochs2 = 256, 0.3, 10
loss2 = nn.CrossEntropyLoss(reduction='none')
trainer2 = torch.optim.SGD(net2.parameters(), lr=lr2)

train_iter2, test_iter2 = d2l.load_data_fashion_mnist(batch_size2)
d2l.train_ch3(net2, train_iter2, test_iter2, loss2, num_epochs2, trainer2)

image


(2)尝试不同的激活函数,哪个激活函数效果最好?

net3 = nn.Sequential(nn.Flatten(),
                    nn.Linear(784, 256),
                    nn.Sigmoid(),
                    nn.Linear(256, 10))

net4 = nn.Sequential(nn.Flatten(),
                    nn.Linear(784, 256),
                    nn.Tanh(),
                    nn.Linear(256, 10))

def init_weights(m):
    if type(m) == nn.Linear:
        nn.init.normal_(m.weight, std=0.01)

net3.apply(init_weights)
net4.apply(init_weights)


train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
batch_size, lr, num_epochs = 256, 0.1, 10
loss = nn.CrossEntropyLoss(reduction='none')
trainer = torch.optim.SGD(net3.parameters(), lr=lr)
d2l.train_ch3(net3, train_iter, test_iter, loss, num_epochs, trainer)
---------------------------------------------------------------------------

AssertionError                            Traceback (most recent call last)

Cell In[5], line 4
      2 loss = nn.CrossEntropyLoss(reduction='none')
      3 trainer = torch.optim.SGD(net3.parameters(), lr=lr)
----> 4 d2l.train_ch3(net3, train_iter, test_iter, loss, num_epochs, trainer)


File c:\Software\Miniconda3\envs\d2l\lib\site-packages\d2l\torch.py:340, in train_ch3(net, train_iter, test_iter, loss, num_epochs, updater)
    338     animator.add(epoch + 1, train_metrics + (test_acc,))
    339 train_loss, train_acc = train_metrics
--> 340 assert train_loss < 0.5, train_loss
    341 assert train_acc <= 1 and train_acc > 0.7, train_acc
    342 assert test_acc <= 1 and test_acc > 0.7, test_acc


AssertionError: 0.5017133202234904

image

batch_size, lr, num_epochs = 256, 0.1, 10
loss = nn.CrossEntropyLoss(reduction='none')
trainer = torch.optim.SGD(net4.parameters(), lr=lr)
d2l.train_ch3(net4, train_iter, test_iter, loss, num_epochs, trainer)

image

还是 ReLU 比较奈斯。


(3)尝试不同的方案来初始化权重,什么方案效果最好。

累了,不想试试了。略......

标签:4.3,nn,iter,感知机,Pytorch,train,lr,256,Linear
From: https://www.cnblogs.com/AncilunKiang/p/17672748.html

相关文章

  • Lnton羚通算法算力云平台【PyTorch】教程:torch.nn.Mish
    torch.nn.Mish是PyTorch中的一个激活函数类,它实现了Mish激活函数。Mish是一种近年来提出的激活函数,它在激活函数的设计中引入了自适应斜率。Mish函数的定义如下:Mish(x)=x*tanh(softplus(x))其中softplus(x)是软正值函数,定义为softplus(x)=log(1+exp(x))。Mish函......
  • 《动手学深度学习 Pytorch版》 4.2 多层感知机的从零开始实现
    importtorchfromtorchimportnnfromd2limporttorchasd2l#经典数据集与batchsizebatch_size=256train_iter,test_iter=d2l.load_data_fashion_mnist(batch_size)4.2.1初始化模型为什么不直接使用Tensor而是用nn.Parameter函数将其转换为parameter呢?......
  • PyTorch多卡分布式训练DDP单机多卡
    前言因为课题组发的卡还没有下来,先向导师问了实验室的两张卡借用。之前都是单卡训练模型,正好在这个机会实践以下单机多卡训练模型的方法。关于DDP网上有很多资料,但都比较零碎(有些博客的代码甚至没办法run),Pytorch给出的官方文档看起来也比较吃力。因此这篇文章的主要目的是......
  • Pytorch环境搭建
     https://pytorch.org/ https://blog.csdn.net/weixin_43737866/article/details/127784768https://www.jianshu.com/p/4c7b9127cf83https://blog.csdn.net/m0_56945481/article/details/126998629第一次装Anaconda3最新版,报Solvingenvironment:failedwithinitialfro......
  • Lnton羚通视频分析算法平台【PyTorch】教程:torch.nn.maxpool2d
    torch.nn.MaxPool2d是PyTorch中的一个二维最大池化层。它用于在神经网络中执行最大池化操作,以减少特征图的空间尺寸并提取出主要特征。torch.nn.MaxPool2d的常用语法如下:torch.nn.MaxPool2d(kernel_size,stride=None,padding=0,dilation=1,return_indices=False,ceil_mode......
  • 【pytorch】从零开始,利用yolov5、crnn+ctc进行车牌识别
    笔者的运行环境:python3.8+pytorch2.0.1+pycharm+kaggle用到的网络框架:yolov5、crnn+ctc项目地址:GitHub-WangPengxing/plate_identification:利用yolov5、crnn+ctc进行车牌识别1.写在开始之前在学习过目标检测和字符识别后想用yolov5、crnn+ctc做一个车牌识别项目,本意是参......
  • [note] pytorch的几种维度操作方式比对
    pre今天看代码在想torch.unbind+torch.cat与torch.reshape的区别,直观上来看reshape似乎更便利。chatgpt问题xisatensorofthreedimension,whatisthedifferencebetweentorch.cat(torch.unbind(x,dim=1),dim=0)andtorch.reshape(x,(x.shape[0]*x.shape[1]......
  • Lnton羚通视频算法算力云平台【PyTorch】教程:torch.nn.ELU
    在PyTorch中,torch.nn.ELU代表指数线性单元(ExponentialLinearUnit),是一种激活函数。ELU函数可以用来增加神经网络的非线性表达能力,使其具备更强的适应性。ELU函数的定义如下:elu(x)=xifx>=0alpha*(exp(x)-1)ifx<0其中,x是输入,alpha是一个正数超参数,控制ELU......
  • pytorch nn.LSTM模块参数详解
    nn.LSTM模块参数input_size:输入的维度hidden_size:h的维度num_layers:堆叠LSTM的层数,默认值为1bias:偏置,默认值:Truebatch_first:如果是True,则input为(batch,seq,input_size)。默认值为:False(seq_len,batch,input_size)bidirectional:是否双向传播,默认值为False 输入(in......
  • 带你上手基于Pytorch和Transformers的中文NLP训练框架
    本文分享自华为云社区《全套解决方案:基于pytorch、transformers的中文NLP训练框架,支持大模型训练和文本生成,快速上手,海量训练数据》,作者:汀丶。1.简介目标:基于pytorch、transformers做中文领域的nlp开箱即用的训练框架,提供全套的训练、微调模型(包括大模型、文本转向量、文本生......