When do we need autograd.Function

Hi, I’m confused about autograd.Function recently. I think most of the common mathematical operation can be implemented by pytorch functions, and pytorch can automatically build computation graph for us to backward.

Do we need to use this class only when we need to define our own derivation or integrate some operations in one class?

If I don’t need some special derivation operation, can I just define a normal function containing several math functions and use it in my forward procedure?

Ideally, yes. you are right. If you do not need any custom functions for your model, you need not use autograd.Function. You could just use modules from torch.nn packages, APIs from torch, torch.nn.functional packages. As you said, you can create normal python function with these predefined APIs from torch and torch will take care of calculating the gradient automatically for you.

Thanks a lot!I figured it out now.