-
Notifications
You must be signed in to change notification settings - Fork 16
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
[WIP, Policy] Docstring and refactoring on top of PR 327 #335
Changes from 5 commits
e6a14ef
57b0b13
178a08e
ace8a28
8e6f03d
774c411
9520315
910a948
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 |
---|---|---|
|
@@ -6,19 +6,32 @@ | |
|
||
|
||
class CNNPolicy(Policy): | ||
def __init__(self, config, env, device, float_precision, base=None): | ||
def __init__(self, **kwargs): | ||
# Shared weights, defaults to False | ||
self.shared_weights = config.get("shared_weights", False) | ||
# Reload checkpoint, defaults to False | ||
self.reload_ckpt = config.get("reload_ckpt", False) | ||
# CNN features: number of layers, number of channels, kernel sizes, strides | ||
self.n_layers = config.get("n_layers", 3) | ||
self.channels = config.get("channels", [16] * self.n_layers) | ||
self.kernel_sizes = config.get("kernel_sizes", [(3, 3)] * self.n_layers) | ||
self.strides = config.get("strides", [(1, 1)] * self.n_layers) | ||
# Environment | ||
# TODO: rethink whether storing the whole environment is needed | ||
self.env = env | ||
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. We might not need to store the env most of the time actually. But sometimes one might need to know more about the environment configuration to induce inductive bias to their policy model. In the CNN Policy, we use to access the grid dimension like 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. +1, I think storing the whole env is likely to duplicate a lot of info unnecessarily, especially if you're trying to do multiprocessing stuff down the line. |
||
super().__init__( | ||
config=config, | ||
env=env, | ||
device=device, | ||
float_precision=float_precision, | ||
base=base, | ||
) | ||
# Base init | ||
super().__init__(**kwargs) | ||
|
||
def make_cnn(self): | ||
def make_model(self): | ||
""" | ||
Defines an CNN with no top layer activation | ||
Instantiates a CNN with no top layer activation. | ||
|
||
Returns | ||
------- | ||
model : torch.nn.Module | ||
A torch model containing the CNN. | ||
is_model : bool | ||
True because a CNN is a model. | ||
""" | ||
if self.shared_weights and self.base is not None: | ||
layers = list(self.base.model.children())[:-1] | ||
|
@@ -27,14 +40,15 @@ def make_cnn(self): | |
) | ||
|
||
model = nn.Sequential(*layers, last_layer).to(self.device) | ||
return model | ||
return model, True | ||
|
||
current_channels = 1 | ||
conv_module = nn.Sequential() | ||
|
||
if len(self.kernel_sizes) != self.n_layers: | ||
raise ValueError( | ||
f"Inconsistent dimensions kernel_sizes != n_layers, {len(self.kernel_sizes)} != {self.n_layers}" | ||
f"Inconsistent dimensions kernel_sizes != n_layers, " | ||
"{len(self.kernel_sizes)} != {self.n_layers}" | ||
) | ||
|
||
for i in range(self.n_layers): | ||
|
@@ -59,33 +73,19 @@ def make_cnn(self): | |
in_channels = conv_module(dummy_input).numel() | ||
if in_channels >= 500_000: # TODO: this could better be handled | ||
raise RuntimeWarning( | ||
"Input channels for the dense layer are too big, this will increase number of parameters" | ||
"Input channels for the dense layer are too big, this will " | ||
"increase number of parameters" | ||
) | ||
except RuntimeError as e: | ||
raise RuntimeError( | ||
"Failed during convolution operation. Ensure that the kernel sizes and strides are appropriate for the input dimensions." | ||
"Failed during convolution operation. Ensure that the kernel sizes " | ||
"and strides are appropriate for the input dimensions." | ||
) from e | ||
|
||
model = nn.Sequential( | ||
conv_module, nn.Flatten(), nn.Linear(in_channels, self.output_dim) | ||
) | ||
return model.to(self.device) | ||
|
||
def parse_config(self, config): | ||
super().parse_config(config) | ||
if config is None: | ||
config = OmegaConf.create() | ||
self.checkpoint = config.get("checkpoint", None) | ||
self.shared_weights = config.get("shared_weights", False) | ||
self.reload_ckpt = config.get("reload_ckpt", False) | ||
self.n_layers = config.get("n_layers", 3) | ||
self.channels = config.get("channels", [16] * self.n_layers) | ||
self.kernel_sizes = config.get("kernel_sizes", [(3, 3)] * self.n_layers) | ||
self.strides = config.get("strides", [(1, 1)] * self.n_layers) | ||
|
||
def instantiate(self): | ||
self.model = self.make_cnn() | ||
self.is_model = True | ||
return model.to(self.device), True | ||
|
||
def __call__(self, states): | ||
states = states.unsqueeze(1) # (batch_size, channels, height, width) | ||
|
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.
I think you also need to import
Tuple
hereThere 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.
Indeed.