码迷,mamicode.com
首页 > 其他好文 > 详细

pytorch中index_select()的用法

时间:2020-09-03 16:44:08      阅读:50      评论:0      收藏:0      [点我收藏+]

标签:class   print   sel   参数   style   select   col   表示   ORC   

例子

a = torch.linspace(1, 12, steps=12).view(3, 4)
print(a)
b = torch.index_select(a, 0, torch.tensor([0, 2]))
print(b)
print(a.index_select(0, torch.tensor([0, 2])))
c = torch.index_select(a, 1, torch.tensor([1, 3]))
print(c)

先定义了一个tensor,这里用到了linspace和view方法。
第一个参数是索引的对象第二个参数0表示按行索引,1表示按列进行索引第三个参数是一个tensor,就是索引的序号,比如b里面tensor[0, 2]表示第0行和第2行,c里面tensor[1, 3]表示第1列和第3列。

输出结果如下:

tensor([[ 1.,  2.,  3.,  4.],
        [ 5.,  6.,  7.,  8.],
        [ 9., 10., 11., 12.]])
tensor([[ 1.,  2.,  3.,  4.],
        [ 9., 10., 11., 12.]])
tensor([[ 1.,  2.,  3.,  4.],
        [ 9., 10., 11., 12.]])
tensor([[ 2.,  4.],
        [ 6.,  8.],
        [10., 12.]])

 

pytorch中index_select()的用法

标签:class   print   sel   参数   style   select   col   表示   ORC   

原文地址:https://www.cnblogs.com/Jason66661010/p/13558272.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!