How to get the output from an intermediate layer of a custom made model in Pytorch?

I am a beginner to Pytorch and I have trained a custom built model. It looks like this:

stackedIndRNN_encoder(
  (DIs): ModuleList(
    (0): Linear(in_features=150, out_features=512, bias=True)
    (1): Linear(in_features=512, out_features=512, bias=True)
    (2): Linear(in_features=512, out_features=512, bias=True)
    (3): Linear(in_features=512, out_features=512, bias=True)
    (4): Linear(in_features=512, out_features=512, bias=True)
    (5): Linear(in_features=512, out_features=512, bias=True)
  )
  (BNs): ModuleList(
    (0): Batch_norm_step(
      (bn): BatchNorm1d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    )
    (1): Batch_norm_step(
      (bn): BatchNorm1d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    )
    (2): Batch_norm_step(
      (bn): BatchNorm1d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    )
    (3): Batch_norm_step(
      (bn): BatchNorm1d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    )
    (4): Batch_norm_step(
      (bn): BatchNorm1d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    )
    (5): Batch_norm_step(
      (bn): BatchNorm1d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    )
  )
  (RNNs): ModuleList(
    (0): IndRNN_onlyrecurrent()
    (1): IndRNN_onlyrecurrent()
    (2): IndRNN_onlyrecurrent()
    (3): IndRNN_onlyrecurrent()
    (4): IndRNN_onlyrecurrent()
    (5): IndRNN_onlyrecurrent()
  )
  (lastfc): Linear(in_features=512, out_features=60, bias=True)
)

I want to access the output from the last layer, RNNs that is a ** 512-dimensional vector** that is inputted into the final fully connected layer, lastfc. I have tried forward hook in the following way:

RNNs_output = None

def RNN_hook(module, input_, output):
	global RNNs_output
	RNNs_output = output

model.lastfc.register_forward_hook(RNN_hook)	

model(skeletons)
print(RNNs_output)

but I get None in RNNS_output. What am I doing wrong?

After registering a forward hook, you would need to perform a forward pass, so that the input and output will be filled with tensors.
Also, if lastfc is your last layer, you’ll also get its output as the return value from your model’s forward:

output = model(data)

Thanks for your reply, @ptrblck. Let me try and get back to you.