TypeError:hook()接受2个位置参数,但给出了3个 [英] TypeError: hook() takes 2 positional arguments but 3 were given
问题描述
我是pytorch的新手,我想在我的项目中使用 hook()
和 register_forward_pre_hook
I'm new to pytorch and I'm trying to use hook()
and register_forward_pre_hook
in my project
我尝试过的是
def get_features_hook(module,input):
print(input)
handle_feat = alexnet.features[0].register_forward_pre_hook(get_features_hook)
a = alexnet(input_data)
我在处遇到以下错误a = alexnet (input_data)
TypeError:get_features_hook()接受2个位置参数,但给出了3个
TypeError: get_features_hook() takes 2 positional arguments but 3 were given
我在这个问题上已经浪费了几个小时,我只是无法弄清楚。
I've lost few hours on this problem and I just can't able to figure it out.
有人喜欢帮助我吗?
在Shai的帮助下,我尝试了他的密码,得到了这个
With Shai's help, I tried his codes, and I got this
Conv2d(3, 64, kernel_size=(11, 11), stride=(4, 4), padding=(2, 2))
get_features_hook called with 2 args:
arg of type Conv2d
arg of type tuple
File "<input>", line 2, in get_features_hook
NameError: name 'args' is not defined
推荐答案
如果 get_features_hook
是在您的 torch.nn.Module
中定义的,应标注为 @staticmethod
,否则 self
会隐式传递给它
If get_features_hook
is defined inside your torch.nn.Module
, it should be annotated as @staticmethod
, otherwise self
is implicitly passed to it
这篇关于TypeError:hook()接受2个位置参数,但给出了3个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!