tianshou: Batch: don't create new objects on getitem

Currently Batch.__getitem__ will always create a new object. This is counterintuitive and destroys equality checks. E.g.,

b = Batch(...)
id1 = id(b[0]) 
id2 = id(b[0])

will result in id1 != id2, which leads to b[0] == b[0] being False

Related to #922

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 15 (7 by maintainers)

Commits related to this issue

Most upvoted comments

Yes, seems to be quite involving at this point. I wonder how torch is able to do it: Torch an numpy can provide “views” of the arrays, so the id is the same.

As I found out just now, python’s own list actually cannot do this, so

l = [1, 2, 3]
id(l[:2]) == id(l[:2])
>>> False

Since Batch is more of a python object than an array, it’s fine if we don’t do better than list 😃. Let’s just implement __eq__ properly and resolve this issue

In the last months I implemented a lot of helper things that also could help with this issue. Gonna open a PR tomorrow and assign you two as reviewers