py
import torch
import torch.nn as nn
# 定义简单的神经网络
class SimpleNN(nn.Module):
def __init__(self):
super(SimpleNN, self).__init__()
self.fc1 = nn.Linear(2, 2) # 隐藏层
self.fc2 = nn.Linear(2, 1) # 输出层
# 将隐藏层权重和偏置初始化为0
self.fc1.weight.data.fill_(0)
self.fc1.bias.data.fill_(0)
# 将输出层权重和偏置初始化为0
self.fc2.weight.data.fill_(0)
self.fc2.bias.data.fill_(0)
def forward(self, x):
x = torch.relu(self.fc1(x)) # 激活函数
x = self.fc2(x)
return x
# 创建网络实例
net = SimpleNN()
# 输入数据
input_data = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
# 前向传播
output = net(input_data)
print("Output:", output)
记录一下上述代码,体现了隐含层和输出层的权重为全0时的结果,可惜奇怪的是李沐的例子中权重改为全0仍然能够成功训练,目前还不知道为什么。