hooks.append(model.children._modules["17"].register_forward_hook(get_activation("s_f1"))),我想问一下这句代码是可以取出第17层的输出结果吧,它怎么在单机单卡可以训练,但单机多卡就报错呢。torch.nn.modules.module.ModuleAttributeError: 'DistributedDataParallel' object has no attribute 'model',这个错该怎么解决呢。谢谢大神指导
1条回答 默认 最新
- |__WhoAmI__| 2022-12-25 14:35关注
在使用单机多卡训练的情况下,由于模型的结构被封装在了DistributedDataParallel模块中,因此在访问模型的层时应该使用model.module.children而不是model.children。在上述代码中,应该将行修改为:
hooks.append(model.module.children._modules["17"].register_forward_hook(get_activation("s_f1")))
这样就可以在单机多卡训练的情况下访问模型的层并正常使用钩子函数了。
望采纳本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用