5.3.0 头文件
import torch
import torch.nn.functional as F
from torch import nn
5.3.1 定义不带参数的层
# 定义不带参数的层
class CenteredLayer(nn.Module):
def __init__(self):
super().__init__()
def forward(self, X):
return X - X.mean()
layer = CenteredLayer()
Y = layer(torch.FloatTensor([1, 2, 3, 4, 5]))
print(Y)
# 输出:
# tensor([-2., -1., 0., 1., 2.])
# 将层放到顺序块中
net = nn.Sequential(nn.Linear(8, 128), CenteredLayer())
Y = net(torch.rand(4, 8))
print(Y.mean())
# 输出:
# tensor(2.3283e-09, grad_fn=<MeanBackward0>)
5.3.2 定义带参数的层
# 定义不带参数的层
class MyLinear(nn.Module):
# in_units:输入神经元个数
# units:输出神经元个数
def __init__(self, in_units, units):
# 调用父类构造函数
super().__init__()
# 随机初始化权重w
self.weight = nn.Parameter(torch.randn(in_units, units))
# 随机初始化偏移量
self.bias = nn.Parameter(torch.randn(units,))
# 前向传播
def forward(self, X):
linear = torch.matmul(X, self.weight.data) + self.bias.data
return F.relu(linear)
linear = MyLinear(5, 3)
print(linear.weight)
# 输出:
# tensor([[ 0.0612, -1.9050, -1.8448],
# [ 0.5920, -0.0301, -0.9718],
# [-1.1657, 0.3628, 0.4078],
# [ 1.3419, -0.2535, -0.7558],
# [-1.2800, 1.3049, -0.5029]], requires_grad=True)
本小节完整代码如下
import torch
import torch.nn.functional as F
from torch import nn
# ------------------------------定义不带参数的层------------------------------------
# 定义不带参数的层
class CenteredLayer(nn.Module):
def __init__(self):
super().__init__()
def forward(self, X):
return X - X.mean()
layer = CenteredLayer()
Y = layer(torch.FloatTensor([1, 2, 3, 4, 5]))
print(Y)
# 输出:
# tensor([-2., -1., 0., 1., 2.])
# 将层放到顺序块中
net = nn.Sequential(nn.Linear(8, 128), CenteredLayer())
Y = net(torch.rand(4, 8))
print(Y.mean())
# 输出:
# tensor(2.3283e-09, grad_fn=<MeanBackward0>)
# ------------------------------定义带参数的层------------------------------------
# 定义不带参数的层
class MyLinear(nn.Module):
# in_units:输入神经元个数
# units:输出神经元个数
def __init__(self, in_units, units):
# 调用父类构造函数
super().__init__()
# 随机初始化权重w
self.weight = nn.Parameter(torch.randn(in_units, units))
# 随机初始化偏移量
self.bias = nn.Parameter(torch.randn(units,))
# 前向传播
def forward(self, X):
linear = torch.matmul(X, self.weight.data) + self.bias.data
return F.relu(linear)
linear = MyLinear(5, 3)
print(linear.weight)
# 输出:
# tensor([[ 0.0612, -1.9050, -1.8448],
# [ 0.5920, -0.0301, -0.9718],
# [-1.1657, 0.3628, 0.4078],
# [ 1.3419, -0.2535, -0.7558],
# [-1.2800, 1.3049, -0.5029]], requires_grad=True)
浙公网安备 33010602011771号