Introduction of Tensors


1.Reshaping a tensor

x = torch.arange(10)
print(x)
# tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) 
print(x.view(2,5))          
# two rows with five columns
# tensor([[0, 1, 2, 3, 4],
          [5, 6, 7, 8, 9]])

Note that the original x is not modified, but p = x.view(2,5) can store the change in variable p.

2.Entries of a tensor can be scalar

A matrix is 2-dimensional Tensor

A row of a matrix is a 1-dimensional Tensor

An entry of a matrix is a 0-dimensional Tensor

0-dimensional Tensor are scalar

If we want to convert a 0-dimensional Tensor into python number, we need to use item()

x = torch.Tensor([[1,2,3,4,5],[6,7,8,9,10]])
a = x[0,2] # how to slice a tensor: x[0], x[1:4], etc.
b = a.item()

print(a)
print(type(a))
print(b)
print(type(b))

# tensor(3.)
# <class 'torch.Tensor'>
# 3.0
# <class 'float'>

3.The Storage of Tensors

A = torch.arange(5) # tensor([0, 1, 2, 3, 4])
B = A[2:]

print(A.storage().data_ptr())
print(B.storage().data_ptr())

# 2076006947200
# 2076006947200

Author: cipher
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source cipher !
  TOC