当前位置:网站首页>李沐d2l(五)---多层感知机
李沐d2l(五)---多层感知机
2022-07-26 08:57:00 【madkeyboard】
一、感知机
概念
给定输入x,权重w,和偏移b,感知机输出如下。感知机的输出是一个二分类的问题,而线性回归输出的是一个实数,Softmax如果有n个类就会输出n个元素,就是一个多分类问题。
感知机不能拟合XOR问题,因为它只能产生线性分割面
训练感知机
它的求解算法等价于使用批量大小为1的梯度下降。
收敛定理
二、多层感知机
为什么叫多层?当我们要完成的目标一次不能完成,就先学一个简单的函数,再学一个简单函数,最后再用另一个函数来结合这两个函数。
单隐藏层-单分类
输入是n维的向量,隐藏层是m x n的矩阵,偏移是长位m的向量。输出层也是长位m的向量,偏移是一个标量。
为什么需要一个非线性的激活函数?假设激活函数是本身,即σ(x) = x,可以看到输出o 的 w2T W1 x + b’仍是一个线性函数,那么就等价于一个单层的感知机。
Sigmoid激活函数
Tanh激活函数
ReLU激活函数
多层分类
输入和隐藏层跟单分类一样,区别在于输出层是一个m x k 的矩阵,偏移是一个长为k的向量。
三、多层感知机代码从零开始
import torch
from torch import nn
from d2l import torch as d2l
import os
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
batch_size = 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
# 1 实现一个具有单隐藏层的多层感知机,包含256个隐藏单元
num_inputs, num_outputs = 784, 10
num_hiddens = 256
w1 = nn.Parameter(torch.randn(num_inputs, num_hiddens, requires_grad=True) * 0.01) # randn是正态分布在(0,1)之所以乘以0.01是想把范围限制到(0,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]
# 2 实现RELU激活函数
def relu(X):
a = torch.zeros_like(X)
return torch.max(X, a)
# 3 实现模型
def net(X):
X = X.reshape((-1, num_inputs))
H = relu(X @ w1 + b1) # @矩阵乘法的简写
return (H @ w2 + b2)
# 4 损失
loss = nn.CrossEntropyLoss(reduction='none')
# 5 训练过程
num_epochs, lr = 10, 0.1
updater = torch.optim.SGD(params, lr=lr)
d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, updater)
d2l.plt.show()
从结果可以看到与上次sofmax相比,它的loss降低了,但是精度却没有提高。
四、简单实现
import torch
from torch import nn
from d2l import torch as d2l
net = nn.Sequential(nn.Flatten(), nn.Linear(784, 256), nn.ReLU(),
nn.Linear(256, 10))
def init_weights(m):
if type(m) == nn.Linear:
nn.init.normal_(m.weight, std=0.01)
net.apply(init_weights);
batch_size, lr, num_epochs = 256, 0.1, 10
loss = nn.CrossEntropyLoss()
trainer = torch.optim.SGD(net.parameters(), lr=lr)
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, trainer)
边栏推荐
- 2022茶艺师(中级)特种作业证考试题库模拟考试平台操作
- Study notes of automatic control principle -- correction and synthesis of automatic control system
- 数据库操作 题目一
- Form form
- node-v下载与应用、ES6模块导入与导出
- Deploy prometheus+grafana monitoring platform
- 对标注文件夹进行清洗
- Implementation of Prometheus web authentication and alarm
- Dynamic SQL and exceptions of pl/sql
- 论文笔记: 知识图谱 KGAT (未完暂存)
猜你喜欢
对标注文件夹进行清洗
day06 作业--技能题1
Flask project learning (I) -- sayhello
sklearn 机器学习基础(线性回归、欠拟合、过拟合、岭回归、模型加载保存)
Set of pl/sql
Day06 homework -- skill question 1
Vision Group Training Day5 - machine learning, image recognition project
[eslint] Failed to load parser ‘@typescript-eslint/parser‘ declared in ‘package. json » eslint-confi
Study notes of automatic control principle -- dynamic model of feedback control system
Form form
随机推荐
Study notes of automatic control principle -- dynamic model of feedback control system
2022流动式起重机司机考试题模拟考试题库模拟考试平台操作
Cat安装和使用
P3743 Kotori's equipment
[leetcode database 1050] actors and directors who have cooperated at least three times (simple question)
node-v下载与应用、ES6模块导入与导出
The idea shortcut key ALT realizes the whole column operation
力扣——二叉树剪枝
【数据库 】GBase 8a MPP Cluster V95 安装和卸载
PAT 甲级 A1034 Head of a Gang
TypeScript版Snowflake主键生成器
谷粒学院的全部学习源码
Set of pl/sql
Pytoch realizes logistic regression
General file upload vulnerability getshell of a digital campus system (penetration test -0day)
mysql函数
聪明的美食家 C语言
Ueditot_ JSP SSRF vulnerability recurrence
node的js文件引入
Zipkin安装和使用