Computer_Vision/Chapter02/Initializing_a_tensor.ipynb
2024-02-13 03:34:51 +01:00

7.9 KiB

Open In Colab

import torch
x = torch.tensor([[1,2]])
y = torch.tensor([[1],[2]])
print(x.shape)
# torch.Size([1,2]) # one entity of two items
print(y.shape)
# torch.Size([2,1]) # two entities of one item each
print(x.dtype)
# torch.int64
torch.Size([1, 2])
torch.Size([2, 1])
torch.int64
x = torch.tensor([False, 1, 2.0])
print(x)
# tensor([0., 1., 2.])
tensor([0., 1., 2.])
torch.zeros((3, 4))
tensor([[0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.]])
torch.ones((3, 4))
tensor([[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]])
torch.randint(low=0, high=10, size=(3,4))
tensor([[8, 2, 5, 9],
        [6, 1, 6, 0],
        [5, 6, 9, 5]])
torch.rand(3, 4)
tensor([[0.3196, 0.9387, 0.9268, 0.1246],
        [0.6700, 0.7529, 0.8687, 0.3948],
        [0.2279, 0.2309, 0.0151, 0.0339]])
torch.randn((3,4))
tensor([[ 1.3746, -0.2895, -0.4134, -1.8711],
        [-0.3113,  0.8154,  0.5902, -0.8436],
        [ 0.8125,  0.3806, -0.5974, -0.6515]])
import numpy as np
x = np.array([[10,20,30],[2,3,4]])
y = torch.tensor(x)
print(type(x), type(y))
<class 'numpy.ndarray'> <class 'torch.Tensor'>