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

相关文章

解决python3中cv2读取中文路径的问题

如下所示: python3: img_path =  ' ' im = cv2.imdecode(np.fromfile(img_path,dtype = np.uin...

Python中绑定与未绑定的类方法用法分析

本文实例讲述了Python中绑定与未绑定的类方法。分享给大家供大家参考,具体如下: 像函数一样,Python中的类方法也是一种对象。由于既可以通过实例也可以通过类来访问方法,所以在Pyt...

基于python 二维数组及画图的实例详解

1、二维数组取值 注:不管是二维数组,还是一维数组,数组里的数据类型要一模一样,即若是数值型,全为数值型 #二维数组 import numpy as np list1=[[1.73...

Python实现TCP通信的示例代码

使用socket实现tcp通信,需导入socket模块 1、服务端 主要步骤: (1)创建socket:socket.socket(family=AF_INET, type=SOCK_S...

查看django执行的sql语句及消耗时间的两种方法

下面介绍两种查看django 执行的sql语句的方法。 方法一: queryset = Apple.objects.all() print queryset.query SELEC...