Pytorch 实现多层网络

Pytorch 实现多层网络

参考学习花书多层感知机的简洁实现

import torch
from torch import nn
from torch.nn import init
import numpy as np
import sys
sys.path.append("..") 
import d2lzh_pytorch as d2l
# 1 定义模型
num_inputs, num_outputs, num_hiddens = 784, 10, 256 #输入个数为784(Fashion-MNIST数据集中图像),输出个数为10,隐藏单元个数为256。
    
net = nn.Sequential(
        d2l.FlattenLayer(),
        nn.Linear(num_inputs, num_hiddens),
        nn.ReLU(),
        nn.Linear(num_hiddens, num_outputs), 
        )
    
for params in net.parameters():
    init.normal_(params, mean=0, std=0.01)

# 2 读取数据并训练模型
batch_size = 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size) #使用Fashion-MNIST数据集
loss = torch.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)
  • output:
epoch 1, loss 0.0020, train acc 0.850, test acc 0.935
epoch 2, loss 0.0008, train acc 0.942, test acc 0.943
epoch 3, loss 0.0006, train acc 0.958, test acc 0.961
epoch 4, loss 0.0004, train acc 0.967, test acc 0.969
epoch 5, loss 0.0004, train acc 0.974, test acc 0.968
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值