Trying to get an introduction to PyTorch. I am a little confused by some of the methods used in the class I am in. Let’s say we have this tensor object called ‘abc’. I see the method dtype which returns data type of the tensor object – which is also, I guess, a tensor type. Then you can use, I think, the Python ‘type()’ builtin to do the same thing, albeit differently named type.

Then there is a case where you want to cast tensor object ‘abc’ by tacking a abc.numpy() constructor onto it. Yet, although imported, the numpy constructor is not anywhere a documented as a Tensor class method.

So I guess I am having trouble with Python <–> PyTorch <–> Numpy. Little help?

The tensor.dtype is an attribute of a tensor object of the Tensor class in PyTorch (which is similar to a NumPy array). I.e., PyTorch tensors and NumPy arrays are relatively similar and totally different from say Python lists. In any case, the dtype, which is specific to PyTorch tensors and NumPy arrays give you much more specific information about the object. I.e., type() will just tell you that the tensor is an object created from class Tensor whereas dtype tells you also what data type the tensor has, i.e., float32, float64, int32, int64, and so forth

Then there is a case where you want to cast tensor object ‘abc’ by tacking a abc.numpy() constructor onto it. Yet, although imported, the numpy constructor is not anywhere a documented as a Tensor class method.

Not sure why it’s not documented. But in either case, this is just converting from PyTorch tensor to NumPy array. Again, these are mostly similar and usually you don’t need to do this, except you want to use some specific methods that are only available in NumPy but not in PyTorch. In 99% of the time in DL, you won’t to do that conversion.

>>> t = torch.tensor([1,2,3])
>>> t.dtype
torch.int64
>>> t.type()
'torch.LongTensor'
>>> np2t = t.numpy()
>>> np2t.dtype
dtype('int64')
>>> np2t.type()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'numpy.ndarray' object has no attribute 'type'
>>> t2np = torch.from_numpy(np2t)
>>> t2np.dtype
torch.int64
>>> t2np.type()
'torch.LongTensor'

See, I am just confused about the syntax within this mash-up of Python, Numpy, PyTorch. The torch object does have documented method for ‘from_numpy()’. Thanks for your help, I am peddling as fast as I can!