AI4Finance-Foundation / AI4Finance-Foundation/RLSolver

🐛 find bug in TNCO env and the explain this env data struct

Đang mở
#93 2 bình luận 0 reaction 0 người được giao Xem trên GitHub
bug
Ngôn ngữ chính
Python
Star
169
Fork
36
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

[张量收缩计算图解.pptx](https://github.com/AI4Finance-Foundation/ElegantRL_Solver/files/11128434/default.pptx)

---

https://github.com/AI4Finance-Foundation/ElegantRL_Solver/pull/92

已经提交PR 92 去修复此BUG

---

https://github.com/AI4Finance-Foundation/ElegantRL_Solver/blob/52b4dc3ac5b8461772751a7294f5c9c10fdba5a5/rlsolver/rlsolver_learn2opt/tensor_train/TNCO_env.py#L269-L271

上面的代码有bug,这里想要修改 list中存放的指针的指向,但是最右边的等号是一个【赋值】操作,它让指针重新指向新的地址,这是不正确的。

应该修改成
```
node_dims_tens = th.stack([self.node_dims_ten.clone() for _ in range(num_envs)])
node_bool_tens = th.stack([self.node_bool_ten.clone() for _ in range(num_envs)])
for i in range(run_edges):
...
for j in range(num_envs):
...
node_dims_arys = node_dims_tens[j]
node_bool_arys = node_bool_tens[j]
...
node_dims_arys[contract_bool] = contract_dims.repeat(1, 1) # 根据 bool 将所有收缩后的节点都刷新成相同的信息
node_bool_arys[contract_bool] = contract_bool.repeat(1, 1) # 根据 bool 将所有收缩后的节点都刷新成相同的信息
...
```

举例:
初始化 `arys = [torch.zeros(2) + i for i in range(5)]`,打印 `arys`
```
[tensor([0., 0.]),
tensor([1., 1.]),
tensor([2., 2.]),
tensor([3., 3.]),
tensor([4., 4.])]
```

修改指针指向 `arys[0] = arys[1] = arys[2] = torch.zeros(2) -1`,打印 `arys`
```
[tensor([-1., -1.]),
tensor([-1., -1.]),
tensor([-1., -1.]),
tensor([3., 3.]),
tensor([4., 4.])]
```

修改 arys[0],使用赋值操作 `arys[0] = torch.zeros(2) + 0`。重新打印 `arys`,确认指针指向是否正确
```
[tensor([0., 0.]), -----> 我把 arys[0] 从-1 改成了 0
tensor([-1., -1.]), -----> 发现 arys[1] 没有跟着 arys[0] 一起变成 0,这是错误的
tensor([-1., -1.]), -----> 发现 arys[2] 没有跟着 arys[0] 一起变成 0,这是错误的
tensor([3., 3.]),
tensor([4., 4.])]
```

**不能使用赋值操**作改变指针指向的地址,应该用 `arys[0][:] = torch.zeros(2) + 0`
重新运行命令 修改指针指向 `arys[0] = arys[1] = arys[2] = torch.zeros(2) -1`
重新运行命令 修改 arys[0] 的赋值 `arys[0][:] = torch.zeros(2) + 0`
得到预期中的结果:
```
[tensor([0., 0.]),
tensor([0., 0.]),
tensor([0., 0.]),
tensor([3., 3.]),
tensor([4., 4.])]
```

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.