<jupyter_start><jupyter_text>Softmax回归 pytorch实现> 本部分代码看起来有些复杂，但只需要掌握最核心的代码即可>> 我们借助了动手学习深度学习课程的[d2l库](https://github.com/d2l-ai/d2l-zh)，来导入数据集，对此表示感谢。<jupyter_code>import torch
from torch import nn
from d2l import torch as d2l
from utils import train
# 加入数据集
batch_size = 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
# Pytorch不会隐式的调整输入的形状

# 我们定义了展平层flatten
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)  # 初始化超参数

loss = nn.CrossEntropyLoss()  # 交叉熵损失

trainer = torch.optim.SGD(net.parameters(), lr=0.1)  # 优化器
# 训练
if __name__ == '__main__':
    num_epochs = 10
    train(net, train_iter, test_iter, loss, num_epochs, trainer)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12577065351418001, 'fraction_numerical': 0.025893958076448828, 'mean_word_length': 5.548387096774194, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '40050', 'license': 'Apache-2.0', 'n_tokens_mistral': 417, 'n_tokens_neox': 431, 'n_words': 59, 'path': 'Part1 Machine Learning Basics/2 LogisticRegression&Softmax/softmax_pytorch_version.ipynb', 'repo_name': 'Gary-code/MachineLearning', 'size': '811'}