-
Notifications
You must be signed in to change notification settings - Fork 381
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(nyz): fix confusing shallow copy operation about next_obs (#641)
- Loading branch information
Showing
6 changed files
with
70 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import pytest | ||
import numpy as np | ||
import torch | ||
from ding.worker.collector.base_serial_collector import to_tensor_transitions | ||
|
||
|
||
def get_transition(): | ||
return { | ||
'obs': np.random.random((2, 3)), | ||
'action': np.random.randint(0, 6, size=(1, )), | ||
'reward': np.random.random((1, )), | ||
'done': False, | ||
'next_obs': np.random.random((2, 3)), | ||
} | ||
|
||
|
||
@pytest.mark.unittest | ||
def test_to_tensor_transitions(): | ||
# test case when shallow copy is True | ||
transition_list = [get_transition() for _ in range(4)] | ||
tensor_list = to_tensor_transitions(transition_list, shallow_copy_next_obs=True) | ||
for i in range(len(tensor_list)): | ||
tensor = tensor_list[i] | ||
assert isinstance(tensor['obs'], torch.Tensor) | ||
assert isinstance(tensor['action'], torch.Tensor), type(tensor['action']) | ||
assert isinstance(tensor['reward'], torch.Tensor) | ||
assert isinstance(tensor['done'], bool) | ||
assert 'next_obs' in tensor | ||
if i < len(tensor_list) - 1: | ||
assert id(tensor['next_obs']) == id(tensor_list[i + 1]['obs']) | ||
# test case when shallow copy is False | ||
transition_list = [get_transition() for _ in range(4)] | ||
tensor_list = to_tensor_transitions(transition_list, shallow_copy_next_obs=False) | ||
for i in range(len(tensor_list)): | ||
tensor = tensor_list[i] | ||
assert isinstance(tensor['obs'], torch.Tensor) | ||
assert isinstance(tensor['action'], torch.Tensor) | ||
assert isinstance(tensor['reward'], torch.Tensor) | ||
assert isinstance(tensor['done'], bool) | ||
assert 'next_obs' in tensor | ||
if i < len(tensor_list) - 1: | ||
assert id(tensor['next_obs']) != id(tensor_list[i + 1]['obs']) |