![](/img/trans.png)
[英]Pytorch: can we use nn.Module layers directly in forward() function?
[英]too many arguments in a Pytorch custom nn module forward function
我正在尝试在pytorch中创建一个神经网络,该神经网络的层数可变。 我的问题是,显然我正在将某种可迭代的项目与多个项目传递给只能接受一个参数的线性层。 我只是不明白为什么。
所以这是一些代码。 首先,我创建了自己的模块,然后将其导入到笔记本中
import torch
class NNet(torch.nn.Module):
def __init__(self, layer_shapes, activation_functions):
super(NNet, self).__init__()
assert len(layer_shapes) == len(activation_functions) + 1
self.layer_shapes = layer_shapes
self.activation_functions = activation_functions
linear_functions = list()
for i in range(len(self.layer_shapes)-1):
linear_functions.append(torch.nn.Linear(
self.layer_shapes[i], self.layer_shapes[i+1]))
self.linear_functions = linear_functions
def parameters(self):
parameters = list()
for function in self.linear_functions:
parameters = parameters+list(function.parameters())
return parameters
def forward(self, x):
assert x.shape[1] == self.layer_shapes[0]
y = x
for i in range(len(self.layer_shapes)-1):
lin = self.linear_functions[i](y)
y = self.activation_functions[i](lin)
return y
在笔记本中,错误在于y = self.activation_functions[i](self.linear_functions[i](y))
的forward
函数中
现在,我尝试使用由提供的数据集MNIST torchvision
,用我自己的模块。
batch_size = 100
epochs = 500
learning_rate = 0.001
train_set = torchvision.datasets.MNIST(root =
'../../data',
train=True,
transform=torchvision.transforms.ToTensor(),
download=True)
test_set = torchvision.datasets.MNIST(root =
'../../data',
train=False,
transform=torchvision.transforms.ToTensor(),
download=True)
train_loader = torch.utils.data.DataLoader(dataset=train_set,
batch_size=batch_size,
shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_set,
batch_size=batch_size,
shuffle=False)
model = nnet.NNet([784, 16, 10], [torch.nn.Tanh,
torch.nn.Softmax(dim=1)])
loss_function = torch.nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
loss_items = list()
for t in range(epochs):
for i, (images, labels) in enumerate(train_loader):
images = images.reshape(-1,28*28)
outputs = model(images)
loss = loss_function(outputs, labels)
loss_items.append(loss.item())
optimizer.zero_grad()
loss.backward()
optimizer.step()
最后一个for循环会产生错误:
TypeError Traceback (most recent call last)
<ipython-input-6-4ccb4b105a41> in <module>()
5 images = images.reshape(-1,28*28)
6
----> 7 outputs = model(images)
8 loss = loss_function(outputs, labels)
9 loss_items.append(loss.item())
~/.local/lib/python3.6/site-packages/torch/nn/modules/module.py in
__call__(self, *input, **kwargs)
475 result = self._slow_forward(*input, **kwargs)
476 else:
--> 477 result = self.forward(*input, **kwargs)
478 for hook in self._forward_hooks.values():
479 hook_result = hook(self, input, result)
~/Desktop/Archive/Computing/Projects/Python/ai/neural_network.py in
forward(self, x)
28 for i in range(len(self.layer_shapes)-1):
29 lin = self.linear_functions[i](y)
---> 30 y = self.activation_functions[i](lin)
31 return y
32
TypeError: __init__() takes 1 positional argument but 2 were given
我确定有人可以告诉我为什么会这样,但是有人可以给我一个有用的调试策略吗? 我是pytorch的新手,我怀疑这将是我遇到的最后一个麻烦。 因此,研究这些问题的策略将很有帮助。
我将不胜感激。
在model
的定义中,我忘记了torch.nn.Tanh类的括号。 它应该是torch.nn.Tanh()
我一直认为这些是函数而不是类。 我还有一些要解决的问题,但很高兴看到了。 真令人沮丧 我基本上是通过在代码中放置断言和打印语句来找到它的。
您可能要使用Sequential类
import torch.nn as nn
class NNet(nn.Module):
def __init__(self, idim, hdim, odim, depth):
super().__init__()
layers = [nn.Linear(idim, hdim)]
layers += [nn.Linear(hdim, hdim)
for i in range(depth)]
layers += [nn.Linear(hdim, odim)]
self.net = nn.Sequential(*layers)
def forward(self, x):
return self.net(x)
这也要注意参数等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.