1 Star 0 Fork 0

郭鸿凯/AI_DataProcess_Study

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
softmax_plus.py 923 Bytes
一键复制 编辑 原始数据 按行查看 历史
郭鸿凯 提交于 2023-04-11 10:56 . good
import torch
from d2l import torch as d2l
from torch import nn
from softmax_normal import train_ch3
batch_size = 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
# softmax回归的输出层是一个全连接层
# PyTorch不会隐式地调整输入形状
# 因此,定义展平层()在线性层前调整网络输入的形状
net = nn.Sequential(nn.Flatten(), nn.Linear(784, 10))
def init_weights(m):
""""""
if type(m) == nn.Linear:
nn.init.normal_(m.weight, std=0.01)
net.apply(init_weights)
# 在交叉熵损失函数中传递未归一化的预测,并同时计算softmax及其对数
loss = nn.CrossEntropyLoss()
# 使用学习率为0.1的小批量随机梯度下降作为优化算法
trainer = torch.optim.SGD(net.parameters(), lr=0.1)
# 调用之前定义的函数来训练模型
num_epochs = 10
d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, trainer)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/guo-honkai/AI_DataProcess_Study.git
git@gitee.com:guo-honkai/AI_DataProcess_Study.git
guo-honkai
AI_DataProcess_Study
AI_DataProcess_Study
master

搜索帮助