pytorch索引查找 index_select的例子

yipeiwu_com6年前Python基础

index_select

anchor_w = self.FloatTensor(self.scaled_anchors).index_select(1, self.LongTensor([0]))

参数说明:index_select(x, 1, indices)

1代表维度1,即列,indices是筛选的索引序号。

例子:

import torch
 
 
x = torch.linspace(1, 12, steps=12).view(3,4)
 
print(x)
indices = torch.LongTensor([0, 2])
y = torch.index_select(x, 0, indices)
print(y)
 
z = torch.index_select(x, 1, indices)
print(z)
 
z = torch.index_select(y, 1, indices)
print(z)

结果:

tensor([[ 1.,  2.,  3.,  4.],
    [ 5.,  6.,  7.,  8.],
    [ 9., 10., 11., 12.]])
tensor([[ 1.,  2.,  3.,  4.],
    [ 9., 10., 11., 12.]])
tensor([[ 1.,  3.],
    [ 5.,  7.],
    [ 9., 11.]])
tensor([[ 1.,  3.],
    [ 9., 11.]])

以上这篇pytorch索引查找 index_select的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

django使用admin站点上传图片的实例

Django有提供文件系统支持,在Admin站点中可以轻松上传图片。使用Admin站点保存图片,需要安装Python的图片操作包 pip install Pillow 1 配置...

pip安装python库的方法总结

使用pip安装python库的几种方式 1、使用pip在线安装 1.1 安装单个package 格式如下: pip install SomePackage 示例如下: 比如:pip...

python 根据pid杀死相应进程的方法

用python语言实现根据pid杀死相应进程 kill_process.py代码如下 #! /usr/bin/python # -*- coding: utf-8 -*- impo...

python判断数字是否是超级素数幂

如果一个数字能表示成 p^q,且p是一个素数,q为大于1的正整数,则此数字就是超级素数幂。 param number: 测试该数字是否是超级素数幂 return: 如果不是就返回 F...

python GUI实现小球满屏乱跑效果

python GUI实现小球满屏乱跑效果

本文实例为大家分享了python GUI实现小球满屏乱跑效果的具体代码,供大家参考,具体内容如下 学习tkinter有一段时间了,综合运用一下,做一个类似屏保类的东西,碰到屏幕边缘就反弹...