pytorch索引查找 index_select的例子
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的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。
相关推荐
-
在PyTorch中Tensor的查找和筛选例子
本文源码基于版本1.0,交互界面基于0.4.1 import torch 按照指定轴上的坐标进行过滤 index_select() 沿着某tensor的一个轴dim筛选若干个坐标 >>> x = torch.randn(3, 4) # 目标矩阵 >>> x tensor([[ 0.1427, 0.0231, -0.5414, -1.0009], [-0.4664, 0.2647, -0.1228, -1.1068], [-1.1734, -0.6571, 0.7230,
-
Pytorch Tensor的索引与切片例子
1. Pytorch风格的索引 根据Tensor的shape,从前往后索引,依次在每个维度上做索引. 示例代码: import torch a = torch.rand(4, 3, 28, 28) print(a[0].shape) #取到第一个维度 print(a[0, 0].shape) # 取到二个维度 print(a[1, 2, 2, 4]) # 具体到某个元素 上述代码创建了一个shape=[4, 3, 28, 28]的Tensor,我们可以理解为4张图片,每张图片有3个通道,每个通道
-
pytorch索引查找 index_select的例子
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.Lo
-
pytorch中index_select()的用法详解
pytorch中index_select()的用法 index_select(input, dim, index) 功能:在指定的维度dim上选取数据,不如选取某些行,列 参数介绍 第一个参数input是要索引查找的对象 第二个参数dim是要查找的维度,因为通常情况下我们使用的都是二维张量,所以可以简单的记忆: 0代表行,1代表列 第三个参数index是你要索引的序列,它是一个tensor对象 刚开始学习pytorch,遇到了index_select(),一开始不太明白几个参数的意思,后来查了一
-
pytorch 模型可视化的例子
如下所示: 一. visualize.py from graphviz import Digraph import torch from torch.autograd import Variable def make_dot(var, params=None): """ Produces Graphviz representation of PyTorch autograd graph Blue nodes are the Variables that require gra
-
Pytorch中index_select() 函数的实现理解
函数形式: index_select( dim, index ) 参数: dim:表示从第几维挑选数据,类型为int值: index:表示从第一个参数维度中的哪个位置挑选数据,类型为torch.Tensor类的实例: 刚开始学习pytorch,遇到了index_select(),一开始不太明白几个参数的意思,后来查了一下资料,算是明白了一点. a = torch.linspace(1, 12, steps=12).view(3, 4) print(a) b = torch.index_selec
-
Pytorch 实现自定义参数层的例子
注意,一般官方接口都带有可导功能,如果你实现的层不具有可导功能,就需要自己实现梯度的反向传递. 官方Linear层: class Linear(Module): def __init__(self, in_features, out_features, bias=True): super(Linear, self).__init__() self.in_features = in_features self.out_features = out_features self.weight = Pa
-
获取Pytorch中间某一层权重或者特征的例子
问题:训练好的网络模型想知道中间某一层的权重或者看看中间某一层的特征,如何处理呢? 1.获取某一层权重,并保存到excel中; 以resnet18为例说明: import torch import pandas as pd import numpy as np import torchvision.models as models resnet18 = models.resnet18(pretrained=True) parm={} for name,parameters in resnet18
-
在pytorch中查看可训练参数的例子
pytorch中我们有时候可能需要设定某些变量是参与训练的,这时候就需要查看哪些是可训练参数,以确定这些设置是成功的. pytorch中model.parameters()函数定义如下: def parameters(self): r"""Returns an iterator over module parameters. This is typically passed to an optimizer. Yields: Parameter: module paramete
-
在pytorch中为Module和Tensor指定GPU的例子
pytorch指定GPU 在用pytorch写CNN的时候,发现一运行程序就卡住,然后cpu占用率100%,nvidia-smi 查看显卡发现并没有使用GPU.所以考虑将模型和输入数据及标签指定到gpu上. pytorch中的Tensor和Module可以指定gpu运行,并且可以指定在哪一块gpu上运行,方法非常简单,就是直接调用Tensor类和Module类中的 .cuda() 方法. import torch from PIL import Image import torch.nn as
随机推荐
- JS实现分页浏览横向图片(类轮播)实例代码
- Swift中通知中心(NotificationCenter)的使用示例
- Asp+Rss阅读器制作第1/2页
- C++设计模式之原型模式
- 用AJAX返回HTML片段中的JavaScript脚本
- ASP.NET Web页生命周期和执行的方法介绍
- asp.ent下合并两个结构相同的DataTable
- php一些错误处理的方法与技巧总结
- Android checkbox的listView具体操作方法
- PHP中加速、缓存扩展的区别和作用详解(eAccelerator、memcached、xcache、APC )
- C#实现多选项卡的浏览器控件
- Android开发之Activity详解
- asp.net网站底部的版权信息实现代码且可维护
- JS实现弹出浮动窗口(支持鼠标拖动和关闭)实例详解
- php操作access数据库的方法详解
- php实现的debug log日志操作类实例
- 可左右平滑滚动的产品展示效果
- jQuery右下角悬浮广告实例
- javascript实现的三维正方体(兼容ie7,ff)
- win2003 服务器安全配置全套详解