Thank you!!!
So let me restate what you mentioned to make sure I got it correct.
Inside a function, all the constant and temporary elements should map to Variable.
However, we should be very careful and dont Variable the elements that are gonna be changed.
e.g. in my function I should not say     DCT_Output = torch.mm 2(DCT_Matrix_var,Variable(torch.t(InputMatrix))). But I can say     D = Variable(len(InputMatrix)) or     DCT_Matrix = (torch.sin(Grid_R+Variable(0.05))*(Grid_R /D)).
Because using a variable for an element that is changing inside the function would mess up the gradient.
Am I understanding it right?