多层感知机(MLP,Multilayer Perceptron)也叫人工神经网络(ANN,Artificial Neural Network),除了输入输出层,它中间可以有多个隐层,最简单的MLP只含一个隐层,即三层的结构,如下图最简单的MLP:
上图模型pyTorch代码:
import torch
from torch.nn import functional as F
x = torch.randn(1, 10) # 输入x的特征有10个
w = torch.randn(3, 10, requires_grad=True) # 一个隐藏层,节点个数为3
b = torch.randn(1, 3, requires_grad=True)
# print(w)
pre = torch.sigmoid(x @ w.t() + b) # 预测值
# pre
trage = torch.ones(1, 3) # 目标值
loss = F.mse_loss(pre, trage) # loss function
loss.backward() # 反向传播
# w.grad, b.grad
w = w - lr * w.grad # 更新权值
b = b - lr * b.grad