gpt4 book ai didi

python - 无需 reshape 的索引 Numpy 张量

转载 作者:太空宇宙 更新时间:2023-11-03 14:14:28 26 4
gpt4 key购买 nike

我有一个形状为 (5,48,15) 的张量。我怎样才能沿着第 0 个轴访问一个元素并仍然保持 3 个维度而不需要 reshape 。例如:

x.shape                    # this is (5,48,15) 
m = x[0,:,:]
m.shape # This is (48,15)
m_new = m.reshape(1,48,15)
m_new.shape # This is now (1,48,15)

这可能不需要 reshape 吗?

最佳答案

当您使用单个整数对轴进行索引时,如 x[0, :, :],返回数组的维数会下降一维。

要保持三个维度,您可以...

  • 在索引的同时插入一个新轴:

    >>> x[None, 0, :, :].shape
    (1, 48, 15)
  • 或使用切片:

    >>> x[:1, :, :].shape
    (1, 48, 15)
  • 或者使用花哨的索引:

    >>> x[[0], :, :].shape
    (1, 48, 15)

关于python - 无需 reshape 的索引 Numpy 张量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34548921/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com