我通过使用以下tensorlayer命令创建了npy
数据集。
tl.files.save_any_to_npy(
save_dict={
'images': aggregated_images,
'actions': aggregated_actions,
'rewards': aggregated_rewards},
name='./data/episode0.npy')
我能够通过以下方式加载文件(奖励/动作是标量数组;图像是矩阵数组)
import numpy as np
data = np.load('./data/episode0.npy')
我认为这类似于字典(print(data)
的作品)。因此,我尝试了
actions = data['actions']
但这给了我以下错误
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
如何解决此错误?我想我可以使用三个变量来解决,但我只想跟踪所有文件。
解决方案(贷记给Goyo):
import tensorlayer as tl data = tl.files.load_npy_to_any(path='./data', name='episode0.npy') actions = data['actions']
答案 0 :(得分:2)
尝试一下:
data = np.load('./data/episode0.npy').item()
data["actions"]