动手学深度学习11- 多层感知机pytorch简洁实现

多层感知机的简洁实现
import  torch
from torch import nn
from torch.nn import init
import sys
import numpy as np
sys.path.append('..')
import d2lzh_pytorch as d2l
定义模型
num_inputs,num_outputs,num_hidden =784,10,256
net = nn.Sequential(d2l.FlattenLayer(),
                   nn.Linear(num_inputs,num_hidden),
                    nn.ReLU(),
                    nn.Linear(num_hidden,num_outputs),
                   )
for params in net.parameters():
    init.normal_(params,mean=0,std=0.01)
    
print(net.parameters)
<bound method Module.parameters of Sequential(
  (0): FlattenLayer()
  (1): Linear(in_features=784, out_features=256, bias=True)
  (2): ReLU()
  (3): Linear(in_features=256, out_features=10, bias=True)
)>
读取数据
batch_size= 256
train_iter,test_iter = d2l.get_fahsion_mnist(batch_size)
损失函数
loss = nn.CrossEntropyLoss()
定义优化算法
optimizer = torch.optim.SGD(net.parameters(),lr=0.5)
num_epochs =5
训练数据并验证测试集
d2l.train_ch3(net,train_iter,test_iter,loss,num_epochs,batch_size,None,None,optimizer)
epoch 1, loss 0.0031, train acc 0.707, test acc 0.810
epoch 2, loss 0.0019, train acc 0.821, test acc 0.810
epoch 3, loss 0.0017, train acc 0.843, test acc 0.835
epoch 4, loss 0.0015, train acc 0.858, test acc 0.840
epoch 5, loss 0.0014, train acc 0.865, test acc 0.853
小结
  • 通过pytorch可以使用Sequential这样的写法简洁的实现多层感知机
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值