Proper way to access to get the output of the i-th layer of a model

Hello, I am working with functorch Jacobian, I am using it to get the Jacobian of an intermediate layer i in my model with respect to the input z, however, I have two methods to access the i-th layer, the first is by just taking the model indexes, as follows:

i_layer_out = G.model[:i+1](z)

and the second is using recurssion:

inp = z
for j in range(i+1):
     inp = G.model[j](inp)
i_layer_out = inp   

The output of the model slightly varies depending on the approach, but the Jacobians are different, so I was wondering what is the best method to get the output of the intermediate layer, thanks for any comment.