多层感知机和softmax没有本质区别,只是多加了一层隐藏层 没有隐藏层就是softmax回归,加上隐藏层就是多层感知机
import torch
from torch import nn
from d2l import torch as d2l
batch_size = 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
2.实现一个具有单隐藏层的多层感知机,他包含256个隐藏单元
num_inputs, num_outputs, num_hiddens = 784, 10, 256
# 28 * 28
# 声明是torch的Parameter
W1 = nn.Parameter(
# 生成随机数字的tensor
torch.randn(num_inputs, num_hiddens, requires_grad=True))
b1 = nn.Parameter(torch.zeros(num_hiddens, requires_grad = True))
W2 = nn.Parameter(
torch.randn(num_hiddens, num_outputs, requires_grad=T rue))
b2 = nn.Parameter(torch.zeros(num_outputs, requires_grad=True))
params = [W1, b1, W2, b2]
生成随机数字的tensor
这些随机数字满足标准正态分布
torch.randn(size) size可以为一个数字或者一个元组
import torch
x = torch.randn(3)
y = torch.randn(2,3)
print(x)
print(y)
tensor([-0.1201, -1.0340, 0.7885])
tensor([[-0.5694, 0.0461, 1.0315],
[-1.0342, -0.9757, -0.1844]])
torch.zeros_like(input, dtype=None, layout=None, device=None, requires_grad=False)
返回一个与给定输入张量形状和数据类型相同,但所有元素都被设置为零的新张量。
import torch
x = torch.tensor([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
y = torch.zeros_like(x)
print(y)
tensor([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])