-
Notifications
You must be signed in to change notification settings - Fork 1.2k
update utils.network #275
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
Merged
Merged
update utils.network #275
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
02441e5
pep8 fix
ChenDRAG 8ea3d5b
cosistency in net/discrete
ChenDRAG 1e577d6
pep8 fix
ChenDRAG 445fa23
small error fix
ChenDRAG 8c2c4f6
fix ci
Trinkle23897 04994ae
simplify network code and enable type checking
Trinkle23897 0c171f3
[a] * b -> [a for i in range(b)]
Trinkle23897 4cff49b
fix net
Trinkle23897 37c49a8
fix ci
Trinkle23897 a304ddd
fix pydocstyle and change default [] -> ()
Trinkle23897 c9648d4
update args
Trinkle23897 bacb445
complete docstring of each network
Trinkle23897 b88090f
move atari network to examples/atari/network.py
Trinkle23897 bb3ac84
network -> atari_network
Trinkle23897 8ddc64a
small tune
Trinkle23897 d9f608d
final tune
Trinkle23897 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import torch | ||
import numpy as np | ||
from torch import nn | ||
from typing import Any, Dict, Tuple, Union, Optional, Sequence | ||
|
||
|
||
class DQN(nn.Module): | ||
"""Reference: Human-level control through deep reinforcement learning. | ||
|
||
For advanced usage (how to customize the network), please refer to | ||
:ref:`build_the_network`. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
c: int, | ||
h: int, | ||
w: int, | ||
action_shape: Sequence[int], | ||
device: Union[str, int, torch.device] = "cpu", | ||
features_only: bool = False, | ||
) -> None: | ||
super().__init__() | ||
self.device = device | ||
self.net = nn.Sequential( | ||
nn.Conv2d(c, 32, kernel_size=8, stride=4), nn.ReLU(inplace=True), | ||
nn.Conv2d(32, 64, kernel_size=4, stride=2), nn.ReLU(inplace=True), | ||
nn.Conv2d(64, 64, kernel_size=3, stride=1), nn.ReLU(inplace=True), | ||
nn.Flatten()) | ||
with torch.no_grad(): | ||
self.output_dim = np.prod( | ||
self.net(torch.zeros(1, c, h, w)).shape[1:]) | ||
if not features_only: | ||
self.net = nn.Sequential( | ||
self.net, | ||
nn.Linear(self.output_dim, 512), nn.ReLU(inplace=True), | ||
nn.Linear(512, np.prod(action_shape))) | ||
self.output_dim = np.prod(action_shape) | ||
|
||
def forward( | ||
self, | ||
x: Union[np.ndarray, torch.Tensor], | ||
state: Optional[Any] = None, | ||
info: Dict[str, Any] = {}, | ||
duburcqa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) -> Tuple[torch.Tensor, Any]: | ||
r"""Mapping: x -> Q(x, \*).""" | ||
x = torch.as_tensor( | ||
x, device=self.device, dtype=torch.float32) # type: ignore | ||
return self.net(x), state | ||
|
||
|
||
class C51(DQN): | ||
"""Reference: A distributional perspective on reinforcement learning. | ||
|
||
For advanced usage (how to customize the network), please refer to | ||
:ref:`build_the_network`. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
c: int, | ||
h: int, | ||
w: int, | ||
action_shape: Sequence[int], | ||
num_atoms: int = 51, | ||
device: Union[str, int, torch.device] = "cpu", | ||
) -> None: | ||
super().__init__(c, h, w, [np.prod(action_shape) * num_atoms], device) | ||
self.action_shape = action_shape | ||
self.num_atoms = num_atoms | ||
|
||
def forward( | ||
self, | ||
x: Union[np.ndarray, torch.Tensor], | ||
duburcqa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
state: Optional[Any] = None, | ||
info: Dict[str, Any] = {}, | ||
duburcqa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) -> Tuple[torch.Tensor, Any]: | ||
r"""Mapping: x -> Z(x, \*).""" | ||
x, state = super().forward(x) | ||
x = x.view(-1, self.num_atoms).softmax(dim=-1) | ||
x = x.view(-1, np.prod(self.action_shape), self.num_atoms) | ||
return x, state |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.