ElegantRL icon indicating copy to clipboard operation
ElegantRL copied to clipboard

list(map()) bug

Open MMeecatfish opened this issue 2 years ago • 8 comments

注意到在PR#202中为了加快速度,将list()替换成[],但是将所有的list(map())都替换成了[map()],例如AgentBase.py第282行:

traj_list1 = [map(list, zip(*traj_list))]  # state, reward, done, action, noise

这不会引起迭代展开,而是生成了单个元素的list对象,如下:

>>> list(map(list, zip(*((1,2,3),(4,5,6)))))
[[1, 4], [2, 5], [3, 6]]
>>> [map(list, zip(*((1,2,3),(4,5,6))))]
[<map object at 0x7f96184ec910>]

MMeecatfish avatar Sep 02 '22 06:09 MMeecatfish

非常感谢您的反馈!! 这两天我们陆续收到了 多个反馈,正努力修复。 这个 PR 接收得 有点快; 另一方面,也是因为 缺少 优良的Unit Tests....补课补课。

注意到在PR#202中为了加快速度,将list()替换成[],但是将所有的list(map())都替换成了[map()],例如AgentBase.py第282行:

traj_list1 = [map(list, zip(*traj_list))]  # state, reward, done, action, noise

这不会引起迭代展开,而是生成了单个元素的list对象,如下:

>>> list(map(list, zip(*((1,2,3),(4,5,6)))))
[[1, 4], [2, 5], [3, 6]]
>>> [map(list, zip(*((1,2,3),(4,5,6))))]
[<map object at 0x7f96184ec910>]

非常感谢您的反馈!! 这两天我们陆续收到了 多个反馈,正努力修复。 这个 PR 接收得 有点快; 另一方面,也是因为 缺少 优良的Unit Tests....补课补课。

YangletLiu avatar Sep 02 '22 06:09 YangletLiu

Same problem, when will it be solved?

I have tried changing [] to list() in the line on AgentBase.py:

traj_list1 = list(map(list, zip(*traj_list)))

It doesn't work either. It gives error in this notebook image

sboal94 avatar Sep 10 '22 13:09 sboal94

把所有[map]换成list(map)就可解决。map()生成的是一个可迭代对象(iterable),直接放进方括号[]得到的list也只能迭代使用,而不能通过index索引,后面的代码用的index索引,所以报错。

jimmydengpeng avatar Sep 13 '22 08:09 jimmydengpeng

@jimmydengpeng Thank you for your contribution. I have replaced this

traj_list1 = [map(list, zip(*traj_list))] # state, reward, done, action, noise

with this

traj_list1 = list(map(list, zip(*traj_list))) # state, reward, done, action, noise

Same error

 289         traj_state = torch.stack(traj_list1[0])
    290         traj_action = torch.stack(traj_list1[3])
    291 

TypeError: stack(): argument 'tensors' (position 1) must be tuple of Tensors, not map

sboal94 avatar Sep 13 '22 17:09 sboal94

@sboal94 Do you install elegantrl with -e flag? traj_list1[0] is a map style. It seems that the modification doesn't work.

LotuSrc avatar Oct 25 '22 03:10 LotuSrc

just replace states, rewards, masks, actions = [torch.cat(item, dim=0) for item in traj_items] with [states, rewards, masks, actions] = traj_list since the samples are already shaped.

quantumiracle avatar Oct 29 '22 20:10 quantumiracle

The follow Pull request fix this bug ↓ Fix bug for vec env and agentbase init https://github.com/AI4Finance-Foundation/ElegantRL/pull/248

Yonv1943 avatar Jan 09 '23 02:01 Yonv1943

just replace states, rewards, masks, actions = [torch.cat(item, dim=0) for item in traj_items] with [states, rewards, masks, actions] = traj_list since the samples are already shaped.

Thanks very much!

YangletLiu avatar Jan 09 '23 07:01 YangletLiu