torch 的 dim 和 numpy 的axis 表示方向不同

1.  torch中以index_select为例子

torch.index_select(input, dim, index, out=None)
- 功能:在维度dim上,按index索引数据
- 返回值:依index索引数据拼接的张量
    - index:要索引的张量
    - dim:要索引的维度
    - index:要索引数据的序号

x = torch.randn(3, 4)
print(x)
indices = torch.tensor([0, 2])
torch.index_select(x, 1, indices)



#把1改为0
y = torch.randn(3, 4)
print(y)
indices = torch.tensor([0, 2])
torch.index_select(y, 0, indices)

输出如下,可以看出,dim=1时按照列索引;dim=0时,按照行索引

tensor([[ 1.9626,  0.1007, -1.2005,  1.2650],
        [ 0.3603,  0.6343, -0.6197,  0.5740],
        [-0.0798,  0.9674, -0.7761,  0.5552]])
tensor([[ 1.9626, -1.2005],
        [ 0.3603, -0.6197],
        [-0.0798, -0.7761]])


tensor([[ 0.2274, -2.1934, -0.3129,  0.3869],
        [ 0.3831, -0.7156, -1.0765, -2.1098],
        [-0.8007, -0.0095,  0.8703, -0.8797]])
tensor([[ 0.2274, -2.1934, -0.3129,  0.3869],
        [-0.8007, -0.0095,  0.8703, -0.8797]])

2.numpy 中 以mean为例

x = numpy.random.randint(1,10,(3,4))
print(x)
print(x.mean(0))


y = numpy.random.randint(1,10,(3,4))
print(y)
print(y.mean(1))

输出如下,axis = 0时,按照竖直方向从上往下计算均值,输出4个数;axis=1时,按照水平方向从左往右计算均值,输出三个数。

[[6 8 4 9]
 [7 5 9 3]
 [1 7 6 1]]
[4.66666667 6.66666667 6.33333333 4.33333333]


[[3 3 6 5]
 [4 3 1 5]
 [7 2 2 5]]
[4.25 3.25 4.  ]

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wym_king

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值