Torch inttroduction

Published

March 10, 2023

import torch
x = torch.arange(12)
x
tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
x.shape
torch.Size([12])
x.numel()
12
X = x.reshape(3,4)
X
tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]])
torch.zeros((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,4))
tensor([[[1., 1., 1., 1.],
         [1., 1., 1., 1.],
         [1., 1., 1., 1.]],

        [[1., 1., 1., 1.],
         [1., 1., 1., 1.],
         [1., 1., 1., 1.]]])
torch.randn((2,3,4))
tensor([[[-0.5377, -0.6275, -0.0928,  0.4499],
         [ 0.4682,  0.3981,  0.3752, -0.5297],
         [ 0.2890,  1.2961, -1.6566, -0.4075]],

        [[ 0.7588, -2.0455, -2.4607, -0.2350],
         [-0.5268,  0.4881,  1.2903,  1.7120],
         [ 0.5241, -1.4895,  0.6152, -1.4183]]])
X[-1]
tensor([ 8,  9, 10, 11])
X[1:3]
tensor([[ 4,  5,  6,  7],
        [ 8,  9, 10, 11]])
X[1,2]
tensor(6)
X[1,2]=8
X
tensor([[ 0,  1,  2,  3],
        [ 4,  5,  8,  7],
        [ 8,  9, 10, 11]])
X[0:2,:] =12
X
tensor([[12, 12, 12, 12],
        [12, 12, 12, 12],
        [ 8,  9, 10, 11]])
Z = torch.zeros_like(Y)
print()
import pandas as pd
before = id(Y)
Y = Y+X
id(Y)== before
NameError: name 'Y' is not defined