class Model(nn.Module):
def __init__(self):
pass
def forward(self, x):
x1 = x * 2
x2 = x1 + x
x3 = x2 + 1
return x3
I want to get x1, x2 and x3.
As a universal method, I CAN NOTreturn x1, x2, x3. I try forward_hook but it can only trace the module which defined in __init__.
Then those operators would be like a Module instead of a operator. So that I can use forward_hook. BUT IT’S EXTREMELY COMPLEX, do you have any better suggestions? @ptrblck
from torchvision.models.feature_extraction import create_feature_extractor
# Confused about the node specification here?
# We are allowed to provide truncated node names, and `create_feature_extractor`
# will choose the last node with that prefix.
feature_extractor = create_feature_extractor(
model, return_nodes=['blocks.0', 'blocks.1', 'blocks.2', 'blocks.3'])
# `out` will be a dict of Tensors, each representing a feature map
out = feature_extractor(torch.zeros(1, 3, 32, 32))
(And from the blog post, people do appear to believe that this is the best method to do it.)