How can I extract intermediate layer output from loaded CNN model?

Thanks for the answer. I’ve tried the first method you said, and it worked!
By the way, I’m curious, is there a possibility that a negative value will come out even after passing through the relu function?

Extractor

activation = {}
def get_activation(name):
    def hook(model, input, output):
        activation[name] = output.detach()
    return hook
model.fc3.register_forward_hook(get_activation('fc3'))
output = model(x)
activation['fc3']

tensor([[ -6.8134, -9.0775, -5.5216, 7.8066, -8.0170, -6.4616, -7.9551,
-10.1412, -7.9658, -0.5005, -8.6268, -3.1905, -9.5662, 5.7462,
-7.9711, -11.6978, -9.5338, -3.5561, -8.5417, -9.5329, -6.0198,
-6.1909, -7.8344, -6.3041, -7.9333, -9.6809, -4.8881, -8.7193,
-6.9742, -7.7307, -6.6041, -12.6779, -6.9386, -8.6367, -9.9166,
-6.2529, -9.1278, -7.4166, -6.9962, -10.5488, -9.0119, 5.0765,
-11.8163, -12.2532, -3.2899, -10.9596, -5.1833, -7.9950, -8.2713,
-4.9636, -1.9635, -9.4730, -8.7698, -9.3743, -8.4019, -6.0198,
-11.1968, -9.3805, -8.0975, -7.8172, -6.3183, -10.1746, -8.0311,
-8.4333, -9.8825, -12.8623, -8.6050, -5.4799, -10.6940, -8.5273,
-8.9511, -7.6086, -7.7462, -7.8799, -10.8322, -2.3653, -2.5826,
-2.2110, -8.2639, -10.9057, -8.5005, -5.1672, -11.6788, -8.2162,
-10.8182, -11.0485, -7.9058, -10.0653, -9.7719, -10.3265, -6.8779,
-8.0053, -7.7753, -8.8138, 21.4658, -9.3669, -9.3695, -8.1936,
-9.8124, -10.6773, -8.2539, -10.2004, -8.5175, -9.4678, -8.3933,
-1.1410, -8.9344, -5.1007, -8.7830, -8.8065, -7.1228, -9.0213,
-3.4798, -8.1264, -7.5717, -0.0733, -13.1373, -10.7214, -6.4440,
-10.8441, -10.6284, -7.5801, -9.7635, -7.8054, -8.3500, -9.1799,
-10.1877, -9.4926]], device=‘cuda:0’)

8 Likes