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

相关文章

Python中的with语句与上下文管理器学习总结

0、关于上下文管理器 上下文管理器是可以在with语句中使用,拥有__enter__和__exit__方法的对象。 with manager as var: do_somethi...

对python中Matplotlib的坐标轴的坐标区间的设定实例讲解

对python中Matplotlib的坐标轴的坐标区间的设定实例讲解

如下所示: <span style="font-family: Arial, Helvetica, sans-serif;">>>> import nu...

Python安装官方whl包和tar.gz包的方法(推荐)

Windows环境:   安装whl包:pip install wheel    ->    pip install&n...

python中requests爬去网页内容出现乱码问题解决方法介绍

最近在学习python爬虫,使用requests的时候遇到了不少的问题,比如说在requests中如何使用cookies进行登录验证,这可以查看这篇文章。这篇博客要解决的问题是如何避免在...

Python基于回溯法子集树模板解决选排问题示例

Python基于回溯法子集树模板解决选排问题示例

本文实例讲述了Python基于回溯法子集树模板解决选排问题。分享给大家供大家参考,具体如下: 问题 从n个元素中挑选m个元素进行排列,每个元素最多可重复r次。其中m∈[2,n],r∈[1...