Hi, @smth. I deeply appreciate this working snippet, it is basically what I asked.
I have one further question about this. It seems the grad_output we provide in this line
gradients = torch.randn(*output.size())

are never used to compute anything during the backward process since when I tried to print every self.output[i+1].grad.data, they’re all equal to the random gradient generated by torch.randn(*output.size()). And everything in self.output has grad_fn=None. That make sense because as you mentioned this hack “ompletely avoid autograd’s automatic backward computation and manually reverse-computing the backward graph”.
My question is if I want to make this net works “normally”, do I need to manually handle the entire backward process (e.g. write all backward functions for all layers, manually compute gradients, and etc.)? Is there any way to make this less hacky or any function (e.g. backward function of Conv layer) to borrow?