简体   繁体   English

Pytorch,TypeError:object()不带参数

[英]Pytorch, TypeError: object() takes no parameters

This is probably a beginner question, but nevertheless: When running an image classifier build with pytorch, I get this error: 这可能是一个初学者的问题,但是:尽管如此,当使用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

I define the network class like this: 我这样定义网络类:

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)

Pretty sure that I imported all the relevant pytorch library modules correctly. 可以肯定的是,我正确地导入了所有相关的pytorch库模块。 (import torch.nn as nn and (将torch.nn导入为nn,
import torch) 进口火炬)

Any ideas of what I'm doing wrong? 关于我在做什么错的任何想法?

Thank you! 谢谢!

You might have a problem with your pytorch version, when I run the code: 当我运行代码时,您的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)

The output is: 输出为:

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)
)

I would suggest to update/reinstall pytorch. 我建议更新/重新安装pytorch。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM