pytorch基础总结01

pytorch是用于代替numpy的计算库,numpy 唯一不同的是pytorch是可以使用gpu进行计算
基本上numpy中的所有特性和方法在pytorch中都可以找到
这里用于验证pytorch中的基本数据类型
1)tensor张量是pytorch中的核心的数据类型
  tensor有5种基本数据类型–这5种类型可以用来之间创建tensor
     torch.FloatTensor 默认的数据类型,32位浮点数,torch.tensor默认就是这种类型
torch.LongTensor   长整形64
torch.ShortTensor   短整形32
torch.IntTensor    整形16位
torch.DoubleTensor  双精度浮点数64位
事实上,pytorch有8中dtype:
------------------------------------------------------------------------
数据类型 | dtype |Tensor 类型
------------------------------------------------------------------------
16位浮点数 | torch.float16、torch.half |torch.HalfTensor
32位浮点数 | torch.float32、torch.float(default) |torch.FloatTensor
64位浮点数 | torch.float64、torch.double |torch.DoubleTensor
8位无符号整数 | torch.uint8 |torch.ByteTensor
8位有符号整数 | torch.int8 |torch.CharTensor
16位有符号整数 | torch.int16、torch.short |torch.ShortTensor
32位有符号整数 | torch.int32、torch.int |torch.IntTensor
64位有符号整数 | torch.int64、torch.long |torch.LongTensor
-------------------------------------------------------------------------
2)变量Variable是pytorch中的一个功能强大的数据类型,相当于在tensor外面套了一个壳,
赋予了前向传播、反向传播、自动求导等功能,需要从torch.autograd导入
Variable有两个属性,grad和data,其中,grad是用来记录梯度,data就是tensor本身

  tensor转Variable    Variable(tensor)
  Variable转tensor    variable.data
  numpy转tensor       torch.from_numpy(ndarray)
  tensor装numpy       tensor.numpy()

3)tensor有三个属性:dtype, device, layout
dtype就是数据类型,8种
device代表tensor被分配的设备是CPU(‘cpu’)还是GPU(‘cuda’)
layout代表的是tensor的内存分布,暂时都为torch.strided
下面讲如何给tensor分配设备以及如何在 cpu和gpu之间转换
查看cuda的信息: torch.cuda.is_available()
torch.cuda.device_count()
torch.cude.get_device_name(0)
torch.cuda.current_device()
给tensor分配设备: pytorch中默认分配的设备为cpu
直接分配 tensor1.cuda()
torch.tensor([1,2],device=‘cuda’)
torch.tensor([1,2],device=‘cuda:0’)
torch.tensor([1,2],device=‘cpu’)
使用’to’来转换
device_cpu = torch.device(‘cpu’)
device_cuda = torch.device(‘cuda’)
data = torch.tensor([1,2,3])
data.to(device_cpu)
data.to(device_cuda)
4)创建tensor的总结:
@1 直接创建: torch.tensor(data,dtype=None,device=None,requires_grad=False)
torch.FloatTensor(5,3)
torch.Tensor(5,3,dtype=torch.int8)
@2 从Numpy中创建: torch.from_numpy()
@3 创建指定数值的矩阵 torch.zeros(size)
torch.zeros_like(input)
torch.ones() torch.ones_like()
torch.full(size,fill_value)
torch.full_like(input,fill_value)
torch.arange(start,end,step)
torch.linspace(start,end,step)
torch.logspace(start,end,step)
@4 创建指定类型的矩阵 torch.eye(n,m=None)
torch.empty(size)
torch.empty_like(input)
@5 随机生成
torch.normal(mean,std) #离散正态分布
torch.randn(size) #标准正态分布
torch.rand(size) #返回[0-1] 之间均匀分布的随机值
torch.rand_like(input) #返回[0-1] 之间均匀分布的随机值
torch.ranint(low=0,high,size) #返回[low,high]之间均匀分布的随机值
torch.ranint_like(input,low=0,high) #返回[low,high]之间均匀分布的随机值
torch.randperm(n) #返回的是0到n-1之间的随机排列
5)tensor的基本操作:
@1 拼接和叠加操作
torch.cat(seq,dim=0) #沿dim方向,拼接
torch.stack(seq,dim=0) #沿dim方向叠加
torch.gather(input,dim,index) #沿着dim方向,在index的指导下,在原tensor-input中收集元素产生新的额tensor
out[i][j][k] = input[ index[i][j][k] ][j][k] #dim=0
out[i][j][k] = input[i][ index[i][j][k] ][k] #dim=1
out[i][j][k] = input[i][j][ index[i][j][k] ] #dim=2
@2 拆分操作
torch.split(input,split_size_or_selections,dim=0) #沿dim方向将input切割位大小为split_size大小的快
torch.chuck(input,chucks,dim=0) #沿着dim方向拆分成chucks个组块
@3 索引操作
torch.index_select(input,dim,index) #index必须是LongTensor,即64位长整形
torch.masked_select(input,mask) #mask必须是ByteTensor,即8位无符号整形
@4 其他操作
torch.transpose(input,dim0,dim1)
torch.t(input)
torch.squeeze(input,dim)
torch.unsqueeze(input,dim)
torch.reshape(input,shape)
torch.where(condition,x,y)
torch.unbind(tensor,dim)
torch.nonzero(input)
6)tensor的运算相关的操作
@1 三角函数
torch.sin() torch.sinh() torch.arcsin()
torch.cos() torch.cosh() torch.arccos()
torch.tan() torch.tanh() torch.arctan() torch.arctan2()
torch.abs()
@2 算术运算
torch.add(input,value) #input + value
torch.add(input,value,other) # other * value + input
torch.addcdiv(input,value,tensor1,tensor2) # (tensor1 / tensor2) * value + input
torch.addcmul(input,value,tensor1,tensor2) # tensor1 * tensor2 * value + input
torch.div(input,value)
torch.div(input,other)
torch.mul(input,value)
torch.mul(input,other)
@3 对数-幂函数
torch.log(input)
torch.log1p(input)
torch.log2(input)
torch.log10(input)
@4 截断函数
torch.ceil(input) #向上取整
torch.floor(input) #向下取整
torch.round(input) #四舍五入
torch.trunc(input) #整数部分
torch.frac(input) #小数部分
torch.fmod(input,divisor) #返回input / divisor 的余数部分
torch.remainder(input,divisor) #和fmod一样
@5 降维
torch.argmax()
torch.max()
torch.argmin()
torch.min()
torch.cumprod(input,dim) #指定维度上连乘 y_i = y_1 * y_2 * y3 y_i
torch.cumsum(input,dim) #指定维度上连加 y_i = y_1 + y_2 + y3 +…+ y_i
torch.prod()
torch.mean()
torch.sum()
torch.median()
torch.mode()
torch.std()
torch.var()
@6 对比
torch.eq(input,other)
torch.equal(tensor1,tensor2)
torch.ge(intput,other)
torch.gt(intput,other)
torch.le(intput,other)
torch.lt(intput,other)
torch.ne(intput,other)
torch.max()
torch.min()
torch.sort()
torch.isnan()
torch.topk()#返回指定维度上的最大的k个元素及其索引
torch.kthvalue()#返回指定维度上最小的k个元素及其索引
@7 其他
torch.dot(tensor1,tensor2)
torch.cross(input,other,din=-1)
torch.mm(mat1,mat2)
torch.eig(a)
torch.det(A)
torch.trace(input)
torch.histic(input,bins=100,min=0,max=0)
tensor.item()
torch.is_tensor()
torch.is_storage()

‘’’

猜你喜欢

转载自blog.csdn.net/weixin_44493916/article/details/89749894