【PyTorch总结】PyTorch的基础使用
·
文章目录
Initializing Tensor
import torch
device = "cuda" if torch.cuda.is_available() else "cpu"
my_tensor = torch.tensor([[1,2,3],[4,5,6]],dtype=torch.float32,device=device,requires_grad=True)
print(my_tensor)
print(my_tensor.dtype)
print(my_tensor.device)
print(my_tensor.shape)
print(my_tensor.requires_grad)
How to initialize and convert tensort to other types(int, float, double)
tensor = torch.arange(4)
print(tensor.bool()) # boolean True/False
print(tensor.short()) # int 16
print(tensor.long()) # int64(Important)
print(tensor.half()) # float16
print(tensor.float()) # float32(Important)
print(tensor.double()) # float64
Array to Tensor conversion and vice-versa
import numpy as np
import torch
np_array = np.zeros((5,5))
tensor = torch.from_numpy(np_array)
np_array_back = tensor.numpy()
Math
### Math篇
import torch
x = torch.tensor([1,2,3])
y = torch.tensor([9,8,7])
# Addition
z1 = torch.empty(3)
torch.add(x,y,out=z1)
z2 = torch.add(x,y)
z = x+y
# Substraction
z = x-y
# Division
z = torch.true_divide(x,y) # torch1.6 版本以上
z = torch.floor_divide(y,x) # torch1.6 版本以上
# inplace operations
t = torch.zeros(3)
t.add_(x)
t += x
# Exponentiation
z = x.pow(2)
z = x ** 2
# Simple comparison
z = x > 0
z = x < 0
# Matrix Mulitiplication
x1 = torch.rand(2,5)
x2 = torch.rand(5,3)
x3 = torch.mm(x1,x2)
x3 = x1.mm(x2)
# Matrix exponentiation
matrix_exp = torch.rand(5,5)
z = matrix_exp.matrix_power(3)
# element wise mult
z = x*y
# dot product
z = torch.dot(x,y)
# Batch Matrix Multiplication
batch = 32
n = 10
m = 20
p = 30
tensor1 = torch.rand((batch,n,m))
tensor2 = torch.rand((batch,m,p))
out_bmm = torch.bmm(tensor1,tensor2)
# Example of Broadcastiong
x1 = torch.rand((5,5))
x2 = torch.rand((1,5))
z = x1 - x2
# Other torch useful tensor operations
x = torch.tensor([[1,5,3],[4,2,6]])
sum_x = torch.sum(x,dim=0)
values, indices = torch.max(x,dim=1) # -> x.max(dim=0)
z = torch.argmax(x,dim=1) # 相比上面的情况只返回indices
values, indices = torch.min(x,dim=1)
z = torch.argmin(x,dim=1) # 相比上面的情况只返回indices
abx_x = torch.abs(x)
mean_x = torch.mean(x.float(),dim=0)
z = torch.eq(x,y)
sorted_x, indices = torch.sort(x,dim=0,descending=True)
z = torch.clamp(x,min=0,max=10)
z = torch.tensor([1,0,1,1,1],dtype=torch.bool)
z = torch.any(x)
z = torch.all(x)
Tensor Indexing
# Tensor Indexing
import torch
batch_size = 10
features = 25
x = torch.rand((batch_size,features))
# print(x)
# print(x[0].shape) #-> x[0,:]
# print(x[:,0].shape)
# print(x.view(x.size(0),-1))
# print(x[0:2,0:10]) # 0-2 row 0-10 colum
# Fancy indexing
x = torch.arange(10)
indices = [2,5,8]
# print(x[indices])
x = torch.rand((3,5))
rows = torch.tensor([1,0])
cols = torch.tensor([4,0])
# print(x)
# print(x[rows,cols])
# More advanced indexing
x = torch.arange(10)
print(x[(x < 2) | (x > 8)])
print(x[x.remainder(2)==0])
# Useful operations
print(torch.where(x > 5, x, x*2)) # 如果x>5,执行x,否则输出x*2
print(torch.tensor([0,0,1,2,2,3,4]).unique())
print(x.ndimension())
print(x.numel()) # tensor元素数量
Tensor Reshaping
# Tensor Reshaping
import torch
x = torch.arange(9)
x_3x3 = x.view(3,3)
# print(x_3x3)
x_3x3 = x.reshape(3,3)
y = x_3x3.t()
# print(y.contiguous().view(9))
x1 = torch.rand(2,5)
x2 = torch.rand(2,5)
# print(x1)
# print(x2)
# print(torch.cat((x1,x2),dim=0))
# print(torch.cat((x1,x2),dim=1))
z = x1.view(-1)
# print(z.shape)
batch = 64
x = torch.rand((batch,3,5))
z = x.view(batch,-1)
# print(z.shape)
z = x.permute(0,2,1)
# print(z.shape)
x = torch.arange(10)
print(x.unsqueeze(0).shape)
print(x.unsqueeze(1).shape)
更多推荐


所有评论(0)