import torch
from torch import nn
from d2l import torch as d2l
# 超参数
batch_size = 256
# 数据
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
# 模型
# pytorch不会调整input的形状
# 定义展平层(flatten)保留第0维,展开其他维度为1个向量,保留样本数,展开28*28=784,先展平再输入
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中
net.apply(init_weights)
Sequential(
(0): Flatten(start_dim=1, end_dim=-1)
(1): Linear(in_features=784, out_features=10, bias=True)
)
# 损失函数
loss = nn.CrossEntropyLoss()
# 优化算法 以一定的学习率去学习 更新参数
trainer = torch.optim.SGD(net.parameters(), lr=0.1)
# 开始训练
num_epochs = 10
# todo(训练的结果曲线没有 train loss)
d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, trainer)

query
softmax中含有指数exp
softlabel应用于为了解决softmax的问题
交叉熵,相对熵
类别不平衡
似然函数,最大似然估计?todo
排序回归(ranking)可以使用softmax吗
方差参数初始化
batch_size
net.eval():模型设置为评估模式
测试精度先上升再下降是过拟合?