torch.index_select函数测试

1. description

index_select 函数的方法是可以按照序号的方式随机抽取指定维度的向量,比如按照行进行采样,按照进行采样。
在这里插入图片描述

2. pytorch

  • code
import torch
import torch.nn as nn

torch.manual_seed(23224)

torch.set_printoptions(precision=3, sci_mode=False, threshold=torch.inf)

if __name__ == "__main__":
    run_code = 0
    batch_size = 4
    model_dim = 6
    a_total = batch_size * model_dim
    a_matrx = torch.arange(a_total).reshape(batch_size, model_dim)
    a_index = torch.randperm(model_dim)
    print(f"a_matrx=\n{a_matrx}")
    print(f"a_index=\n{a_index}")
    a_column = torch.index_select(input=a_matrx, dim=1, index=a_index)
    print(f"a_column=\n{a_column}")
  • result
a_matrx=
tensor([[ 0,  1,  2,  3,  4,  5],
        [ 6,  7,  8,  9, 10, 11],
        [12, 13, 14, 15, 16, 17],
        [18, 19, 20, 21, 22, 23]])
a_index=
tensor([2, 3, 5, 4, 0, 1])
a_column=
tensor([[ 2,  3,  5,  4,  0,  1],
        [ 8,  9, 11, 10,  6,  7],
        [14, 15, 17, 16, 12, 13],
        [20, 21, 23, 22, 18, 19]])
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值