Pytorch,TypeError:object()不带参数

时间:2017-05-04 11:24:01

标签: python computer-vision torch pytorch

这可能是一个初学者的问题,但是:当使用pytorch运行图像分类器构建时,我收到此错误:

Traceback (most recent call last):
File "/pytorch/kanji_torch.py", line 47, in <module>
    network = Network()
  File "/pytorch/kanji_torch.py", line 113, in __init__
    self.conv1 = nn.Conv2d(1, 32, 5)
  File "/python3.5/site-packages/torch/nn/modules/conv.py", line 233, in __init__
    False, _pair(0), groups, bias)
  File "/python3.5/site-packages/torch/nn/modules/conv.py", line 32, in __init__
    out_channels, in_channels // groups, *kernel_size))
TypeError: object() takes no parameters

我像这样定义网络类:

class Network(torch.nn.Module):
    def __init__(self):
        super(Network, self).__init__()
        self.conv1 = nn.Conv2d(1, 32, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(32, 64, 5)
        self.pool2 = nn.MaxPool2d(2, 2)
        self.conv3 = nn.Conv2d(64, 64, 5)
        self.pool2 = nn.MaxPool2d(2, 2)
        self.fc1 = nn.Linear(64 * 5 * 5, 512)
        self.fc2 = nn.Linear(512, 640)
        self.fc3 = nn.Linear(640, 3756)

很确定我正确导入了所有相关的pytorch库模块。 (导入torch.nn为nn和
进口火炬)

我做错了什么想法?

谢谢!

1 个答案:

答案 0 :(得分:0)

当我运行代码时,你的pytorch版本可能有问题:

class Network(torch.nn.Module):
    def __init__(self):
        super(Network, self).__init__()
        self.conv1 = nn.Conv2d(1, 32, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(32, 64, 5)
        self.pool2 = nn.MaxPool2d(2, 2)
        self.conv3 = nn.Conv2d(64, 64, 5)
        self.pool2 = nn.MaxPool2d(2, 2)
        self.fc1 = nn.Linear(64 * 5 * 5, 512)
        self.fc2 = nn.Linear(512, 640)
        self.fc3 = nn.Linear(640, 3756)
print(network)

输出结果为:

Network (
  (conv1): Conv2d(1, 32, kernel_size=(5, 5), stride=(1, 1))
  (pool): MaxPool2d (size=(2, 2), stride=(2, 2), dilation=(1, 1))
  (conv2): Conv2d(32, 64, kernel_size=(5, 5), stride=(1, 1))
  (pool2): MaxPool2d (size=(2, 2), stride=(2, 2), dilation=(1, 1))
  (conv3): Conv2d(64, 64, kernel_size=(5, 5), stride=(1, 1))
  (fc1): Linear (1600 -> 512)
  (fc2): Linear (512 -> 640)
  (fc3): Linear (640 -> 3756)
)

我建议更新/重新安装pytorch。