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
TRPOandCPOalgorithm. 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
TRPOandCPOalgorithm. 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_wandbandTrue, then the output dict will be{'train_cfgs': {'use_wandb': True}}.- Parameters:
key_list (list) – list of keys.
value – value.
- omnisafe.utils.tools.load_yaml(path)[source]#
Get the default kwargs from
yamlfile.Note
This function search the
yamlfile 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
yamlfile.- Return type:
dict
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.benchmarkortorch.backends.cudnn. deterministicand yourcudaversion is over 10.2, you need to set theCUBLAS_WORKSPACE_CONFIGandPYTHONHASHSEEDenvironment variables.