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使用paramiko实现远程拷贝文件的方法

本文实例讲述了python使用paramiko实现远程拷贝文件的方法。分享给大家供大家参考,具体如下: 首先是安装paramiko库(其实现了SSH2安全协议),ubuntu下可直接通过...

django项目简单调取百度翻译接口的方法

django项目简单调取百度翻译接口的方法

1,建路由; 2,写方法; def fanyi(request): import requests import json content = request.POST...

Python中的is和id用法分析

本文实例讲述了Python中的is和id用法。分享给大家供大家参考。具体分析如下: (ob1 is ob2) 等价于 (id(ob1) == id(ob2)) 首先id函数可以获得对象的...

Django框架中render_to_response()函数的使用方法

通常的情况是,我们一般会载入一个模板文件,然后用 Context渲染它,最后返回这个处理好的HttpResponse对象给用户。 我们已经优化了方案,使用 get_template()...

解决Python3 被PHP程序调用执行返回乱码的问题

因为有一部分程序是 Python 写的,所以需要 PHP 调用 Python 程序返回数据,使用 exec 返回的是乱码 $data = "Geek程序员" $get = exec(...