I am attempting to create a tensor-like class that I wish to integrate with PyTorch. The class will have to store the tensor values and gradients in a custom format. I've been following the instructions at extending torch with a Tensor-like type. I have questions especially pertaining to gradient storage and calculation:
- I want to initialize my class from a (float) tensor and be able to convert it back. I know I can retrieve the values from the tensor using the numpy() function, but how do I get gradient data if I wish to store that too? Is directly accessing
tensor.gradthe correct way to do so? When I convert back to tensor, how can I give it back the stored gradient data? - In the above link I saw the instructions for making functions like
torch.addwork with my custom type, but that seems to only deal with the forward pass and I will also need to modify how the gradient is calculated by autograd. How do I define both the custom forward and backward versions of torch.add? Elsewhere, I saw the instructions for extendingtorch.autogradwith a custom function but I am not sure if those instructions can be merged with this case. Is there a way I can create a custom add function and say that that function'sapplyimplementstorch.addfor my class?
Thank you