Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 具有不同维数索引数组的索引pytorch张量_Python_Numpy_Indexing_Pytorch - Fatal编程技术网

Python 具有不同维数索引数组的索引pytorch张量

Python 具有不同维数索引数组的索引pytorch张量,python,numpy,indexing,pytorch,Python,Numpy,Indexing,Pytorch,我有下面的函数,它使用numpy.array实现了我想要的功能,但是在馈送torch.Tensor时由于索引错误而中断 import torch import numpy as np def combination_matrix(arr): idxs = np.arange(len(arr)) idx = np.ix_(idxs, idxs) mesh = np.stack(np.meshgrid(idxs, idxs)) def np_combinatio

我有下面的函数,它使用
numpy.array
实现了我想要的功能,但是在馈送
torch.Tensor
时由于索引错误而中断

import torch
import numpy as np


def combination_matrix(arr):
    idxs = np.arange(len(arr))
    idx = np.ix_(idxs, idxs)
    mesh = np.stack(np.meshgrid(idxs, idxs))

    def np_combination_matrix():
        output = np.zeros((len(arr), len(arr), 2, *arr.shape[1:]), dtype=arr.dtype)
        num_dims = len(output.shape)
        output[idx] = arr[mesh].transpose((2, 1, 0, *np.arange(3, num_dims)))
        return output

    def torch_combination_matrix():
        output = torch.zeros(len(arr), len(arr), 2, *arr.shape[1:], dtype=arr.dtype)
        num_dims = len(output.shape)
        print(arr[mesh].shape)  # <-- This is wrong/different to numpy!
        output[idx] = arr[mesh].permute(2, 1, 0, *np.arange(3, num_dims))
        return output

    if isinstance(arr, np.ndarray):
        return np_combination_matrix()
    elif isinstance(arr, torch.Tensor):
        return torch_combination_matrix()
但尺寸不同:

(2, 3, 3, 3)
torch.Size([3, 3])
这会导致错误(逻辑上):

回溯(最近一次呼叫最后一次):
文件“/home/XXX/util.py”,第226行,在
火炬梳=组合矩阵(特征)
文件“/home/XXX/util.py”,第218行,组合矩阵
返回火炬组合矩阵()
文件“/home/XXX/util.py”,第212行,在torch\u组合矩阵中
输出[idx]=arr[mesh].permute(2,1,0,*np.arange(3,num_dims))
RuntimeError:排列中的DIM数不匹配
如何将火炬行为与numpy相匹配?
我在火炬论坛上读过各种各样的问题(例如),但我可以在这里找到如何应用这些问题的方法。类似地,只适用于一个维度,但我需要它至少适用于两个维度。

这实际上非常简单。您只需要展平索引,然后重塑和排列维度。 这是完整的工作版本:

import torch
import numpy as np


def combination_matrix(arr):
    idxs = np.arange(len(arr))
    idx = np.ix_(idxs, idxs)
    mesh = np.stack(np.meshgrid(idxs, idxs))

    def np_combination_matrix():
        output = np.zeros((len(arr), len(arr), 2, *arr.shape[1:]), dtype=arr.dtype)
        num_dims = len(output.shape)
        output[idx] = arr[mesh].transpose((2, 1, 0, *np.arange(3, num_dims)))
        return output

    def torch_combination_matrix():
        output_shape = (2, len(arr), len(arr), *arr.shape[1:])  # Note that this is different to numpy!
        return arr[mesh.flatten()].reshape(output_shape).permute(2, 1, 0, *range(3, len(output_shape)))

    if isinstance(arr, np.ndarray):
        return np_combination_matrix()
    elif isinstance(arr, torch.Tensor):
        return torch_combination_matrix()
我使用pytest在不同维度的随机数组上运行它,它似乎在所有情况下都有效:

import pytest

@pytest.mark.parametrize('random_dims', range(1, 5))
def test_combination_matrix(random_dims):
    dim_size = np.random.randint(1, 40, size=random_dims)
    elements = np.random.random(size=dim_size)
    np_combs = combination_matrix(elements)
    features = torch.from_numpy(elements)
    torch_combs = combination_matrix(features)

    assert np.array_equal(np_combs, torch_combs.numpy())

if __name__ == '__main__':
    pytest.main(['-x', __file__])

我试图减少您的问题:您试图索引到一个具有更大维度(可能还有更大大小)索引的数组中。我已经尝试在这里捕捉其中的一些信息:-但是,我不知道正确的解决方案是什么-抱歉@每一次我都发现它其实非常简单
import torch
import numpy as np


def combination_matrix(arr):
    idxs = np.arange(len(arr))
    idx = np.ix_(idxs, idxs)
    mesh = np.stack(np.meshgrid(idxs, idxs))

    def np_combination_matrix():
        output = np.zeros((len(arr), len(arr), 2, *arr.shape[1:]), dtype=arr.dtype)
        num_dims = len(output.shape)
        output[idx] = arr[mesh].transpose((2, 1, 0, *np.arange(3, num_dims)))
        return output

    def torch_combination_matrix():
        output_shape = (2, len(arr), len(arr), *arr.shape[1:])  # Note that this is different to numpy!
        return arr[mesh.flatten()].reshape(output_shape).permute(2, 1, 0, *range(3, len(output_shape)))

    if isinstance(arr, np.ndarray):
        return np_combination_matrix()
    elif isinstance(arr, torch.Tensor):
        return torch_combination_matrix()
import pytest

@pytest.mark.parametrize('random_dims', range(1, 5))
def test_combination_matrix(random_dims):
    dim_size = np.random.randint(1, 40, size=random_dims)
    elements = np.random.random(size=dim_size)
    np_combs = combination_matrix(elements)
    features = torch.from_numpy(elements)
    torch_combs = combination_matrix(features)

    assert np.array_equal(np_combs, torch_combs.numpy())

if __name__ == '__main__':
    pytest.main(['-x', __file__])