从.npy文件中提取元素,将其转换为PyTorch张量

时间:2019-04-18 22:24:40

标签: python numpy multidimensional-array deserialization pytorch

我读了一个.npy文件,其中仅包含图像标签。标签以字典格式存储。我需要将其转换为张量数组。但是我无法从文件返回的对象numpy.ndarray类型中提取元素中的另一个。


import numpy as np
data = np.load('/content/drive/My Drive/targets.npy')
print(data.item())


{0: array(5), 1: array(0), 2: array(4), 3: array(1), 4: array(9), 5: array(2), 6: array(1), 7: array(3)}

print(data[()].values())

dict_values([array(5), array(0), array(4), array(1), array(9), array(2), array(1), array(3)])

我想创建一个张量数组。

谢谢。

2 个答案:

答案 0 :(得分:1)

假设您的data是字典:

In [59]: dct = {0: np.array([5]), 1: np.array([0]), 2: np.array([4]), 
                3: np.array([1]), 4: np.array([9]), 5: np.array([2]), 
                6: np.array([1]), 7: np.array([3])}

您可以使用包裹在numpy.concatenate()中的torch.tensor()来获取张量:

In [63]: torch.tensor(np.concatenate(list(dct.values())))
Out[63]: tensor([5, 0, 4, 1, 9, 2, 1, 3])

另外,如果要将键和值都堆叠在单个2D张量中,请使用torch.cat()

# tensor with just keys
In [86]: tk = torch.tensor(list(dct.keys()))
In [87]: tk
Out[87]: tensor([0, 1, 2, 3, 4, 5, 6, 7])

# tensor with just values
In [88]: tv = torch.tensor(np.concatenate(list(dct.values())))
In [89]: tv
Out[89]: tensor([5, 0, 4, 1, 9, 2, 1, 3])

# horizontally stack them into a single 2D tensor
In [85]: torch.cat((tk[:, None], tv[:, None]), dim=1)
Out[85]: 
tensor([[0, 5],
        [1, 0],
        [2, 4],
        [3, 1],
        [4, 9],
        [5, 2],
        [6, 1],
        [7, 3]])

经过一系列评论,我现在已经了解了您的问题,这是解决问题的方法:

In [48]: data_item = {0: np.array(5), 1: np.array(0), 2: np.array(4), 
                      3: np.array(1), 4: np.array(9), 5: np.array(2),
                      6: np.array(1), 7: np.array(3)}

# convert keys to an 1D tensor
In [53]: tk = torch.tensor(list(data_item.keys()))

In [54]: tk
Out[54]: tensor([0, 1, 2, 3, 4, 5, 6, 7])

由于您将值包含为0D数组(即标量),因此我们需要从中提取元素。为此,我们可以在lambda旁边使用map函数,其作用是将lambda函数应用于可迭代对象(此处为data_item.values()),并为我们提供元素。这些可以传递给torch.tensor以获得所需的一维张量。

# convert values to an 1D tensor
In [57]: tv = torch.tensor(list(map(lambda a: a.item(), data_item.values())))

In [58]: tv
Out[58]: tensor([5, 0, 4, 1, 9, 2, 1, 3])

# horizontally stack them into a single 2D tensor, if needed
In [85]: torch.cat((tk[:, None], tv[:, None]), dim=1)
Out[85]: 
tensor([[0, 5],
        [1, 0],
        [2, 4],
        [3, 1],
        [4, 9],
        [5, 2],
        [6, 1],
        [7, 3]])

答案 1 :(得分:0)

以下内容对我有用,在@ kmario23的指导下

import numpy as np
data = np.load('/content/drive/My Drive/targets.npy')
print(data.item())

{0: array(5), 1: array(0), 2: array(4), 3: array(1), 4: array(9), 5: array(2), 6: array(1), 7: array(3)}
# data is a 0-d numpy.ndarray that contains a dictionary. 

print(list(data[()].values()))

[array(5),
 array(0),
 array(4),
 array(1),
 array(9),
 array(2),
 array(1),
 array(3),
 array(1),
 array(4),
 array(3)]

# torch.Tensor(5) gives tensor([2.0581e-35, 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00])
# torch.tensor(5) gives 5
# unsure of why the difference exists..

Labels = torch.stack([torch.tensor(i) for i in list_of_labels_array_form])

print(Labels)

tensor([5, 0, 4,  ..., 2, 5, 0])