Hi,
I am trying to extend the tensor class, as a simple toy experiment to understand basics of the pytorch, to have a new function “say()”, which will only print the contents of the tensor or a related info. Of course, just calling repr would do a similar behaviour, however I am trying the extend the class.
If the tensor class were a common python class, which has an init function, then the following might have worked:
def new_tensor(torch.Tensor):
init(self, an_array):
super(self).init(an_array)
def say(self):
print(super(self))
a desired usage would be:
a = new_tensor([0, 1, 3])
a.say() // prints tensor([0.0000, 1.0000, 3.0000])
How can we extend the tensor class?

  
      github.com
  
  
    pytorch/pytorch/blob/master/torch/tensor.py 10
import sys
import torch
import torch._C as _C
from collections import OrderedDict
import torch.utils.hooks as hooks
import warnings
import weakref
from torch._six import imap
from torch._C import _add_docstr


# NB: If you subclass Tensor, and want to share the subclassed class
# across processes, you must also update torch/multiprocessing/reductions.py
# to define a ForkingPickler serialization mode for the class.
#
# NB: If you add a new method to Tensor, you must update
# torch/__init__.py.in to add a type annotation for your method;
# otherwise, it will not show up in autocomplete.
class Tensor(torch._C._TensorBase):
    def __deepcopy__(self, memo):


  This file has been truncated. show original