OmniSafe Tools#

Algorithms Tools#

Documentation

omnisafe.utils.tools.get_flat_params_from(model)[source]#

This function is used to get the flattened parameters from the model.

Note

Some algorithms need to get the flattened parameters from the model, such as the TRPO and CPO algorithm. In these algorithms, the parameters are flattened and then used to calculate the loss.

Example

>>> model = torch.nn.Linear(2, 2)
>>> model.weight.data = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
>>> get_flat_params_from(model)
tensor([1., 2., 3., 4.])
Parameters:

model (torch.nn.Module) – model to be flattened.

Return type:

Tensor

omnisafe.utils.tools.get_flat_gradients_from(model)[source]#

This function is used to get the flattened gradients from the model.

Note

Some algorithms need to get the flattened gradients from the model, such as the TRPO and CPO algorithm. In these algorithms, the gradients are flattened and then used to calculate the loss.

Parameters:

model (torch.nn.Module) – model to be flattened.

Return type:

Tensor

omnisafe.utils.tools.set_param_values_to_model(model, vals)[source]#

This function is used to set the parameters to the model.

Note

Some algorithms (e.g. TRPO, CPO, etc.) need to set the parameters to the model, instead of using the optimizer.step().

Example

>>> model = torch.nn.Linear(2, 2)
>>> model.weight.data = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
>>> vals = torch.tensor([1.0, 2.0, 3.0, 4.0])
>>> set_param_values_to_model(model, vals)
>>> model.weight.data
tensor([[1., 2.],
        [3., 4.]])
Parameters:
  • model (torch.nn.Module) – model to be set.

  • vals (torch.Tensor) – parameters to be set.

Return type:

None

Config Tools#

Documentation

omnisafe.utils.tools.custom_cfgs_to_dict(key_list, value)[source]#

This function is used to convert the custom configurations to dict.

Note

This function is used to convert the custom configurations to dict. For example, if the custom configurations are train_cfgs:use_wandb and True, then the output dict will be {'train_cfgs': {'use_wandb': True}}.

Parameters:
  • key_list (list) – list of keys.

  • value – value.

omnisafe.utils.tools.update_dic(total_dic, item_dic)[source]#

Updater of multi-level dictionary.

omnisafe.utils.tools.load_yaml(path)[source]#

Get the default kwargs from yaml file.

Note

This function search the yaml file by the algorithm name and environment name. Make sure your new implemented algorithm or environment has the same name as the yaml file.

Parameters:

path (str) – path of the yaml file.

Return type:

dict

omnisafe.utils.tools.recursive_check_config(config, default_config, exclude_keys=())[source]#

Check whether config is valid in default_config.

Parameters:
  • config (dict) – config to be checked.

  • default_config (dict) – default config.

Seed Tools#

Documentation

omnisafe.utils.tools.seed_all(seed)[source]#

This function is used to set the random seed for all the packages.

Hint

To reproduce the results, you need to set the random seed for all the packages. Including numpy, random, torch, torch.cuda, torch.backends.cudnn.

Warning

If you want to use the torch.backends.cudnn.benchmark or torch.backends.cudnn. deterministic and your cuda version is over 10.2, you need to set the CUBLAS_WORKSPACE_CONFIG and PYTHONHASHSEED environment variables.

Experiment Grid Tools#

Documentation

omnisafe.utils.exp_grid_tools.all_bools(vals)[source]#

Check if all values are bools

Parameters:

vals (list) – Values to check.

Return type:

bool

omnisafe.utils.exp_grid_tools.valid_str(vals)[source]#

Convert a value or values to a string which could go in a path of file.

Partly based on this gist.

Parameters:

vals (Union[List, str]) – Value or values to convert to a string.

Return type:

str