I think you used the code directly. Looking at your previous post it seems that emb_teacher has requires_grad=False, maybe you want to use the backward hook for the student. I hope you are getting the technique behind using register_backward_hook, so you can adapt it for your use cases.