PyTorch Tensor

PyTorch
Deep Learning
Author

Kunlei Lian

Published

June 24, 2024

Tensor

Tensor Construction

import torch

a = torch.ones(3)
print(f"a: {a}")
print(f"d.shape: {a.shape}")
print(f"a.dim: {a.dim()}")
print(f"a.dtype: {a.dtype}")
a: tensor([1., 1., 1.])
d.shape: torch.Size([3])
a.dim: 1
a.dtype: torch.float32
torch.zeros(size=(2, 3, 4))
tensor([[[0., 0., 0., 0.],
         [0., 0., 0., 0.],
         [0., 0., 0., 0.]],

        [[0., 0., 0., 0.],
         [0., 0., 0., 0.],
         [0., 0., 0., 0.]]])
torch.ones((2, 3))
tensor([[1., 1., 1.],
        [1., 1., 1.]])
b = torch.tensor([
    [1.0, 2.0],
    [2.0, 4.0],
    [3.0, 6.0]
])
print(f"b: {b}")
print(f"b.shape: {b.shape}")
print(f"b.dim: {b.dim()}")
print(f"b.dtype: {b.dtype}")
b: tensor([[1., 2.],
        [2., 4.],
        [3., 6.]])
b.shape: torch.Size([3, 2])
b.dim: 2
b.dtype: torch.float32
image_t = torch.randn(size=(3, 5, 5))
weights = torch.tensor([0.2126, 0.7152, 0.0722])
batch_t = torch.randn(size=(2, 3, 5, 5))
image_gray_naive = image_t.mean(dim=-3)
batch_gray_naive = batch_t.mean(dim=-3)

image_gray_naive.shape, batch_gray_naive.shape
(torch.Size([5, 5]), torch.Size([2, 5, 5]))
image_gray_naive
tensor([[-0.2332,  0.3938, -0.3767,  0.5071, -0.7080],
        [ 0.3150, -0.0869,  0.3916, -0.1328,  0.2715],
        [-0.1035, -0.4130,  0.1052, -0.1838, -0.2134],
        [-0.4150, -0.0241,  0.7105, -0.9059,  0.5978],
        [ 0.2595, -0.3144,  0.1214, -0.4523,  1.3808]])
image_t[:, 0, 0].mean()
tensor(-0.2332)
batch_gray_naive
tensor([[[-0.2599, -0.2635,  0.0538,  0.3724,  0.0113],
         [-1.0019, -0.4934,  0.3516, -0.5659, -0.2056],
         [-0.2812, -1.2758,  0.8221, -0.3691, -0.9555],
         [ 0.7293, -0.8631,  0.6655, -0.1749,  0.6374],
         [-0.9790, -0.0309, -0.0135,  0.6081,  0.1212]],

        [[-0.6569, -0.2976,  0.4795, -0.3773,  0.5667],
         [-0.3750,  0.9626,  1.4857,  0.1569, -0.3597],
         [-0.8239, -0.8770,  0.1830, -1.0730,  0.1203],
         [-0.1412, -0.3397,  0.0377, -0.4546,  0.1984],
         [ 0.7905,  0.2810, -0.8559,  0.1058,  0.4775]]])
batch_t[0, :, 0, 0].mean()
tensor(-0.2599)
unsqueezed_weights = weights.unsqueeze(dim=-1).unsqueeze(dim=-1)
unsqueezed_weights
tensor([[[0.2126]],

        [[0.7152]],

        [[0.0722]]])
weights.unsqueeze(dim=0).unsqueeze(dim=0)
tensor([[[0.2126, 0.7152, 0.0722]]])
image_t.shape, unsqueezed_weights.shape
(torch.Size([3, 5, 5]), torch.Size([3, 1, 1]))
double_points = torch.ones(size=(10, 2), dtype=torch.double)
short_points = torch.tensor([[1, 2], [3, 4]], dtype=torch.short)
short_points.dtype
torch.int16
double_points = torch.zeros(size=(10, 2)).double()
double_points.dtype
double_points.shape
torch.Size([10, 2])
a = torch.ones(size=(3, 2))
a_t = torch.transpose(a, dim0=0, dim1=1)
a.shape, a_t.shape
(torch.Size([3, 2]), torch.Size([2, 3]))
a, a_t
(tensor([[1., 1.],
         [1., 1.],
         [1., 1.]]),
 tensor([[1., 1., 1.],
         [1., 1., 1.]]))
points = torch.tensor([[4.0, 1.0], [5.0, 3.0], [2.0, 1.0]])
points.storage()
/var/folders/8h/0198_l7s2vj1nwsk_h331bc00000gn/T/ipykernel_80062/2923139482.py:2: UserWarning: TypedStorage is deprecated. It will be removed in the future and UntypedStorage will be the only storage class. This should only matter to you if you are using storages directly.  To access UntypedStorage directly, use tensor.untyped_storage() instead of tensor.storage()
  points.storage()
 4.0
 1.0
 5.0
 3.0
 2.0
 1.0
[torch.storage.TypedStorage(dtype=torch.float32, device=cpu) of size 6]
points.untyped_storage()
 0
 0
 128
 64
 0
 0
 128
 63
 0
 0
 160
 64
 0
 0
 64
 64
 0
 0
 0
 64
 0
 0
 128
 63
[torch.storage.UntypedStorage(device=cpu) of size 24]
a = torch.ones(size=(3, 2))
a
tensor([[1., 1.],
        [1., 1.],
        [1., 1.]])
a.zero_()
tensor([[0., 0.],
        [0., 0.],
        [0., 0.]])
points = torch.ones(size=(3, 4))
points_np = points.numpy()
points_np
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]], dtype=float32)
import imageio.v2 as imageio

img_arr = imageio.imread('./image-dog/bobby.jpg')
img_arr.shape
(720, 1280, 3)
type(img_arr)
numpy.ndarray
img = torch.from_numpy(img_arr)
out = img.permute(2, 0, 1)
out.shape
torch.Size([3, 720, 1280])
batch_size = 3
batch = torch.zeros(size=(batch_size, 3, 256, 256), dtype=torch.uint8)

import os
data_dir = './image-cats/'
filenames = [name for name in os.listdir(data_dir)
            if os.path.splitext(name)[-1] == '.png']
            
for i, filename in enumerate(filenames):
    img_arr = imageio.imread(os.path.join(data_dir, filename))
    img_t = torch.from_numpy(img_arr)
    img_t = img_t.permute(2, 0, 1)
    img_t = img_t[:3]
    batch[i] = img_t
batch.shape
torch.Size([3, 3, 256, 256])