简体   繁体   English

类型错误:hook() 需要 2 个位置参数,但给出了 3 个

[英]TypeError: hook() takes 2 positional arguments but 3 were given

I'm new to pytorch and I'm trying to use hook() and register_forward_pre_hook in my project我是 pytorch 的新手,我正在尝试在我的项目中使用hook()register_forward_pre_hook

What I've tried is我试过的是

def get_features_hook(module,input):
    print(input)

handle_feat = alexnet.features[0].register_forward_pre_hook(get_features_hook)


a = alexnet(input_data)

And I got belows error at a = alexnet(input_data)我在a = alexnet(input_data)处出现以下错误

TypeError: get_features_hook() takes 2 positional arguments but 3 were given类型错误:get_features_hook() 需要 2 个位置参数,但给出了 3 个

I've lost few hours on this problem and I just can't able to figure it out.我已经在这个问题上失去了几个小时,我无法弄清楚。

Anyone likes to help me?有人喜欢帮助我吗?


With Shai's help, I tried his codes, and I got this在Shai的帮助下,我尝试了他的代码,我得到了这个

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隐式传递给它

I got the same error.我得到了同样的错误。 I've tried re-running the notebook and that solved it我试过重新运行笔记本并解决了它

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

相关问题 TypeError: hook() 需要 3 个位置 arguments 但给出了 4 个 - TypeError: hook() takes 3 positional arguments but 4 were given TypeError:create()接受2个位置参数,但给出了4个 - TypeError: create() takes 2 positional arguments but 4 were given 类型错误:randint() 需要 3 个位置参数,但给出了 4 个 - TypeError: randint() takes 3 positional arguments but 4 were given TypeError:deriv()接受2个位置参数,但给出了4个 - TypeError: deriv() takes 2 positional arguments but 4 were given TypeError:init()接受0个位置参数,但给出了5个 - TypeError: init() takes 0 positional arguments but 5 were given TypeError: _() 需要 2 个位置 arguments 但有 4 个被赋予 Databricks - TypeError: _() takes 2 positional arguments but 4 were given Databricks TypeError:open()接受0个位置参数,但给出了2个 - TypeError: open() takes 0 positional arguments but 2 were given TypeError:invoke()接受2个位置参数,但给出了3个 - TypeError: invoke() takes 2 positional arguments but 3 were given TypeError: imwrite() 接受 2 个位置参数,但给出了 3 个 - TypeError: imwrite() takes 2 positional arguments but 3 were given TypeError 接受 2 个位置参数,但给出了 3 个 - TypeError takes 2 positional arguments but 3 were given
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM