python - 无法初始化我的神经网络 PyTorch 的权重
问题描述
我无法使用函数 MyNet.apply(init_weights) 初始化权重。
这些是我的功能:
def init_weights(net):
if type(net) == torch.nn.Module:
torch.nn.init.kaiming_uniform_(net.weight)
net.bias.data.fill_(0.01) # tots els bias a 0.01
我的神经网络如下:
class NeuralNet(torch.nn.Module):
def __init__(self):
super().__init__() # Necessary for torch to detect this class as trainable
# Here define network architecture
self.layer1 = torch.nn.Linear(28**2, 32).to(device) # Linear layer with 32 neurons
self.layer2 = torch.nn.Linear(32, 64).to(device) # Linear layer with 64 neurons
self.layer3 = torch.nn.Linear(64, 128).to(device) # Linear layer with 128 neurons
self.output = torch.nn.Linear(128, 1).to(device) # Linear layer with 1 output neuron (binary output)
def forward(self, x):
# Here define architecture behavior
x = torch.sigmoid(self.layer1(x)).to(device) # x = torch.nn.functional.relu(self.layer1(x))
x = torch.sigmoid(self.layer2(x)).to(device)
x = torch.sigmoid(self.layer3(x)).to(device)
return torch.sigmoid(self.output(x)).to(device) # Binary output
type(net) 打印为线性,因此它永远不会进入 if 语句,如果我删除它会产生以下错误:
AttributeError:“NeuralNet”对象没有属性“权重”
解决方案
您应该只初始化线性层的权重:
def init_weights(net):
if type(net) == torch.nn.Linear:
torch.nn.init.kaiming_uniform_(net.weight)
net.bias.data.fill_(0.01) # tots els bias a 0.01
推荐阅读
- linux - 有没有办法在 Jenkins 中针对 RDS (Oracle) 运行 SQL 查询而不使用 SQL Plus Script Runner Plugin
- python - 加入函数没有从列表中返回字符串值
- bash - 当包含多行时缩进“读取”输入
- javascript - 如何在 NodeJS 中要求一个带参数和不带参数的模块?
- google-cloud-platform - GCP 数据流与云函数
- rest - 尽管在 SDN Floodlight 控制器上启用了防火墙规则,但对所有主机的 Ping 仍然有效
- docker - 如何修复 Docker Ubuntu 18.04 映像中的“哈希和不匹配”
- c++ - 我怎样才能拥有一个以二维数组作为参数的函数,而该数组有一个我想要更改的参数/维度?
- python - 如何从检索到的 JSON 数据中删除双引号
- aws-lambda - spacy with zappa 在 AWS lambda 上显示错误