Thanks a lot for your reply!
I tried s.register_hook(fn) and create_graph = True. Problem is that both input and output of hook function fn still have requires_grad = False and no grad_fn. So I can’t calculate gradient again… (If I register_hook for weights, they do have requires_grad = True and grad_fn)
I know that we can use torch.autograd.grad to calculate higher order gradients for w, but preactivation s is non-leaf variable. Then grad(loss, s) doesn’t have requires_grad nor grad_fn.
It seems that we can only calculate higher order gradients for leaf variables?