PyTorch (1) Tensor

1. CPU Tensor:

A torch.Tensor is a multi-dimensional matrix containing elements of a single data type.

A tensor can be constructed from a Python list or sequence using the torch.tensor() constructor:

import torch
a = torch.tensor([[2,3],[4,5],[6,7]])

print(a)
# tensor([[ 2.,  3.],
#         [ 4.,  5.],
#         [ 6.,  7.]])

print(a.shape)
# torch.Size([3, 2])

print(a.dtype)
# torch.float32

Assign the data type to the Tensor elements:

b = torch.tensor([[1.5, 2.3]], dtype=torch.int8)

print(b)
# tensor([[ 1,  2]], dtype=torch.int8)

Create a zero matrix:

c = torch.zeros(1 , 3)

print(c)
tensor([[ 0.,  0.,  0.]])

Create a random tensor and add it with other tensor:

d = torch.rand(1,3)
e = c + d

2. Numpy array and Torch:

a = torch.tensor([1, 2, 3])

b = a.numpy()
print(b)
# [1, 2, 3]

print(type(b))
# <class 'numpy.ndarray'>

Let's modify b and see what is happening on a:

b[0] = 0

print(a)
# tensor([ 0,  2,  3])

!! The Torch Tensor and NumPy array will share their underlying memory locations, and changing one will change the other.

Convert a np.array to a torch.tensor:

c = np.array([1, 2, 3])
d = torch.from_numpy(c)

3. CUDA Tensor:

Tensors can be put onto different devices (e.g. CPU, GPU).

if torch.cuda.is_available():
    a = torch.tensor([1,2,3], device="cuda")
print(a)
# tensor([ 1,  2,  3], device='cuda:0')

Torch defines eight CPU tensor types and eight GPU tensor types.

Convert CPU tensor to GPU tensor:

b = torch.tensor([1, 2, 3])

print(b)
# tensor([ 1,  2,  3])

c = b.cuda()

print(c)
# tensor([ 1,  2,  3], device='cuda:0')

留言

熱門文章