-
Notifications
You must be signed in to change notification settings - Fork 59
Misc improvements #1333
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
base: pytorch
Are you sure you want to change the base?
Misc improvements #1333
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -337,7 +337,11 @@ def pre_config(configs): | |
| try: | ||
| config1(name, value, mutable=False) | ||
| _HANDLED_PRE_CONFIGS.append((name, value)) | ||
| except ValueError: | ||
| except ValueError as e: | ||
| # Most of the times, for command line flags, this warning is a false alarm. | ||
| # This can be useful in other failures, e.g. when the Config has already been used, | ||
| # before configuring its value. | ||
| logging.warning("pre_config potential error: %s", e) | ||
|
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 warning is hard to understand. It's better to identify the case of the Config has already been used. Perhaps throw a different type of Exception when config has been used in config1()?
Contributor
Author
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. Good point. Logging error in config1. |
||
| _PRE_CONFIGS.append((name, value)) | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,7 +29,7 @@ | |
| DataItem = alf.data_structures.namedtuple( | ||
| "DataItem", [ | ||
| "env_id", "x", "o", "reward", "step_type", "batch_info", | ||
| "replay_buffer", "rollout_info_field" | ||
| "replay_buffer", "rollout_info_field", "discount" | ||
| ], | ||
| default_value=()) | ||
|
|
||
|
|
@@ -40,12 +40,20 @@ def get_batch(env_ids, dim, t, x): | |
| batch_size = len(env_ids) | ||
| x = torch.as_tensor(x, dtype=torch.float32, device="cpu") | ||
| t = torch.as_tensor(t, dtype=torch.int32, device="cpu") | ||
| ox = (x * torch.arange( | ||
| batch_size, dtype=torch.float32, requires_grad=True, | ||
| device="cpu").unsqueeze(1) * torch.arange( | ||
| dim, dtype=torch.float32, requires_grad=True, | ||
| device="cpu").unsqueeze(0)) | ||
| a = x * torch.ones(batch_size, dtype=torch.float32, device="cpu") | ||
| # ox = (x * torch.arange( | ||
| # batch_size, dtype=torch.float32, requires_grad=True, | ||
| # device="cpu").unsqueeze(1) * torch.arange( | ||
| # dim, dtype=torch.float32, requires_grad=True, | ||
| # device="cpu").unsqueeze(0)) | ||
| if batch_size > 1 and x.ndim > 0 and batch_size == x.shape[0]: | ||
| a = x | ||
| else: | ||
| a = x * torch.ones(batch_size, dtype=torch.float32, device="cpu") | ||
| if batch_size > 1 and t.ndim > 0 and batch_size == t.shape[0]: | ||
| pass | ||
| else: | ||
| t = t * torch.ones(batch_size, dtype=torch.int32, device="cpu") | ||
| ox = a.unsqueeze(1).clone().requires_grad_(True) | ||
|
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. what is the purpose of this change?
Contributor
Author
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 is needed because we allow x and t inputs to be scalars, which will be expanded to be consistent with the batch_size. Made code easier to read, and commented. |
||
| g = torch.zeros(batch_size, dtype=torch.float32, device="cpu") | ||
| # reward function adapted from ReplayBuffer: default_reward_fn | ||
| r = torch.where( | ||
|
|
@@ -60,6 +68,10 @@ def get_batch(env_ids, dim, t, x): | |
| "a": a, | ||
| "g": g | ||
| }), | ||
| discount=torch.tensor( | ||
| t != alf.data_structures.StepType.LAST, | ||
| dtype=torch.float32, | ||
| device="cpu"), | ||
| reward=r) | ||
|
|
||
|
|
||
|
|
@@ -79,6 +91,7 @@ def __init__(self, *args): | |
| "a": alf.TensorSpec(shape=(), dtype=torch.float32), | ||
| "g": alf.TensorSpec(shape=(), dtype=torch.float32) | ||
| }), | ||
| discount=alf.TensorSpec(shape=(), dtype=torch.float32), | ||
| reward=alf.TensorSpec(shape=(), dtype=torch.float32)) | ||
|
|
||
| @parameterized.named_parameters([ | ||
|
|
||
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.
Seems unnecessary. Can provide alf.math.identity as argument.
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.
Good point. Removed.