-
Notifications
You must be signed in to change notification settings - Fork 58
draft_retrace #695
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zhuboli
wants to merge
10
commits into
HorizonRobotics:pytorch
Choose a base branch
from
zhuboli:some-feature-retrace
base: pytorch
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
draft_retrace #695
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
edde2da
draft_retrace
zhuboli 394a39a
fix retrace
zhuboli 074b5da
fix retrace
zhuboli 07b5929
fix retrace
zhuboli 1219ef1
fix conflicts
zhuboli 5dc49be
Merge branch 'pytorch' into some-feature-retrace
zhuboli 027c817
fix retrace
zhuboli 23d5945
still need merge advantage function
zhuboli 5cafd48
merge function and fix bug
zhuboli 2466126
merge function and fix bug
zhuboli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -195,6 +195,9 @@ def generalized_advantage_estimation(rewards, | |
| values, | ||
| step_types, | ||
| discounts, | ||
| target_value, | ||
| importance_ratio, | ||
| use_retrace=False, | ||
| td_lambda=1.0, | ||
| time_major=True): | ||
| """Computes generalized advantage estimation (GAE) for the first T-1 steps. | ||
|
|
@@ -231,6 +234,8 @@ def generalized_advantage_estimation(rewards, | |
| rewards = rewards.transpose(0, 1) | ||
| values = values.transpose(0, 1) | ||
| step_types = step_types.transpose(0, 1) | ||
| importance_ratio = importance_ratio.transpose(0, 1) | ||
| target_value = target_value.transpose(0, 1) | ||
|
|
||
| assert values.shape[0] >= 2, ("The sequence length needs to be " | ||
| "at least 2. Got {s}".format( | ||
|
|
@@ -240,18 +245,76 @@ def generalized_advantage_estimation(rewards, | |
| is_lasts = common.expand_dims_as(is_lasts, values) | ||
| discounts = common.expand_dims_as(discounts, values) | ||
|
|
||
| weighted_discounts = discounts[1:] * td_lambda | ||
| advs = torch.zeros_like(values) | ||
| if use_retrace == False: | ||
| weighted_discounts = discounts[1:] * td_lambda | ||
| delta = rewards[1:] + discounts[1:] * values[1:] - values[:-1] | ||
| with torch.no_grad(): | ||
| for t in reversed(range(rewards.shape[0] - 1)): | ||
| advs[t] = (1 - is_lasts[t]) * \ | ||
| (delta[t] + weighted_discounts[t] * advs[t + 1]) | ||
| advs = advs[:-1] | ||
| else: | ||
| delta = (rewards[1:] + discounts[1:] * target_value[1:] - values[:-1]) | ||
| weighted_discounts = discounts[1:] * td_lambda * importance_ratio | ||
| with torch.no_grad(): | ||
| for t in reversed(range(rewards.shape[0] - 1)): | ||
| advs[t] = (1 - is_lasts[t]) * \ | ||
| (delta[t] + weighted_discounts[t] * advs[t + 1]) | ||
| advs = advs[:-1] | ||
|
|
||
| if not time_major: | ||
| advs = advs.transpose(0, 1) | ||
|
|
||
| return advs.detach() | ||
|
|
||
|
|
||
| ''' | ||
| # add for the retrace method | ||
| def generalized_advantage_estimation_retrace(importance_ratio, discounts, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function can be merged with |
||
| rewards, td_lambda, time_major, | ||
| values, target_value, step_types): | ||
| """ | ||
| compute the generalized advantage estimation for retrace method. Main change is adding | ||
| importance ratio | ||
|
|
||
| Args: | ||
| importance_ratio: shape is [T], scalar between [0,1]. Representing importance ratio | ||
| rewards (Tensor): shape is [T, B] (or [T]) representing rewards. | ||
| values (Tensor): shape is [T,B] (or [T]) representing values. | ||
| step_types (Tensor): shape is [T,B] (or [T]) representing step types. | ||
| discounts (Tensor): shape is [T, B] (or [T]) representing discounts. | ||
| td_lambda (float): A scalar between [0, 1]. It's used for variance | ||
| reduction in temporal difference. | ||
| time_major (bool): Whether input tensors are time major. | ||
| False means input tensors have shape [B, T]. | ||
| Returns: | ||
| A tensor with shape [T-1, B] representing advantages. Shape is [B, T-1] | ||
| when time_major is false. | ||
| """ | ||
| if not time_major: | ||
| discounts = discounts.transpose(0, 1) | ||
| rewards = rewards.transpose(0, 1) | ||
| values = values.transpose(0, 1) | ||
| step_types = step_types.transpose(0, 1) | ||
| importance_ratio = importance_ratio.transpose(0, 1) | ||
| target_value = target_value.transpose(0, 1) | ||
|
|
||
| assert values.shape[0] >= 2, ("The sequence length needs to be " | ||
| "at least 2. Got {s}".format( | ||
| s=values.shape[0])) | ||
| advs = torch.zeros_like(values) | ||
| delta = rewards[1:] + discounts[1:] * values[1:] - values[:-1] | ||
| is_lasts = (step_types == StepType.LAST).to(dtype=torch.float32) | ||
| delta = (rewards[1:] + discounts[1:] * target_value[1:] - values[:-1]) | ||
|
|
||
| weighted_discounts = discounts[1:] * td_lambda * importance_ratio | ||
| with torch.no_grad(): | ||
| for t in reversed(range(rewards.shape[0] - 1)): | ||
| advs[t] = (1 - is_lasts[t]) * \ | ||
| (delta[t] + weighted_discounts[t] * advs[t + 1]) | ||
| advs = advs[:-1] | ||
|
|
||
| if not time_major: | ||
| advs = advs.transpose(0, 1) | ||
|
|
||
| return advs.detach() | ||
| ''' | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -96,14 +96,17 @@ class GeneralizedAdvantageTest(unittest.TestCase): | |
| """Tests for alf.utils.value_ops.generalized_advantage_estimation | ||
| """ | ||
|
|
||
| def _check(self, rewards, values, step_types, discounts, td_lambda, | ||
| expected): | ||
| def _check(self, rewards, values, step_types, discounts, target_value, | ||
| importance_ratio, use_retrace, td_lambda, expected): | ||
| np.testing.assert_array_almost_equal( | ||
| value_ops.generalized_advantage_estimation( | ||
| rewards=rewards, | ||
| values=values, | ||
| step_types=step_types, | ||
| discounts=discounts, | ||
| target_value=target_value, | ||
| importance_ratio=importance_ratio, | ||
| use_retrace=use_retrace, | ||
| td_lambda=td_lambda, | ||
| time_major=False), expected) | ||
|
|
||
|
|
@@ -113,6 +116,9 @@ def _check(self, rewards, values, step_types, discounts, td_lambda, | |
| values=torch.stack([values, 2 * values], dim=2), | ||
| step_types=step_types, | ||
| discounts=discounts, | ||
| importance_ratio=importance_ratio, | ||
| target_value=target_value, | ||
| use_retrace=use_retrace, | ||
| td_lambda=td_lambda, | ||
| time_major=False), | ||
| torch.stack([expected, 2 * expected], dim=2), | ||
|
|
@@ -124,7 +130,9 @@ def test_generalized_advantage_estimation(self): | |
| rewards = torch.tensor([[3.] * 5], dtype=torch.float32) | ||
| discounts = torch.tensor([[0.9] * 5], dtype=torch.float32) | ||
| td_lambda = 0.6 / 0.9 | ||
|
|
||
| target_value = torch.tensor([[3.] * 4], dtype=torch.float32) | ||
| importance_ratio = torch.tensor([[0.8] * 3], dtype=torch.float32) | ||
| use_retrace = False | ||
| d = 2 * 0.9 + 1 | ||
| expected = torch.tensor([[((d * 0.6 + d) * 0.6 + d) * 0.6 + d, | ||
| (d * 0.6 + d) * 0.6 + d, d * 0.6 + d, d]], | ||
|
|
@@ -134,7 +142,10 @@ def test_generalized_advantage_estimation(self): | |
| values=values, | ||
| step_types=step_types, | ||
| discounts=discounts, | ||
| importance_ratio=importance_ratio, | ||
| target_value=target_value, | ||
| td_lambda=td_lambda, | ||
| use_retrace=use_retrace, | ||
| expected=expected) | ||
|
|
||
| # two episodes, and exceed by time limit (discount=1) | ||
|
|
@@ -150,7 +161,10 @@ def test_generalized_advantage_estimation(self): | |
| values=values, | ||
| step_types=step_types, | ||
| discounts=discounts, | ||
| importance_ratio=importance_ratio, | ||
| target_value=target_value, | ||
| td_lambda=td_lambda, | ||
| use_retrace=use_retrace, | ||
| expected=expected) | ||
|
|
||
| # tow episodes, and end normal (discount=0) | ||
|
|
@@ -169,8 +183,41 @@ def test_generalized_advantage_estimation(self): | |
| step_types=step_types, | ||
| discounts=discounts, | ||
| td_lambda=td_lambda, | ||
| importance_ratio=importance_ratio, | ||
| target_value=target_value, | ||
| use_retrace=use_retrace, | ||
| expected=expected) | ||
|
|
||
|
|
||
| ''' | ||
| class GeneralizedAdvantage_retrace_Test(unittest.TestCase): | ||
| """Tests for alf.utils.value_ops | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comments not correct |
||
| """GeneralizedAdvantageTest.test_generalized_advantage_estimation() | ||
|
|
||
| def test_generalized_advantage_estimation_retrace(self): | ||
| values = torch.tensor([[2.] * 4], dtype=torch.float32) | ||
| step_types = torch.tensor([[StepType.MID] * 4], dtype=torch.int64) | ||
| rewards = torch.tensor([[3.] * 4], dtype=torch.float32) | ||
| discounts = torch.tensor([[0.9] * 4], dtype=torch.float32) | ||
| td_lambda = 0.6 / 0.9 | ||
| target_value = torch.tensor([[3.] * 4], dtype=torch.float32) | ||
| importance_ratio = torch.tensor([[0.8] * 3], dtype=torch.float32) | ||
| d = 3 * 0.9 + 3 - 2 | ||
| expected = torch.tensor( | ||
| [[(d * 0.6 * 0.8) * 0.6 * 0.8 + 0.6 * 0.8 * d + d, | ||
| d * 0.6 * 0.8 + d, d]], | ||
| dtype=torch.float32) | ||
| np.testing.assert_array_almost_equal( | ||
| value_ops.generalized_advantage_estimation_retrace( | ||
| rewards=rewards, | ||
| values=values, | ||
| target_value=target_value, | ||
| step_types=step_types, | ||
| discounts=discounts, | ||
| td_lambda=td_lambda, | ||
| importance_ratio=importance_ratio, | ||
| time_major=False), expected) | ||
| ''' | ||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
format, line is too long
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not fixed?