pytorch索引查找 index_select的例子

yipeiwu_com5年前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设计】。

相关文章

python数字图像处理实现直方图与均衡化

python数字图像处理实现直方图与均衡化

在图像处理中,直方图是非常重要,也是非常有用的一个处理要素。 在skimage库中对直方图的处理,是放在exposure这个模块中。 1、计算直方图 函数:skimage.exposur...

python3序列化与反序列化用法实例

本文实例讲述了python3序列化与反序列化用法。分享给大家供大家参考。具体如下: #coding=utf-8 import pickle aa={} aa["title"]="我是...

python2 与python3的print区别小结

在Python2和Python3中都提供print()方法来打印信息,但两个版本间的print稍微有差异 主要体现在以下几个方面: 1.python3中print是一个内置函数,有多个参...

python读取几个G的csv文件方法

如下所示: import pandas as pd file = pd.read_csv('file.csv',iterator=True) while True: chunk...

Python面向对象进阶学习

Python面向对象进阶学习

在前面的章节我们已经了解了面向对象的入门知识,知道了如何定义类,如何创建对象以及如何给对象发消息。为了能够更好的使用面向对象编程思想进行程序开发,我们还需要对Python中的面向对象编程...