Skip to content

Commit

Permalink
fix: remove eval() python method (#265)
Browse files Browse the repository at this point in the history
* fix: remove eval() python method

* fix: dreamer_v3 doapp configs
  • Loading branch information
michele-milesi authored Apr 15, 2024
1 parent 8ca2a60 commit 31a8e9b
Show file tree
Hide file tree
Showing 17 changed files with 109 additions and 81 deletions.
2 changes: 1 addition & 1 deletion howto/learn_in_diambra.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ env:
diambra_settings:
characters: Kasumi
step_ratio: 5
role: diambra.arena.Roles.P1
role: P1
diambra_wrappers:
normalize_reward: True
normalization_factor: 0.3
Expand Down
7 changes: 4 additions & 3 deletions howto/register_external_algorithm.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ from math import prod
from typing import Any, Dict, List, Optional, Sequence, Tuple

import gymnasium
import hydra
import torch
import torch.nn as nn
from lightning import Fabric
Expand Down Expand Up @@ -198,7 +199,7 @@ class PPOAgent(nn.Module):
mlp_keys,
encoder_cfg.dense_units,
encoder_cfg.mlp_layers,
eval(encoder_cfg.dense_act),
hydra.utils.get_class(encoder_cfg.dense_act),
encoder_cfg.layer_norm,
)
if mlp_keys is not None and len(mlp_keys) > 0
Expand All @@ -210,7 +211,7 @@ class PPOAgent(nn.Module):
input_dims=features_dim,
output_dim=1,
hidden_sizes=[critic_cfg.dense_units] * critic_cfg.mlp_layers,
activation=eval(critic_cfg.dense_act),
activation=hydra.utils.get_class(critic_cfg.dense_act),
norm_layer=[nn.LayerNorm for _ in range(critic_cfg.mlp_layers)] if critic_cfg.layer_norm else None,
norm_args=(
[{"normalized_shape": critic_cfg.dense_units} for _ in range(critic_cfg.mlp_layers)]
Expand All @@ -223,7 +224,7 @@ class PPOAgent(nn.Module):
input_dims=features_dim,
output_dim=None,
hidden_sizes=[actor_cfg.dense_units] * actor_cfg.mlp_layers,
activation=eval(actor_cfg.dense_act),
activation=hydra.utils.get_class(actor_cfg.dense_act),
flatten_dim=None,
norm_layer=[nn.LayerNorm] * actor_cfg.mlp_layers if actor_cfg.layer_norm else None,
norm_args=(
Expand Down
7 changes: 4 additions & 3 deletions howto/register_new_algorithm.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ from math import prod
from typing import Any, Dict, List, Optional, Sequence, Tuple

import gymnasium
import hydra
import torch
import torch.nn as nn
from lightning import Fabric
Expand Down Expand Up @@ -197,7 +198,7 @@ class PPOAgent(nn.Module):
mlp_keys,
encoder_cfg.dense_units,
encoder_cfg.mlp_layers,
eval(encoder_cfg.dense_act),
hydra.utils.get_class(encoder_cfg.dense_act),
encoder_cfg.layer_norm,
)
if mlp_keys is not None and len(mlp_keys) > 0
Expand All @@ -209,7 +210,7 @@ class PPOAgent(nn.Module):
input_dims=features_dim,
output_dim=1,
hidden_sizes=[critic_cfg.dense_units] * critic_cfg.mlp_layers,
activation=eval(critic_cfg.dense_act),
activation=hydra.utils.get_class(critic_cfg.dense_act),
norm_layer=[nn.LayerNorm for _ in range(critic_cfg.mlp_layers)] if critic_cfg.layer_norm else None,
norm_args=(
[{"normalized_shape": critic_cfg.dense_units} for _ in range(critic_cfg.mlp_layers)]
Expand All @@ -222,7 +223,7 @@ class PPOAgent(nn.Module):
input_dims=features_dim,
output_dim=None,
hidden_sizes=[actor_cfg.dense_units] * actor_cfg.mlp_layers,
activation=eval(actor_cfg.dense_act),
activation=hydra.utils.get_class(actor_cfg.dense_act),
flatten_dim=None,
norm_layer=[nn.LayerNorm] * actor_cfg.mlp_layers if actor_cfg.layer_norm else None,
norm_args=(
Expand Down
7 changes: 4 additions & 3 deletions sheeprl/algos/a2c/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Any, Dict, List, Optional, Sequence, Tuple

import gymnasium
import hydra
import torch
import torch.nn as nn
from lightning import Fabric
Expand Down Expand Up @@ -72,7 +73,7 @@ def __init__(
mlp_keys,
encoder_cfg.dense_units,
encoder_cfg.mlp_layers,
eval(encoder_cfg.dense_act),
hydra.utils.get_class(encoder_cfg.dense_act),
encoder_cfg.layer_norm,
)
if mlp_keys is not None and len(mlp_keys) > 0
Expand All @@ -85,7 +86,7 @@ def __init__(
input_dims=features_dim,
output_dim=1,
hidden_sizes=[critic_cfg.dense_units] * critic_cfg.mlp_layers,
activation=eval(critic_cfg.dense_act),
activation=hydra.utils.get_class(critic_cfg.dense_act),
norm_layer=[nn.LayerNorm for _ in range(critic_cfg.mlp_layers)] if critic_cfg.layer_norm else None,
norm_args=(
[{"normalized_shape": critic_cfg.dense_units} for _ in range(critic_cfg.mlp_layers)]
Expand All @@ -99,7 +100,7 @@ def __init__(
input_dims=features_dim,
output_dim=None,
hidden_sizes=[actor_cfg.dense_units] * actor_cfg.mlp_layers,
activation=eval(actor_cfg.dense_act),
activation=hydra.utils.get_class(actor_cfg.dense_act),
flatten_dim=None,
norm_layer=[nn.LayerNorm] * actor_cfg.mlp_layers if actor_cfg.layer_norm else None,
norm_args=(
Expand Down
22 changes: 11 additions & 11 deletions sheeprl/algos/dreamer_v1/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ def build_agent(
image_size=obs_space[cfg.algo.cnn_keys.encoder[0]].shape[-2:],
channels_multiplier=world_model_cfg.encoder.cnn_channels_multiplier,
layer_norm=False,
activation=eval(world_model_cfg.encoder.cnn_act),
activation=hydra.utils.get_class(world_model_cfg.encoder.cnn_act),
)
if cfg.algo.cnn_keys.encoder is not None and len(cfg.algo.cnn_keys.encoder) > 0
else None
Expand All @@ -384,7 +384,7 @@ def build_agent(
input_dims=[obs_space[k].shape[0] for k in cfg.algo.mlp_keys.encoder],
mlp_layers=world_model_cfg.encoder.mlp_layers,
dense_units=world_model_cfg.encoder.dense_units,
activation=eval(world_model_cfg.encoder.dense_act),
activation=hydra.utils.get_class(world_model_cfg.encoder.dense_act),
layer_norm=False,
)
if cfg.algo.mlp_keys.encoder is not None and len(cfg.algo.mlp_keys.encoder) > 0
Expand All @@ -394,22 +394,22 @@ def build_agent(
recurrent_model = RecurrentModel(
input_size=sum(actions_dim) + world_model_cfg.stochastic_size,
recurrent_state_size=world_model_cfg.recurrent_model.recurrent_state_size,
activation=eval(world_model_cfg.recurrent_model.dense_act),
activation=hydra.utils.get_class(world_model_cfg.recurrent_model.dense_act),
)
representation_model = MLP(
input_dims=(
world_model_cfg.recurrent_model.recurrent_state_size + encoder.cnn_output_dim + encoder.mlp_output_dim
),
output_dim=world_model_cfg.stochastic_size * 2,
hidden_sizes=[world_model_cfg.representation_model.hidden_size],
activation=eval(world_model_cfg.representation_model.dense_act),
activation=hydra.utils.get_class(world_model_cfg.representation_model.dense_act),
flatten_dim=None,
)
transition_model = MLP(
input_dims=world_model_cfg.recurrent_model.recurrent_state_size,
output_dim=world_model_cfg.stochastic_size * 2,
hidden_sizes=[world_model_cfg.transition_model.hidden_size],
activation=eval(world_model_cfg.transition_model.dense_act),
activation=hydra.utils.get_class(world_model_cfg.transition_model.dense_act),
flatten_dim=None,
)
rssm = RSSM(
Expand All @@ -427,7 +427,7 @@ def build_agent(
latent_state_size=latent_state_size,
cnn_encoder_output_dim=cnn_encoder.output_dim,
image_size=obs_space[cfg.algo.cnn_keys.decoder[0]].shape[-2:],
activation=eval(world_model_cfg.observation_model.cnn_act),
activation=hydra.utils.get_class(world_model_cfg.observation_model.cnn_act),
layer_norm=False,
)
if cfg.algo.cnn_keys.decoder is not None and len(cfg.algo.cnn_keys.decoder) > 0
Expand All @@ -440,7 +440,7 @@ def build_agent(
latent_state_size=latent_state_size,
mlp_layers=world_model_cfg.observation_model.mlp_layers,
dense_units=world_model_cfg.observation_model.dense_units,
activation=eval(world_model_cfg.observation_model.dense_act),
activation=hydra.utils.get_class(world_model_cfg.observation_model.dense_act),
layer_norm=False,
)
if cfg.algo.mlp_keys.decoder is not None and len(cfg.algo.mlp_keys.decoder) > 0
Expand All @@ -451,15 +451,15 @@ def build_agent(
input_dims=latent_state_size,
output_dim=1,
hidden_sizes=[world_model_cfg.reward_model.dense_units] * world_model_cfg.reward_model.mlp_layers,
activation=eval(world_model_cfg.reward_model.dense_act),
activation=hydra.utils.get_class(world_model_cfg.reward_model.dense_act),
flatten_dim=None,
)
if world_model_cfg.use_continues:
continue_model = MLP(
input_dims=latent_state_size,
output_dim=1,
hidden_sizes=[world_model_cfg.discount_model.dense_units] * world_model_cfg.discount_model.mlp_layers,
activation=eval(world_model_cfg.discount_model.dense_act),
activation=hydra.utils.get_class(world_model_cfg.discount_model.dense_act),
flatten_dim=None,
)
world_model = WorldModel(
Expand All @@ -478,7 +478,7 @@ def build_agent(
min_std=actor_cfg.min_std,
mlp_layers=actor_cfg.mlp_layers,
dense_units=actor_cfg.dense_units,
activation=eval(actor_cfg.dense_act),
activation=hydra.utils.get_class(actor_cfg.dense_act),
distribution_cfg=cfg.distribution,
layer_norm=False,
expl_amount=actor_cfg.expl_amount,
Expand All @@ -489,7 +489,7 @@ def build_agent(
input_dims=latent_state_size,
output_dim=1,
hidden_sizes=[critic_cfg.dense_units] * critic_cfg.mlp_layers,
activation=eval(critic_cfg.dense_act),
activation=hydra.utils.get_class(critic_cfg.dense_act),
flatten_dim=None,
)
actor.apply(init_weights)
Expand Down
20 changes: 10 additions & 10 deletions sheeprl/algos/dreamer_v2/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,7 @@ def build_agent(
image_size=obs_space[cfg.algo.cnn_keys.encoder[0]].shape[-2:],
channels_multiplier=world_model_cfg.encoder.cnn_channels_multiplier,
layer_norm=world_model_cfg.encoder.layer_norm,
activation=eval(world_model_cfg.encoder.cnn_act),
activation=hydra.utils.get_class(world_model_cfg.encoder.cnn_act),
)
if cfg.algo.cnn_keys.encoder is not None and len(cfg.algo.cnn_keys.encoder) > 0
else None
Expand All @@ -894,7 +894,7 @@ def build_agent(
input_dims=[obs_space[k].shape[0] for k in cfg.algo.mlp_keys.encoder],
mlp_layers=world_model_cfg.encoder.mlp_layers,
dense_units=world_model_cfg.encoder.dense_units,
activation=eval(world_model_cfg.encoder.dense_act),
activation=hydra.utils.get_class(world_model_cfg.encoder.dense_act),
layer_norm=world_model_cfg.encoder.layer_norm,
)
if cfg.algo.mlp_keys.encoder is not None and len(cfg.algo.mlp_keys.encoder) > 0
Expand All @@ -911,7 +911,7 @@ def build_agent(
),
output_dim=stochastic_size,
hidden_sizes=[world_model_cfg.representation_model.hidden_size],
activation=eval(world_model_cfg.representation_model.dense_act),
activation=hydra.utils.get_class(world_model_cfg.representation_model.dense_act),
flatten_dim=None,
norm_layer=[nn.LayerNorm] if world_model_cfg.representation_model.layer_norm else None,
norm_args=(
Expand All @@ -924,7 +924,7 @@ def build_agent(
input_dims=world_model_cfg.recurrent_model.recurrent_state_size,
output_dim=stochastic_size,
hidden_sizes=[world_model_cfg.transition_model.hidden_size],
activation=eval(world_model_cfg.transition_model.dense_act),
activation=hydra.utils.get_class(world_model_cfg.transition_model.dense_act),
flatten_dim=None,
norm_layer=[nn.LayerNorm] if world_model_cfg.transition_model.layer_norm else None,
norm_args=(
Expand All @@ -948,7 +948,7 @@ def build_agent(
latent_state_size=latent_state_size,
cnn_encoder_output_dim=cnn_encoder.output_dim,
image_size=obs_space[cfg.algo.cnn_keys.decoder[0]].shape[-2:],
activation=eval(world_model_cfg.observation_model.cnn_act),
activation=hydra.utils.get_class(world_model_cfg.observation_model.cnn_act),
layer_norm=world_model_cfg.observation_model.layer_norm,
)
if cfg.algo.cnn_keys.decoder is not None and len(cfg.algo.cnn_keys.decoder) > 0
Expand All @@ -961,7 +961,7 @@ def build_agent(
latent_state_size=latent_state_size,
mlp_layers=world_model_cfg.observation_model.mlp_layers,
dense_units=world_model_cfg.observation_model.dense_units,
activation=eval(world_model_cfg.observation_model.dense_act),
activation=hydra.utils.get_class(world_model_cfg.observation_model.dense_act),
layer_norm=world_model_cfg.observation_model.layer_norm,
)
if cfg.algo.mlp_keys.decoder is not None and len(cfg.algo.mlp_keys.decoder) > 0
Expand All @@ -972,7 +972,7 @@ def build_agent(
input_dims=latent_state_size,
output_dim=1,
hidden_sizes=[world_model_cfg.reward_model.dense_units] * world_model_cfg.reward_model.mlp_layers,
activation=eval(world_model_cfg.reward_model.dense_act),
activation=hydra.utils.get_class(world_model_cfg.reward_model.dense_act),
flatten_dim=None,
norm_layer=(
[nn.LayerNorm for _ in range(world_model_cfg.reward_model.mlp_layers)]
Expand All @@ -993,7 +993,7 @@ def build_agent(
input_dims=latent_state_size,
output_dim=1,
hidden_sizes=[world_model_cfg.discount_model.dense_units] * world_model_cfg.discount_model.mlp_layers,
activation=eval(world_model_cfg.discount_model.dense_act),
activation=hydra.utils.get_class(world_model_cfg.discount_model.dense_act),
flatten_dim=None,
norm_layer=(
[nn.LayerNorm for _ in range(world_model_cfg.discount_model.mlp_layers)]
Expand Down Expand Up @@ -1025,15 +1025,15 @@ def build_agent(
min_std=actor_cfg.min_std,
mlp_layers=actor_cfg.mlp_layers,
dense_units=actor_cfg.dense_units,
activation=eval(actor_cfg.dense_act),
activation=hydra.utils.get_class(actor_cfg.dense_act),
distribution_cfg=cfg.distribution,
layer_norm=actor_cfg.layer_norm,
)
critic = MLP(
input_dims=latent_state_size,
output_dim=1,
hidden_sizes=[critic_cfg.dense_units] * critic_cfg.mlp_layers,
activation=eval(critic_cfg.dense_act),
activation=hydra.utils.get_class(critic_cfg.dense_act),
flatten_dim=None,
norm_layer=[nn.LayerNorm for _ in range(critic_cfg.mlp_layers)] if critic_cfg.layer_norm else None,
norm_args=(
Expand Down
20 changes: 10 additions & 10 deletions sheeprl/algos/dreamer_v3/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,7 @@ def build_agent(
channels_multiplier=world_model_cfg.encoder.cnn_channels_multiplier,
layer_norm_cls=hydra.utils.get_class(world_model_cfg.encoder.cnn_layer_norm.cls),
layer_norm_kw=world_model_cfg.encoder.cnn_layer_norm.kw,
activation=eval(world_model_cfg.encoder.cnn_act),
activation=hydra.utils.get_class(world_model_cfg.encoder.cnn_act),
stages=cnn_stages,
)
if cfg.algo.cnn_keys.encoder is not None and len(cfg.algo.cnn_keys.encoder) > 0
Expand All @@ -998,7 +998,7 @@ def build_agent(
input_dims=[obs_space[k].shape[0] for k in cfg.algo.mlp_keys.encoder],
mlp_layers=world_model_cfg.encoder.mlp_layers,
dense_units=world_model_cfg.encoder.dense_units,
activation=eval(world_model_cfg.encoder.dense_act),
activation=hydra.utils.get_class(world_model_cfg.encoder.dense_act),
layer_norm_cls=hydra.utils.get_class(world_model_cfg.encoder.mlp_layer_norm.cls),
layer_norm_kw=world_model_cfg.encoder.mlp_layer_norm.kw,
)
Expand All @@ -1022,7 +1022,7 @@ def build_agent(
input_dims=represention_model_input_size,
output_dim=stochastic_size,
hidden_sizes=[world_model_cfg.representation_model.hidden_size],
activation=eval(world_model_cfg.representation_model.dense_act),
activation=hydra.utils.get_class(world_model_cfg.representation_model.dense_act),
layer_args={"bias": representation_ln_cls == nn.Identity},
flatten_dim=None,
norm_layer=[representation_ln_cls],
Expand All @@ -1038,7 +1038,7 @@ def build_agent(
input_dims=recurrent_state_size,
output_dim=stochastic_size,
hidden_sizes=[world_model_cfg.transition_model.hidden_size],
activation=eval(world_model_cfg.transition_model.dense_act),
activation=hydra.utils.get_class(world_model_cfg.transition_model.dense_act),
layer_args={"bias": transition_ln_cls == nn.Identity},
flatten_dim=None,
norm_layer=[transition_ln_cls],
Expand Down Expand Up @@ -1072,7 +1072,7 @@ def build_agent(
latent_state_size=latent_state_size,
cnn_encoder_output_dim=cnn_encoder.output_dim,
image_size=obs_space[cfg.algo.cnn_keys.decoder[0]].shape[-2:],
activation=eval(world_model_cfg.observation_model.cnn_act),
activation=hydra.utils.get_class(world_model_cfg.observation_model.cnn_act),
layer_norm_cls=hydra.utils.get_class(world_model_cfg.observation_model.cnn_layer_norm.cls),
layer_norm_kw=world_model_cfg.observation_model.mlp_layer_norm.kw,
stages=cnn_stages,
Expand All @@ -1087,7 +1087,7 @@ def build_agent(
latent_state_size=latent_state_size,
mlp_layers=world_model_cfg.observation_model.mlp_layers,
dense_units=world_model_cfg.observation_model.dense_units,
activation=eval(world_model_cfg.observation_model.dense_act),
activation=hydra.utils.get_class(world_model_cfg.observation_model.dense_act),
layer_norm_cls=hydra.utils.get_class(world_model_cfg.observation_model.mlp_layer_norm.cls),
layer_norm_kw=world_model_cfg.observation_model.mlp_layer_norm.kw,
)
Expand All @@ -1101,7 +1101,7 @@ def build_agent(
input_dims=latent_state_size,
output_dim=world_model_cfg.reward_model.bins,
hidden_sizes=[world_model_cfg.reward_model.dense_units] * world_model_cfg.reward_model.mlp_layers,
activation=eval(world_model_cfg.reward_model.dense_act),
activation=hydra.utils.get_class(world_model_cfg.reward_model.dense_act),
layer_args={"bias": reward_ln_cls == nn.Identity},
flatten_dim=None,
norm_layer=reward_ln_cls,
Expand All @@ -1116,7 +1116,7 @@ def build_agent(
input_dims=latent_state_size,
output_dim=1,
hidden_sizes=[world_model_cfg.discount_model.dense_units] * world_model_cfg.discount_model.mlp_layers,
activation=eval(world_model_cfg.discount_model.dense_act),
activation=hydra.utils.get_class(world_model_cfg.discount_model.dense_act),
layer_args={"bias": discount_ln_cls == nn.Identity},
flatten_dim=None,
norm_layer=discount_ln_cls,
Expand All @@ -1141,7 +1141,7 @@ def build_agent(
init_std=actor_cfg.init_std,
min_std=actor_cfg.min_std,
dense_units=actor_cfg.dense_units,
activation=eval(actor_cfg.dense_act),
activation=hydra.utils.get_class(actor_cfg.dense_act),
mlp_layers=actor_cfg.mlp_layers,
distribution_cfg=cfg.distribution,
layer_norm_cls=hydra.utils.get_class(actor_cfg.layer_norm.cls),
Expand All @@ -1155,7 +1155,7 @@ def build_agent(
input_dims=latent_state_size,
output_dim=critic_cfg.bins,
hidden_sizes=[critic_cfg.dense_units] * critic_cfg.mlp_layers,
activation=eval(critic_cfg.dense_act),
activation=hydra.utils.get_class(critic_cfg.dense_act),
layer_args={"bias": critic_ln_cls == nn.Identity},
flatten_dim=None,
norm_layer=critic_ln_cls,
Expand Down
Loading

0 comments on commit 31a8e9b

Please sign in to comment.