import torch
from torch import nn
import torch.nn.functional as F
class FixedHiddenMLP(nn.Module):
def __init__(self):
super().__init__()
# 这里的rand_weight是不参与训练的,因为它不具备梯度
self.rand_weight = torch.rand((20, 20), requires_grad=False)
self.linear = nn.Linear(20, 20)
def forward(self, X):
X = self.linear(X)
X = F.relu(torch.mm(X, self.rand_weight) + 1)
X = self.linear(X)
while X.abs().sum() > 1:
X /= 2
return X.sum()
net = FixedHiddenMLP()
out = net(torch.randn(1,20))
print('out:', out)
【动手学深度学习-Pytorch版】通过继承nn.Module实现灵活的参数处理
于 2023-09-10 22:07:12 首次发布