首页 > 其他分享 >pytorch-tensor高级OP

pytorch-tensor高级OP

时间:2023-07-31 23:23:33浏览次数:44  
标签:10 end tensor torch label pytorch bmatrix OP

Tensor advanced operation
▪ Where
▪ Gather

where

返回的最终的tensor中的每一个值有可能来着A,也有可能来自B。

torch.where(condition,A,B)->tensor
满足condition条件则该位置为A[..],否则为B[..]。
这个condition也是一个相同shape的tensor
比如说:torch.where(cond>0,a,b)
\( \begin{bmatrix} 1 & 0 \\ 0 & 1 \\ \end{bmatrix} \) =>\( \begin{bmatrix} A & B \\ A & B \\ \end{bmatrix} \)

image

cond=torch.tensor([[0.6769,0.7271],[0.8884,0.4163]])
cond
# tensor([[0.6769, 0.7271],
#         [0.8884, 0.4163]])



a=torch.ones(2,2)
a
# tensor([[1., 1.],
#         [1., 1.]])



b=torch.zeros(2,2)
b
# tensor([[0., 0.],
#         [0., 0.]])



torch.where(cond>0.5,a,b)
# tensor([[1., 1.],
#         [1., 0.]])

image

gather

image
这个API设计的初衷就是这样的,下面有一个场景。
下面有三类动物,编号分别为0,1,2

\[\begin{bmatrix} dog \\ cat \\ pig \\ \end{bmatrix} \begin{matrix} 0 \\ 1 \\ 2 \end{matrix} \]

然后我们识别之后的结果是一些编号,然后我们希望将这个结果编号变为类别

\[\begin{bmatrix} 1 \\ 0 \\ 1 \\ 2 \end{bmatrix} => \begin{bmatrix} cat \\ dog \\ cat \\ pig \end{bmatrix} \]

所以这个API中input和index都是tensor。
然后我们看一个具体的例子:
image

idx
# tensor([[8, 2, 0],
#         [4, 8, 1],
#         [0, 4, 9],
#         [0, 9, 2]])

label
# tensor([100, 101, 102, 103, 104, 105, 106, 107, 108, 109])

torch.gather(label.expand(4,10),dim=1,index=idx.long())
# tensor([[108, 102, 100],
#         [104, 108, 101],
#         [100, 104, 109],
#         [100, 109, 102]])

我们来分析一下torch.gather(label.expand(4,10),dim=1,index=idx.long())
首先这个label.expand(4,10)是这样的
image
我们来看看这个label.expand(4,10),是将input的shape变为(4,10),然后idx的每一行都可以按照label变化之后每一行的下标输出了,所以这个dim=1,就是按照10这个下标输出的。

标签:10,end,tensor,torch,label,pytorch,bmatrix,OP
From: https://www.cnblogs.com/lipu123/p/17595116.html

相关文章

  • opencv-python 边缘提取
    边缘时像素值发生跃迁的位置,是图像的显著特征之一。在图像特征提取,对象检测,模式识别等方面有重要作用。1sobel(索贝尔)算子sobel算子对图像求一阶导数。一阶导数越大,说明像素在该方向的变化越大,边缘信号越强。因为图像的灰度值都是离散的数字,sobel算子采用离散差分算子计算图像......
  • DevOps之Docker的安装
    一、Docker安装安装所需的软件包yuminstall-yyum-utilsdevice-mapper-persistent-datalvm2添加Docker的YUM存储库yum-config-manager--add-repohttps://download.docker.com/linux/centos/docker-ce.repo安装DockerCEyuminstalldocker-ce查看安装版本......
  • Exsi 安装OpenWRT并使用gparted扩容
    Exsi安装OpenWRT并使用gparted扩容所有操作均基于MACOS参考资料下载OpenWRT镜像镜像库地址:https://mirror.sjtu.edu.cn/openwrt/releases/[VERSION_ID]/targets/x86/64/22.03.5下载地址cd~\Downloadswgethttps://mirror.sjtu.edu.cn/openwrt/releases/22.03.5/targ......
  • pytorch-tensor属性统计(norm,max,min...)
    statistics▪norm(范数)▪mean,sum(平均值,求和)▪prod(累乘)▪max,min,argmin,argmax▪kthvalue,topk(第k大)norm(范式)这里面有一范式和二范式。一范式:\[||x||_1=\sum_k|x_k|\]二范式:\[||x||_1=\sqrt{\sum_k{x_k^2}}\]a.norm(k,dim)这个dim,可以不填,不填就是......
  • 使用OpenFeign传递二进制流
    在现代的分布式系统中,服务之间的通信变得越来越普遍。OpenFeign是一个流行的JavaHTTP客户端工具,它简化了在微服务架构中进行服务间通信的过程,本文将简单介绍如何使用OpenFeign传递二进制流。什么是OpenFeign?OpenFeign是一个用于声明式、模板化的HTTP客户端的Java库。它简化了编......
  • openGauss数据库常用操作命令
    1.以操作系统用户omm登录数据库主节点su-omm1.1启动服务分布式openGauss:gs_om-tstart启动服务gs_om-trestart重启服务集中式openGauss:gs_om-tstop关闭服务gs_om-tstart启动服务1.2使用“gs_om-tstatus–detail”命令查询openGauss各实例状......
  • Hadoop集群相关理解
    Hadoop集群简介Hadoop集群模式安装安装包获得Hadoop安装包、源码包下载地址:https://archive.apache.org/dist/hadoop/common/hadoop-3.3.0/进入到这样一个界面:源码包找这个:官方编译安装包找这个:一般情况下,下载官方编译安装包即可!安装完成后,就需要进行下面的几个步骤:......
  • PyTorch 中的多 GPU 训练和梯度累积作为替代方案
    动动发财的小手,点个赞吧!在本文中,我们将首先了解数据并行(DP)和分布式数据并行(DDP)算法之间的差异,然后我们将解释什么是梯度累积(GA),最后展示DDP和GA在PyTorch中的实现方式以及它们如何导致相同的结果。简介训练深度神经网络(DNN)时,一个重要的超参数是批量大小。通常,batchsi......
  • error while loading shared libraries: libxml2.so.2: cannot open shared object fi
    参考:https://blog.csdn.net/qq_39779233/article/details/128215517  ============================================   errorwhileloadingsharedlibraries:libxml2.so.2:cannotopensharedobjectfile解决方法  解决方法:sudoapt-getinstalllibxml......
  • centos7中 configure: error: zlib development files not found
     01、系统[liujiaxin01@PC1~]$cat/etc/redhat-release##centos7.6发行版CentOSLinuxrelease7.6.1810(Core) 002、问题[[email protected]]$./configure##出现如下报错 003、解决方法[root@PC1~]#yum-yinstallzlibzlib......