当前位置: 首页 > news >正文

pytorch 使用tensor混合:进行index操作

(Pdb) tmp = torch.randn(3,5)
(Pdb) indx = torch.tensor([1,0]).long()
(Pdb) temp(indx)
*** NameError: name ‘temp’ is not defined
(Pdb) tmp(indx)
*** TypeError: ‘Tensor’ object is not callable
(Pdb) tmp[indx]
tensor([[ 0.1633, 0.9389, 1.2806, -0.2525, 0.2817],
[ 0.6204, 0.5973, -1.7741, 0.3721, -0.5338]])
(Pdb) tmp
tensor([[ 0.6204, 0.5973, -1.7741, 0.3721, -0.5338],
[ 0.1633, 0.9389, 1.2806, -0.2525, 0.2817],
[ 0.4279, -0.2156, 2.4653, 0.3173, -0.0719]])
(Pdb) indx
tensor([1, 0])
(Pdb) indx2= torch.tensor([[1,0]]).long()
(Pdb) index2
*** NameError: name ‘index2’ is not defined
(Pdb) indx2
tensor([[1, 0]])
(Pdb) indx2.shape
torch.Size([1, 2])
(Pdb) tmp[indx2]
tensor([[[ 0.1633, 0.9389, 1.2806, -0.2525, 0.2817],
[ 0.6204, 0.5973, -1.7741, 0.3721, -0.5338]]])
(Pdb) tmp[indx2].shape
torch.Size([1, 2, 5])
(Pdb) tmp[:,indx2].shape
torch.Size([3, 1, 2])
(Pdb) tmp[:,indx2]
tensor([[[ 0.5973, 0.6204]],
[[ 0.9389, 0.1633]],
[[-0.2156, 0.4279]]])

(Pdb) t=torch.arange(10)
(Pdb) t
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
(Pdb) sp=torch.randn(200,300)
(Pdb) sp[:,t:t+3]
*** TypeError: only integer tensors of a single element can be converted to an index
(Pdb) t:t+3
(Pdb) print(t:t+3)
*** SyntaxError: invalid syntax
(Pdb) sp[:,t]
tensor([[ 0.8808, 1.0081, -1.0811, …, 1.8065, 0.8156, 1.3579],
[-0.5902, 0.0588, 0.4373, …, -1.4304, -0.4398, -1.6143],
[-0.9567, -0.6260, 0.8671, …, 0.3011, -0.1352, -1.7263],
…,
[ 0.7137, -0.0028, -1.8195, …, 1.0860, 0.5901, -0.9922],
[-0.9040, -0.2345, -1.1723, …, -1.3859, -1.2003, -0.3777],
[-0.2986, -0.6276, -0.5059, …, 2.4101, 0.0195, -1.7069]])
(Pdb) t2=torch.arange(2,12)
(Pdb) tw
*** NameError: name ‘tw’ is not defined
(Pdb) t2
tensor([ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
(Pdb) t3 = torch.cat(t,t2)
*** TypeError: cat() received an invalid combination of arguments - got (Tensor, Tensor), but expected one of:

  • (tuple of Tensors tensors, int dim, *, Tensor out)
  • (tuple of Tensors tensors, name dim, *, Tensor out)
    (Pdb) t3 = torch.cat([t,t2])
    (Pdb) t3
    tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5, 6, 7, 8, 9,
    10, 11])
    (Pdb) t3=torch.hstack([t,t2])
    (Pdb) t3
    tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5, 6, 7, 8, 9,
    10, 11])
    (Pdb) t3=torch.vstack([t,t2])
    (Pdb) t3
    tensor([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
    [ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]])
    (Pdb) t3.shape
    torch.Size([2, 10])
    (Pdb) sp[:,t3]
    tensor([[[ 0.8808, 1.0081, -1.0811, …, 1.8065, 0.8156, 1.3579],
    [-1.0811, 0.0215, -0.0749, …, 1.3579, 0.5030, -0.6049]],
    [[-0.5902, 0.0588, 0.4373, …, -1.4304, -0.4398, -1.6143],
    [ 0.4373, -0.4506, 1.2061, …, -1.6143, 1.0399, -0.9514]],
    [[-0.9567, -0.6260, 0.8671, …, 0.3011, -0.1352, -1.7263],
    [ 0.8671, -0.2235, -0.1658, …, -1.7263, -0.3991, -1.3480]],
    …,[[ 0.7137, -0.0028, -1.8195, …, 1.0860, 0.5901, -0.9922],
    [-1.8195, 0.7648, -1.2249, …, -0.9922, -1.9712, 1.7941]],
    [[-0.9040, -0.2345, -1.1723, …, -1.3859, -1.2003, -0.3777],
    [-1.1723, -0.8177, -0.5682, …, -0.3777, 0.6858, 0.0616]],
    [[-0.2986, -0.6276, -0.5059, …, 2.4101, 0.0195, -1.7069],
    [-0.5059, 0.3183, -1.1891, …, -1.7069, 0.6224, 0.2936]]])
    (Pdb) sp[:,t3].shape
    torch.Size([200, 2, 10])

还是很方便的

http://www.lryc.cn/news/366179.html

相关文章:

  • Threejs(WebGL)绘制线段优化:Shader修改gl.LINES模式为gl.LINE_STRIP
  • 继承-进阶
  • 探索k8s集群的配置资源(secret和configmap)
  • 如何设置vue3项目中默认的背景为白色
  • MS1112驱动开发
  • K8s存储对象的使用
  • 构建自动化API数据抓取系统
  • 【Qt知识】部分QWidget属性表格
  • 【ARM64 常见汇编指令学习 19.1 -- ARM64 跳转指令 b.pl 详细介绍】
  • WWDC24即将到来,ios18放大招
  • C#中的空合并运算符与空合并赋值运算符:简化空值处理
  • 数据结构:哈夫曼树及其哈夫曼编码
  • 微信如何防止被对方拉黑删除?一招教你解决!文末附软件!
  • jar增量打包
  • 智慧医院物联网建设-统一管理物联网终端及应用
  • Debian的常用命令
  • 矩阵1-范数与二重求和的求和可交换
  • Python笔记 - *args和**kwargs
  • 微信小程序实现图片转base64
  • os和os.path模块
  • 链表题目练习----重排链表
  • 【杂记-浅谈XSS跨站脚本攻击】
  • VMware虚拟机与MobaXterm建立远程连接失败
  • mysql undolog管理
  • 【Linux】进程2——管理概念,进程概念
  • 【C++】植物大战僵尸杂交版自动存档——防闪退存档消失
  • 通过Excel,生成sql,将A表数据插入B表
  • 如何在MySQL中实现upsert:如果不存在则插入?
  • MyBatis中 set标签
  • mysql自带分页