Table of contents
- The basic element operation of pytorch
-
- Create an uninitialized matrix
- Create an initialized matrix
- Create a matrix of all 0s and specify the data element type as long
- Create tensors directly from data
- Create a new tensor of the same size from an existing tensor
- Use the randn_like method to get tensors of the same size, and use random initialization to assign values to them
- Use the .size() method to get the shape of the tensor
- addition
- slice operation
- change the shape of a tensor
- If there is only one element in the tensor, you can use item() to get the value out as a python number
- Conversion between torch tensor and numpy array
- About cuda tensor: tensor can be moved to any device with the .to() method
The basic element operation of pytorch
from __future__ import print_function
import torch
Create an uninitialized matrix
x=torch.empty(5,3)
print(x)
Create an initialized matrix
x=torch.rand(5,3)
print(x)
Create a matrix of all 0s and specify the data element type as long
x=torch.zeros(5,3,dtype=torch.long)
print(x)
Create tensors directly from data
x=torch.tensor([2,5,3,5])
print(x)
Create a new tensor of the same size from an existing tensor
x=x.new_ones(5,3,dtype=torch.double)
print(x)
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)
Use the .size() method to get the shape of the tensor
print(x.size())
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)
Notice
slice operation
x[:,1]
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()
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())
Conversion between torch tensor and numpy array
a=torch.ones(5)
b=a.numpy()
a.add_(1)
print(a,b)
import numpy as np
a=np.ones(5)
b=torch.from_numpy(a)
np.add(a,1,out=a)
print(a,b)
Notice
About cuda tensor: tensor can be moved to any device with the .to() method
windows
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))