object has no attribute

代码

import torch
from torch import nn

input = torch.tensor([[1, 2, 0, 3, 1],
                      [0, 1, 2, 3, 1],
                      [1, 2, 1, 0, 0],
                      [5, 2, 3, 1, 1],
                      [2, 1, 0, 1, 1]], dtype=torch.float32)
input = torch.reshape(input, (-1, 1, 5, 5))
print(input.shape)

class jiang(nn.Module):
    def __int__(self):
        super(jiang, self).__int__()
        self.maxpool1 = torch.nn.MaxPool2d(kernel_size=3,
                                           ceil_mode=True)

    def forward(self, input):
        output = self.maxpool1(input)
        return output

wen = jiang()
wen(input)

错误类型

D:\AnacondaPython\python.exe D:/pythonProject/CNN/nn.MaxPool_demo.py
Traceback (most recent call last):
  File "D:/pythonProject/CNN/nn.MaxPool_demo.py", line 23, in <module>
    wen(input)
  File "D:\AnacondaPython\lib\site-packages\torch\nn\modules\module.py", line 1110, in _call_impl
    return forward_call(*input, **kwargs)
  File "D:/pythonProject/CNN/nn.MaxPool_demo.py", line 19, in forward
    output = self.maxpool1(input)
  File "D:\AnacondaPython\lib\site-packages\torch\nn\modules\module.py", line 1185, in __getattr__
    raise AttributeError("'{}' object has no attribute '{}'".format(
AttributeError: 'jiang' object has no attribute 'maxpool1'
torch.Size([1, 1, 5, 5])

Process finished with exit code 1

拜谢大佬指点

阅读 2.6k
1 个回答

__init__ ,不是 __int__

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏