TypeError:object .__ init __()没有参数尝试子类化torch.FloatTensor

时间:2017-11-17 22:59:26

标签: python python-3.x inheritance pytorch

我试图在Python 3中继承torch.FloatTensor并且无法调用超类__init__。最小的例子:

import inspect
import torch

x = [[1, 2], [3, 4]]

print(torch.FloatTensor(x))
'''
 1  2
 3  4
[torch.FloatTensor of size 2x2]
'''

print(inspect.signature(torch.FloatTensor.__init__))
'''
(self, /, *args, **kwargs)
'''

class Image(torch.FloatTensor):
    def __init__(self, arg):
        print(inspect.signature(super().__init__))
        super().__init__(arg)

Image(x)
'''
(*args, **kwargs)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-369-4b1aee6414f6> in <module>()
     21         super().__init__(arg)
     22 
---> 23 Image(x)

<ipython-input-369-4b1aee6414f6> in __init__(self, arg)
     19     def __init__(self, arg):
     20         print(inspect.signature(super().__init__))
---> 21         super().__init__(arg)
     22 
     23 Image(x)

TypeError: object.__init__() takes no parameters
'''

我对Python比较新,所以我可能会遗漏一些明显的东西。为什么在检查时显示object.__init__() takes no parameters可能需要(*args, **kwargs)

修改:torch.FloatTensor已定义为here。它源自torch._TensorBase,其定义为here

0 个答案:

没有答案