您当前的位置:首页 > IT编程 > python
| C语言 | Java | VB | VC | python | Android | TensorFlow | C++ | oracle | 学术与代码 | cnn卷积神经网络 | gnn | 图像修复 | Keras | 数据集 | Neo4j | 自然语言处理 | 深度学习 | 医学CAD | 医学影像 | 超参数 | pointnet | pytorch | 异常检测 |

自学教程:python中的torch常用tensor处理函数示例详解

51自学网 2023-07-22 10:36:07
  python
这篇教程python中的torch常用tensor处理函数示例详解写得很实用,希望能帮到您。

note 

一、tensor的创建

  • torch.tensor会复制data,不想复制可以使用torch.Tensor.detach()
  • 如果是获得numpy数组数据,可以使用torch.from_numpy(),共享内存
# 1. tensortorch.tensor(data, dtype=None, device=None,requires_grad=False)data - 可以是list, tuple, numpy array, scalar或其他类型dtype - 可以返回想要的tensor类型device - 可以指定返回的设备requires_grad - 可以指定是否进行记录图的操作,默认为False# example 1torch.tensor([[0.1, 1.2], [2.2, 3.1], [4.9, 5.2]])tensor([[ 0.1000,  1.2000],[ 2.2000,  3.1000],[ 4.9000,  5.2000]])# example 2torch.tensor([0, 1]) # Type inference on datatensor([ 0, 1])# example 3torch.tensor([[0.11111, 0.222222, 0.3333333]],dtype=torch.float64,device=torch.device(‘cuda:0')) # creates a torch.cuda.DoubleTensortensor([[ 0.1111, 0.2222, 0.3333]], dtype=torch.float64, device=‘cuda:0')torch.tensor(3.14159) # Create a scalar (zero-dimensional tensor)tensor(3.1416)torch.tensor([]) # Create an empty tensor (of size (0,))tensor([])# 2. 从numpy中获得数据torch.from_numpy(ndarry)# 3. 创建特定数值的tensortorch.zeros(*sizes, out=None, …)# 返回大小为sizes的零矩阵1torch.zeros_like(input, …) # 返回与input相同size的零矩阵torch.ones(*sizes, out=None, …) #f返回大小为sizes的单位矩阵torch.ones_like(input, …) #返回与input相同size的单位矩阵torch.full(size, fill_value, …) #返回大小为sizes,单位值为fill_value的矩阵torch.full_like(input, fill_value, …) 返回与input相同size,单位值为fill_value的矩阵torch.arange(start=0, end, step=1, …) #返回从start到end, 单位步长为step的1-d tensor.torch.linspace(start, end, steps=100, …) #返回从start到end, 间隔中的插值数目为steps的1-d tensortorch.logspace(start, end, steps=100, …) #返回1-d tensor ,从10start到10end的steps个对数间隔# 4. 随机生成torch.normal(mean, std, out=None)torch.rand(*size, out=None, dtype=None, …) #返回[0,1]之间均匀分布的随机数值torch.rand_like(input, dtype=None, …) #返回与input相同size的tensor, 填充均匀分布的随机数值torch.randint(low=0, high, size,…) #返回均匀分布的[low,high]之间的整数随机值torch.randint_like(input, low=0, high, dtype=None, …) #torch.randn(*sizes, out=None, …) #返回大小为size,由均值为0,方差为1的正态分布的随机数值torch.randn_like(input, dtype=None, …)torch.randperm(n, out=None, dtype=torch.int64) # 返回0到n-1的数列的随机排列

二、tensor的加减乘除

  • torch.mm : 用于两个矩阵(不包括向量)的乘法。如维度为(l,m)和(m,n)相乘
  • torch.bmm : 用于带batch的三维向量的乘法。如维度为(b,l,m)和(b,m,n)相乘
  • torch.mul : 用于两个同维度矩阵的逐像素点相乘(点乘)。如维度为(l,m)和(l,m)相乘
  • torch.mv: 用于矩阵和向量之间的乘法(矩阵在前,向量在后)。如维度为(l,m)和(m)相乘,结果的维度为(l)。
  • torch.matmul : 用于两个张量(后两维满足矩阵乘法的维度)相乘或者是矩阵与向量间的乘法,因为其具有广播机制(broadcasting,自动补充维度)。如维度为(b,l,m)和(b,m,n);(l,m)和(b,m,n);(b,c,l,m)和(b,c,m,n);(l,m)和(m)相乘等。【其作用包含torch.mm、torch.bmm和torch.mv】
  • @运算符 : 其作用类似于torch.matmul
  • *运算符 : 其作用类似于torch.mul
  • einsum(Einstein summation convention,即爱因斯坦求和约定)的用法:
  • c i k = ∑ j a i j b j k c_{i k}=/sum_j a_{i j} b_{j k} cik
    PyTorch模型创建与nn.Module构建
    Python随机生成迷宫游戏的代码示例
51自学网自学EXCEL、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。
京ICP备13026421号-1