File size: 1,561 Bytes
460072a a861765 460072a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
from typing import Optional, Type
import gym
import torch.nn as nn
from rl_algo_impls.shared.encoder.cnn import FlattenedCnnEncoder
from rl_algo_impls.shared.module.utils import layer_init
class NatureCnn(FlattenedCnnEncoder):
"""
CNN from DQN Nature paper: Mnih, Volodymyr, et al.
"Human-level control through deep reinforcement learning."
Nature 518.7540 (2015): 529-533.
"""
def __init__(
self,
obs_space: gym.Space,
activation: Type[nn.Module],
cnn_init_layers_orthogonal: Optional[bool],
linear_init_layers_orthogonal: bool,
cnn_flatten_dim: int,
**kwargs,
) -> None:
if cnn_init_layers_orthogonal is None:
cnn_init_layers_orthogonal = True
in_channels = obs_space.shape[0] # type: ignore
cnn = nn.Sequential(
layer_init(
nn.Conv2d(in_channels, 32, kernel_size=8, stride=4),
cnn_init_layers_orthogonal,
),
activation(),
layer_init(
nn.Conv2d(32, 64, kernel_size=4, stride=2),
cnn_init_layers_orthogonal,
),
activation(),
layer_init(
nn.Conv2d(64, 64, kernel_size=3, stride=1),
cnn_init_layers_orthogonal,
),
activation(),
)
super().__init__(
obs_space,
activation,
linear_init_layers_orthogonal,
cnn_flatten_dim,
cnn,
**kwargs,
)
|