学习笔记:动手学深度学习 13 softmax回归的简洁实现

from torch import nn
from d2l import torch as d2l
batch_size = 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
Backend Qt5Agg is interactive backend. Turning interactive mode on.
""""拿进数据"""
Out[3]: '"拿进数据'
"""初始化模型参数""" 
# PyTorch不会隐式地调整输入的形状。因此,
# 我们在线性层前定义了展平层(flatten),来调整网络输入的形状
net = nn.Sequential(nn.Flatten(), nn.Linear(784, 10))#28*28
def init_weights(m):
    if type(m) == nn.Linear:
        nn.init.normal_(m.weight, std=0.01)
"""如果是线性网络,以均值0和标准差0.01随机初始化权重"""
net.apply(init_weights);
"""在交叉熵损失函数中传递未归一化的预测,并同时计算softmax及其对数"""
Out[5]: '在交叉熵损失函数中传递未归一化的预测,并同时计算softmax及其对数'
loss = nn.CrossEntropyLoss()
"""优化算法--学习率为0.1的小批量随机梯度下降作为优化算法"""
Out[7]: '优化算法--学习率为0.1的小批量随机梯度下降作为优化算法'
trainer = torch.optim.SGD(net.parameters(), lr=0.1)
"""训练"""
Out[9]: '训练'
num_epochs = 10
d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, trainer)
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>
<Figure size 350x250 with 1 Axes>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值