Pytorch自用基础知识笔记

0、导入和选择GPU

import torch
import numpy as np

device="cuda" if torch.cuda.is_available() else "cpu"

1、张量的创建,格式和运算


z1=torch.empty(1)
x2=torch.rand(5,3)
x3=torch.zeros(5,3,dtype=torch.long)
x4=torch.tensor([5,3])
print(x2,x3,x4)
print(x3.dtype)

torch.add(x2,x3,out=z1)
print(z1)


y1=torch.rand((2,3))
y2=torch.rand((3,2))
z2=torch.mm(y1,y2)  #矩阵相乘,三维张量相乘用bmm
print(z2.matrix_power(2)) #这是个啥运算?
#z2=y1*y2#这个相乘和torch.mm区别在哪
print(z2)
#torch.max(input, dim) 函数
c = torch.tensor([[1,5,62,54], [2,6,2,6], [2,65,2,6]])
print(c)
torch.max(a, 1)#索引每行的最大值

输出结果

torch.return_types.max(
values=tensor([62,  6, 65]),
indices=tensor([2, 3, 1]))

Pytorch自用基础知识笔记_第1张图片
最大最小值均值

values,indices = torch.min(t6,dim=0)
# print(values)
z12 = torch.argmax(t6,dim=0)print(z12)
z15 = torch.argmin(t6,dim=0)
mean_t6=torch.mean(t6.float(),dim=0)

钳制,any,all

tensor.clamp(t7,min=0,max=1)#钳制
tensor.any(t8)#任何元素为真,就是真
tensor.all(t8)#所有向量都为真,才是真

官网的文档更详细https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html

你可能感兴趣的:(模式识别与人工智能,pytorch,python,深度学习)