Pytorch tool - know pytorch

The basic element operation of pytorch

from __future__ import print_function
import torch

Create an uninitialized matrix

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

insert image description here

Create an initialized matrix

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

insert image description here

insert image description here

Create a matrix of all 0s and specify the data element type as long

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

insert image description here

Create tensors directly from data

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

insert image description here

Create a new tensor of the same size from an existing tensor

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

insert image description here

Use the randn_like method to get tensors of the same size, and use random initialization to assign values ​​to them

y=torch.randn_like(x,dtype=torch.float)
print(y)

insert image description here

Use the .size() method to get the shape of the tensor

print(x.size())

insert image description here

addition

the first method

x=torch.randn(5,3)
y=torch.randn(5,3)
print(x+y)

The second method

print(torch.add(x,y))

third method

result=torch.empty(5,3)
torch.add(x,y,out=result)
print(result)

The fourth way: in-situ replacement (executing y=y+x)

y.add_(x)
print(y)

insert image description here
Notice
insert image description here

slice operation

x[:,1]

insert image description here

change the shape of a tensor

x=torch.randn(4,4)
y=x.view(16)
z=x.view(-1,8)
x.size(),y.size(),z.size()

insert image description here

If there is only one element in the tensor, you can use item() to get the value out as a python number

x=torch.randn(1)
print(x,x.item())

insert image description here

Conversion between torch tensor and numpy array

a=torch.ones(5)
b=a.numpy()
a.add_(1)
print(a,b)

insert image description here

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

insert image description here
Notice
insert image description here

About cuda tensor: tensor can be moved to any device with the .to() method

windows
insert image description here
mac

if torch.backends.mps.is_available():
    device=torch.device('mps')
    #cpu上创建x,gpu上创建y
    x=torch.randn(1)
    y=torch.ones_like(x,device=device)
    x=x.to(device)
    #此时x,y都在gpu上
    z=x+y
    print(z)
    #再将z转移到cpu上
    print(z.to('cpu',torch.float32))

Guess you like

Origin blog.csdn.net/qq_40527560/article/details/131861436