pytorch tutorial Tensor value and operation use learning

  • 2021-11-30 00:25:23
  • OfStack

Directory 1, Tensors establishes a 5*3 matrix, uninitialized establishes a random initialization matrix establishes a zero initialization matrix, data type is Long establishes an tensor data comes from data obtains size2 of tensor, and realizes four ways of addition for Tensor operation. All in-situ substitution uses standard numpy operation uses torch. view changes the shape of tensor, converts tensor into numpy, and converts numpy array into pytorch TensorCUDA Tensors by mutual conversion of itemTorch Tensor and numpy

Reference website

1. Tensors

Tensors are similar to NumPy 's ndaeeays, the difference is that it can be used and accelerated on GPU.
Import package


from __future__ import print_function
import torch

Creates a 5*3 matrix, uninitialized


x = torch.empty(5,3)
print(x)

out


tensor([[ 1.4395e-36,  4.5848e-41,  1.4395e-36],
        [ 4.5848e-41,  1.4395e-36,  4.5848e-41],
        [ 1.4395e-36,  4.5848e-41,  2.8026e-45],
        [-1.9501e+00,  8.5165e+23,  0.0000e+00],
        [ 2.5223e-43,  0.0000e+00,  0.0000e+00]])

Establishing random initialization matrix


x = torch.rand(5,3)
print(x)

out


tensor([[ 0.8074,  0.9175,  0.8109],
        [ 0.3313,  0.5902,  0.9179],
        [ 0.6562,  0.3283,  0.9798],
        [ 0.8218,  0.0817,  0.4454],
        [ 0.5934,  0.0040,  0.3411]])

Create zero initialization matrix with data type Long


...
x = torch.zeros(5,3,dtype = torch.long) 
print(x)
...

out


tensor([[ 0,  0,  0],
        [ 0,  0,  0],
        [ 0,  0,  0],
        [ 0,  0,  0],
        [ 0,  0,  0]])

Establish an tensor data source from data


x = torch.tensor([5.5,3])
print(x)

out


tensor([ 5.5000,  3.0000])

The new tensor formed on the basis of the original tnesor will inherit the shapee and dtype attributes of the original tensor. Of course, we can also modify these attributes


x = x.new_ones(5,3,dtype = torch.double)
print(x)
x = torch.randn_like(x,dype = torch.float)
print(x)

out


x = torch.empty(5,3)
print(x)
0

Get size of tensor


print(x.size())

out


torch.Size([5, 3])

torch. size is 1 tuple and supports all tuple operations (tuple)

2. Operation of Tensor

Four Ways to Realize Addition

Method 1L


x = torch.empty(5,3)
print(x)
3

Method 2


x = torch.empty(5,3)
print(x)
4

Method 3: Output to additional tensor


x = torch.empty(5,3)
print(x)
5

Method 4: Replace in place-the results are stored in y


x = torch.empty(5,3)
print(x)
6

All in-situ replacements

All operations that replace tensor in place have suffixes, such as x. copy (y), which will change x

Use standard numpy operations


print(x[ : 1]

out


tensor([-0.0716,  0.8790,  0.8736, -2.9178,  0.4075])

Change the shape of tensor using torch. view


x = torch.empty(5,3)
print(x)
9

out


torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])

tensor is converted to the number of numpy, using item


x = torch.rnadn(1)
print(x)
print(x.item())

Conversion between Torch, Tensor and numpy


a = torch.ones(5)
print (a) 

out


tensor([ 1.,  1.,  1.,  1.,  1.])

And changing the value of tensor will also change the value of numpy


a.add_(1)
print(a)
print(b)

out


tensor([ 2.,  2.,  2.,  2.,  2.])
[ 2.  2.  2.  2.  2.]

Conversion of numpy array to pytorch Tensor


import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a,1,out = a )
print(a)
print ( b ) 

out


[ 2.  2.  2.  2.  2.]
tensor([ 2.,  2.,  2.,  2.,  2.], dtype=torch.float64)

All tensor on cpu support numpy conversion except tensor with char shape

CUDA Tensors

Tensors can be moved to other devices for use. Method of to


...
if torch.cuda.is_avaulable(): 
device = torch.device( " cuda " ) 
y = torch.ones_like(x,device = devcie) 
x= x.to(device) 
z = x+y 
print(z) 
print(z.to( " cpu " ,torch.double)) 
...

out


tensor([-1.0620], device='cuda:0')
tensor([-1.0620], dtype=torch.float64)

The above is the Tensor study notes of the pytorch tutorial. For more information about the pytorch tutorial, please pay attention to other related articles on this site!


Related articles: