我正在尝试在 pytorch 中使用 nn.module 构建一个神经网络。我想实现自定义权重、偏差和激活函数。 输入值=5,第一层权重= [[0.2, 0.3]],第二层权重= [[1.5],[2.5]],第一层偏差= 2,第二层偏差= 3,激活函数 y=x^ 2 输出值应该获得 2220.765625 但我的代码不计算这个值。你能帮我纠正这个代码吗?
import torch
import torch.nn as nn
class NeuralNet(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(in_features=1, out_features=2)
self.output = nn.Linear(in_features=2, out_features=1)
self.bias1 = torch.tensor([[2.]])
self.bias2 = torch.tensor([[3.]])
def act(self, x):
return x**2
def forward(self, x):
x = self.act(self.fc1(x)) + self.bias1
x = self.act(self.output(x)) + self.bias2
return x
def weights_initialization(self):
with torch.no_grad():
self.fc1.weight.copy_(torch.tensor([[0.2, 0.3]]))
self.output.weight.copy_(torch.tensor([[1.5],
[2.5]]))
net = NeuralNet()
input_data = torch.tensor([[5.]])
output = net(input_data)
print(output)
创建
weights_initialization
对象后,需要调用 net
方法。另外,手动添加 bias
值时,将线性图层内的 False
设置为 bias
。以下代码中还有一些其他修复L
import torch
import torch.nn as nn
class NeuralNet(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(in_features=1, out_features=2, bias=False)
self.output = nn.Linear(in_features=2, out_features=1, bias=False)
self.bias1 = torch.tensor([[2.]])
self.bias2 = torch.tensor([[3.]])
def act(self, x):
return x**2
def forward(self, x):
x = self.act(self.fc1(x) + self.bias1)
x = self.act(self.output(x) + self.bias2)
return x
def weights_initialization(self):
with torch.no_grad():
self.fc1.weight.copy_(torch.tensor([[0.2],
[0.3]]))
self.output.weight.copy_(torch.tensor([[1.5, 2.5]]))
net = NeuralNet()
net.weights_initialization()
input_data = torch.tensor([[5.]])
output = net(input_data)
print(output)