4.2.0 头文件

import torch
from torch import nn
from d2l import torch as d2l
from matplotlib import pyplot as plt
# 设置批量大小、学习率、训练轮数
batch_size, lr, num_epochs = 256, 0.1, 10

 

4.2.1 下载fashion_mnist数据集

# 下载fashion_mnist,并对数据集进行打乱和按批量大小进行切割的操作,得到可迭代的训练集和测试集(训练集和测试集的形式都为(特征数据集合,数字标签集合))
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)

 

4.2.2 网络模型

net = nn.Sequential(nn.Flatten(),               #展平层
                    nn.Linear(784, 256),        #全连接层1
                    nn.ReLU(),                  #激活函数层
                    nn.Linear(256, 10))         #全连接层2

 

4.2.3 初始化模型参数

# 初始化所有全连接层的权重
def init_weights(m):
    if type(m) == nn.Linear:
        nn.init.normal_(m.weight, std=0.01)

net.apply(init_weights);

 

4.2.4 损失函数

# 定义交叉熵损失函数,得到的结果为每个样本的损失
loss = nn.CrossEntropyLoss(reduction='none')

 

4.2.4 优化器

# 定义小批量梯度下降优化器
trainer = torch.optim.SGD(net.parameters(), lr=lr)

 

4.2.6 训练过程

# 开始训练
d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, trainer)

 

4.2.7 训练结果可视化

plt.savefig('Output.png')

 

 

 

本小节完整代码如下所示

import torch
from torch import nn
from d2l import torch as d2l
from matplotlib import pyplot as plt
# 设置批量大小、学习率、训练轮数
batch_size, lr, num_epochs = 256, 0.1, 10

# ------------------------------下载fashion_mnist数据集------------------------------------

# 下载fashion_mnist,并对数据集进行打乱和按批量大小进行切割的操作,得到可迭代的训练集和测试集(训练集和测试集的形式都为(特征数据集合,数字标签集合))
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)

# ------------------------------网络模型------------------------------------

net = nn.Sequential(nn.Flatten(),               #展平层
                    nn.Linear(784, 256),        #全连接层1
                    nn.ReLU(),                  #激活函数层
                    nn.Linear(256, 10))         #全连接层2

# ------------------------------初始化模型参数------------------------------------

# 初始化所有全连接层的权重
def init_weights(m):
    if type(m) == nn.Linear:
        nn.init.normal_(m.weight, std=0.01)

net.apply(init_weights);

# ------------------------------损失函数------------------------------------

# 定义交叉熵损失函数,得到的结果为每个样本的损失
loss = nn.CrossEntropyLoss(reduction='none')

# ------------------------------优化器------------------------------------

# 定义小批量梯度下降优化器
trainer = torch.optim.SGD(net.parameters(), lr=lr)

# ------------------------------训练过程------------------------------------

# 开始训练
d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, trainer)

# ------------------------------训练结果可视化------------------------------------

plt.savefig('Output.png')

 

posted on 2022-11-04 00:10  yc-limitless  阅读(35)  评论(0编辑  收藏  举报