2

I have created a npydataset by using the following tensorlayer command.

tl.files.save_any_to_npy(
save_dict={
    'images': aggregated_images, 
    'actions': aggregated_actions,
    'rewards': aggregated_rewards}, 
    name='./data/episode0.npy')

I am able to load the file (rewards/actions are arrays of scalars; images is an array of matrices) by using

import numpy as np
data = np.load('./data/episode0.npy')

I thought this would be similar to a dictionary (print(data) works). Hence, I tried

actions = data['actions'] 

but this gives me the following error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
>>> actions = data['rewards']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

How can I resolve this error? I think I could use three variables to have a workaround, but I would rather like to only keep track of one file with all the.

Solution (credit goes to Goyo):

import tensorlayer as tl
data = tl.files.load_npy_to_any(path='./data', name='episode0.npy')
actions = data['actions']
MrYouMath
  • 547
  • 2
  • 13
  • 34

1 Answers1

6

Try this:

data = np.load('./data/episode0.npy').item()
data["actions"]
Piotr
  • 2,029
  • 1
  • 13
  • 8
  • That's what `tl.files.load_npy_to_any` does - pull the single item (a dictionary) out of a 0d object dtype array. – hpaulj Dec 02 '18 at 19:42