2021-06-11 pytorch基本语法
前言
pytorch是个好东西,它与tf的区别是,tf的布署性好,而pytorch布属性稍差。何为布署性?就是在实际应用中,可以用c调用、可以用java调用、用php调用...总之,支持的语言越多,布属性越好,这其实与模型尝试、模型训练没有半毛钱关系,何况,以2017年做分水岭,之后的项目全在tf2或pytorch上运行,因此提倡用pytorch,本文对最简单的pytorch进行梳理。
1 python变量和pytorch变量对照
2 如何定义pytorch张量
【1】生成浮点数或整数变量
- 用size生成
a = torch.FloatTensor(2,3) 或
a = torch.IntTensor(2,3)
- 用list生成(list = [2,3,4,5])
a = torch.FloatTensor([2,3,4,5]) 或
a = torch.IntTensor([2,3,4,5])
【2】生成浮点均匀分布随机张量
生成浮点数2X3的矩阵:分布【0,1】均匀
a = torch.rand(2,3)
【3】生成顺序表
import torch
a = torch.range(2,8,1)
【4】生成全零张量
import torch
a = torch.zeros(2,3)
【5】张量清空
将生成一个空张量
print( torch.empty(5, 3) )
【6】张量产生
x = x.new_ones(5, 3, dtype=torch.double)
print("x={}".format(x))
【7】形状复制的随机张量
x = x.new_ones(5, 3, dtype=torch.double)
print("x={}".format(x))
x = torch.rand_like(x,dtype=torch.float)
【8】获取张量的元素值
# 获取某一个元素的值
print("x[1][1]={}".format(x[1][1]))
print("x[1][1].item()={}".format(x[1][1].item()))
【9】转换Torch tensor到numpy
a = torch.ones(6)
b = a.numpy()
【10】转换numpy到Torch tensor
a = np.ones(2)
b = torch.from_numpy(a)
【11】将本地的tensor定义到GPU内
# pytorch 直接将张量定义到cuda内存中
if torch.cuda.is_available():
device = torch.device("cuda")
y = torch.ones_like(x,device=device)
【12】直接创建一个与x大小相同的tensor,放于GPU上
print("\nx={},dtype is {}".format(x,x.dtype))
print("y=torch.ones_like(x,device=device)={},dtype is {}".format(y,y.dtype))
#z = x+y #RuntimeError: expected type torch.FloatTensor but got torch.cuda.FloatTensor一个在GPU上,一个在CPU上,无法运算
x = x.to(device) # 将x传到GPU上
print("x=x.to(device)={},dtype is {}".format(x,x.dtype))
z = x + y
print("z=x+y={},dtype is {}".format(z,z.dtype))
【13】 cpu数据和cuda数据合法性检验
x = torch.ones(5, 3, dtype=torch.double)print ( x.type() )x = x.cuda()print ( x.type() )结果:
torch.DoubleTensor
torch.cuda.DoubleTensor
3 pytorch张量其它
1.用numel获取某张量的元素个数
a = torch.randn(1,2,3,4,5)
torch.numel(a)
>>>120
再来一个例子
a = torch.zeros(4,4)
torch.numel(a)
>>>16
2.测试某张量是否定义在GPU上
用python的Isinstance函数可以测量一个变量定义在内存(cpu),或显存(gpu)
data= torch.randn(1,2,3,4,5) 定义在cpu内存上
Isinstance(data,torch.cuda.doubleTensor)
>>>False
data = data.cuda() 将data定义到显存GPU上
Isinstance(data,torch.cuda.doubleTensor)
>>>True
4 张量上的运算
1绝对值
import torch
a = torch.randn(2,3)
b = torch.abs(a)
2 张量加法
import torch
a = torch.randn(2,3)
b = torch.randn(2,3)
c = torch.add(a,b)
3 张量裁剪
具体的裁剪过程是:使用变量中的每个元素分别和裁剪的上边界及裁剪的下边界的值进行比较,如果元素的值小于裁剪的下边界的值,该元素就被重写成裁剪的下边界的值;同理,如果元素的值大于裁剪的上边界的值,该元素就被重写成裁剪的上边界的值。
import torch
a = torch.randn(2,3)
b = torch.clamp(a,-0.1,0.1)
tensor([[ 0.0251, 1.8832, 1.5243],
[-0.1365, 1.2307, 0.0640]]) #裁剪前
tensor([[ 0.0251, 0.1000, 0.1000],
[-0.1000, 0.1000, 0.0640]]) #裁剪后
4 张量的除法
import torch
a = torch.randn(2,3)
b = torch.randn(2,3)
c = torch.div(a,b)
d = torch.randn(2,3)
e = torch.div(d,10)
print(e)
5 张量的乘法
import torch
a = torch.randn(2,3)
b = torch.randn(2,3)
c = torch.mul(a,b)
d = torch.randn(2,3)
e = torch.mul(d,10)
print(e)
6 张量的幂函数
import torch
a = torch.randn(2,3)
print(a)
b = torch.pow(a,2)
print(b)
7 矩阵的张量积
pytorch乘积相当于tf的matmul
import torch
a = torch.randn(2,3)
print(a)
b = torch.randn(3,2)
print(b)
b = torch.mm(a,b)
print(b)
8 矩阵和向量的积
import torch
a = torch.randn(2,3)
print(a)
b = torch.randn(3)
print(b)
c = torch.mv(a,b)
print(c)
9 in-place方法
in-place方法就是不增加额外内存空间,在现有空间直接运算并用结果替换的方法。
y.add_(x)
x.copy_(y)
10 tensor resize/reshape
将张量形状塑变成其它形状
x = torch.rand(4,4)
y = x.view(16)
以上运算都是对张量进行修理的的运算,还有定义在张量内部运算,以后将在实例中逐步展现。
总结
以上是生活随笔为你收集整理的2021-06-11 pytorch基本语法的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 2021-05-21 深入理解SLAM技
- 下一篇: 编码调试:UnicodeDecodeEr