赞
踩
我们可以通过在⽹络中加⼊⼀个或多个隐藏层来克服线性模型的限制,使其能处理更普遍的函数关系类型。要做到这⼀点,最简单的⽅法是将许多全连接层堆叠在⼀起。每⼀层都输出到上⾯的层,直到⽣成最后的输出。我们可以把前L−1层看作表⽰,把最后⼀层看作线性预测器。这种架构通常称为多层感知机(multilayerperceptron),通常缩写为MLP。
# -*- coding: utf-8 -*- # @Project: zc # @Author: zc # @File name: MLP-FROM-SCRATCH # @Create time: 2021/11/25 13:20 # 1.导入数据库 import torch from torch import nn from d2l import torch as d2l import matplotlib.pyplot as plt # 2.导入数据,输出迭代器 train_iter训练迭代器, test_iter测试迭代器 batch_size = 256 train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size=batch_size) # 3.多层感知机 MLP num_inputs, num_outputs, num_hiddens = 784, 10, 256 # 4. 多层感知机参数初始化,requires_grad=True 可以使得参数能更新 w1 = nn.Parameter(torch.randn( num_inputs, num_hiddens, requires_grad=True) * 0.01) b1 = nn.Parameter(torch.zeros( num_hiddens, requires_grad=True)) w2 = nn.Parameter(torch.randn( num_hiddens, num_outputs, requires_grad=True) * 0.01) b2 = nn.Parameter(torch.zeros( num_outputs, requires_grad=True)) params = [w1, b1, w2, b2] # 5. 定义激活函数 relu def relu(x): a = torch.zeros_like(x) return torch.max(x, a) # 6. 定义模型 MLP,以我们使⽤reshape将每个⼆维图像转换为⼀个⻓度为num_inputs的向量 def net(x): x = x.reshape((-1, num_inputs)) H = relu(x @ w1 + b1) return (H @ w2 + b2) # 7.定义损失函数 loss = nn.CrossEntropyLoss() # 8.定义超参数 num_epochs, lr = 10, 0.1 # 9.定义更新器,随机梯度下降 updater = torch.optim.SGD(params, lr) # 10.训练模型 d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, updater) # 11.预测模型 d2l.predict_ch3(net,test_iter) # 12.显示结果 plt.show()
训练
预测
# -*- coding: utf-8 -*- # @Project: zc # @Author: ZhangChu # @File name: MLP-brief # @Create time: 2021/11/25 15:08 # 1.导入数据库 import torch from torch import nn from d2l import torch as d2l import matplotlib.pyplot as plt # 2.导入数据,输出迭代器 train_iter训练迭代器, test_iter测试迭代器 batch_size = 256 train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size) # 3.多层感知机 MLP,调用 nn,Relu作为激活函数 net = nn.Sequential( nn.Flatten(), nn.Linear(784, 256), nn.ReLU(), nn.Linear(256, 10) ) # 4.初始化模型参数 def init_weights(m): if type(m) == nn.Linear: nn.init.normal_(m.weight, std=0.01) net.apply(init_weights) # 5.定义损失函数 loss = nn.CrossEntropyLoss() # 6.定义更新器,随机梯度下降 SGD,设置超参数 学习率 lr=0.1,训练次数 num_epochs=10 trainer = torch.optim.SGD(net.parameters(), lr=0.1) num_epochs = 10 # 7. 开始训练模型 d2l.train_ch3(net,train_iter,test_iter,loss,num_epochs,updater=trainer) # 8. 开始模型预测 d2l.predict_ch3(net,test_iter) # 9.显示相关结果 plt.show()
训练
预测
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。