多层感知机模型的简洁实现
2020-04-07 11:54
99 查看
多层感知机模型的简洁实现
# 导入所需包和模块 import torch from torch import nn from torch.nn import init # 初始化模块 import numpy as np import sys sys.path.append("..") import d2lzh_pytorch as d2l
定义模型
# 定义超参数 num_inputs, num_outputs, num_hiddens = 784, 10, 256 net = nn.Sequential(d2l.FlattenLayer(), # 改变X的形状 nn.Linear(num_inputs, num_hiddens), nn.ReLU(), # 使用relu激活函数 nn.Linear(num_hiddens, num_outputs), ) for params in net.parameters(): init.normal_(params, mean=0, std=0.01) # 初始化参数
读取数据,训练模型
batch_size = 256 # 设定批量大小为256 train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size) loss = torch.nn.CrossEntropyLoss() optimizer = torch.optim.SGD(net.parameters(), lr = 0.5) num_epochs = 5 # 迭代周期为5 d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size,None, None, optimizer)
相关文章推荐
- TensorFlow实现MLP多层感知机模型
- TensorFlow上实现MLP多层感知机模型
- 机器学习算法之感知机模型算法原理及实现
- 基于tensortflow的多层感知机模型
- python 实现 Peceptron Learning Algorithm ( 二) 感知机模型实现
- python 实现 Peceptron Learning Algorithm ( 三) 感知机模型应用于Iris数据集
- 基于tensorflow使用全连接层函数实现多层神经网络并保存和读取模型
- Keras 多层感知机 多类别的 softmax 分类模型代码
- 《统计学习方法》笔记之感知机模型及代码实现
- TensorFlow实现多层感知机及可视化训练过程中的数据记录
- Task1:线性回归;Softmax与分类模型;多层感知机
- 《动手学习深度学习》之一:线性回归、softmax与分类模型、多层感知机-伯禹课程-打卡1
- 【深度学习】多层感知机及其实现(pyTorch)
- python 实现 Peceptron Learning Algorithm ( 二) 感知机模型实现
- python 实现 Peceptron Learning Algorithm ( 三) 感知机模型应用于Iris数据集
- 深度学习:多层感知机MLP数字识别的代码实现
- 《动手学深度学习》笔记 Task01:线性回归;Softmax与分类模型、多层感知机
- softmax与分类模型;多层感知机。文本预处理;语言模型;循环神经网络基础
- 81、Tensorflow实现LeNet-5模型,多层卷积层,识别mnist数据集
- Tensorflow实战(黄文坚)——多层感知机代码实现