我正在尝试使用Pytorch中的hook函数获取中间层输出(存储在我定义的全局变量中)。但是,我定义的变量在执行NN模型后为空。
# features are used for storing the middle layer output
features = []
# hook function,
# output is a tuple, so I change it into a list
def hook (module, input, output):
features = deepcopy(list(output))
handle = self.model.roi_heads.box.feature_extractor.pooler[3]
with torch.no_grad():
predictions = self.model(image_list)
print(features)
handle.remove()
期望获得该中间层的输出,但是要素为空。