Extract elements from .npy file, convert them to PyTorch tensors
I read a .npy file that contains just the labels for images. The labels are stored in dictionary format. I need to convert this to an array of Tensors. But I'm unable to extract elements one of the other from the object the file returns, which is numpy.ndarray type.


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)])


I would like to create an array of tensors instead. 

Thanks in advance.

The below worked for me, with guidance by @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])

