utils

distributions

class xuance.mindspore.utils.distributions.ActivatedDiagGaussianDistribution(action_dim: int, activation_action)[源代码]

基类:DiagGaussianDistribution

activated_rsample()[源代码]
activated_rsample_and_logprob()[源代码]
class xuance.mindspore.utils.distributions.CategoricalDistribution(action_dim: int)[源代码]

基类:Distribution

deterministic_sample()[源代码]
entropy()[源代码]
get_param()[源代码]
kl_divergence(other: Distribution)[源代码]
log_prob(x)[源代码]
set_param(probs=None, logits=None)[源代码]
stochastic_sample()[源代码]
class xuance.mindspore.utils.distributions.DiagGaussianDistribution(action_dim: int)[源代码]

基类:Distribution

deterministic_sample()[源代码]
entropy()[源代码]
get_param()[源代码]
kl_divergence(other: Distribution)[源代码]
log_prob(x: mindspore.Tensor)[源代码]
set_param(mu, std)[源代码]
stochastic_sample()[源代码]
class xuance.mindspore.utils.distributions.Distribution[源代码]

基类:ABC

abstract deterministic_sample()[源代码]
abstract entropy()[源代码]
abstract get_param()[源代码]
abstract log_prob(x: mindspore.Tensor)[源代码]
abstract set_param(*args)[源代码]
abstract stochastic_sample()[源代码]
xuance.mindspore.utils.distributions.merge_distributions(distribution_list)[源代码]
xuance.mindspore.utils.distributions.split_distributions(distribution)[源代码]

harmonizer

class xuance.mindspore.utils.harmonizer.Harmonizer(device)[源代码]

基类:Module

Learnable parameter for loss_scale balancing Ref: https://github.com/thuml/HarmonyDream/blob/main/dreamerv3-jax/dreamerv3/nets.py

forward(loss: Tensor, regularize=True)[源代码]

Define the computation performed at every call.

Should be overridden by all subclasses.

备注

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

get_harmony()[源代码]
training: bool

layers

xuance.mindspore.utils.layers.cnn_block(input_shape: Sequence[int], filter: int, kernel_size: int, stride: int, normalize: mindspore.nn.BatchNorm2d | mindspore.nn.LayerNorm | mindspore.nn.GroupNorm | mindspore.nn.InstanceNorm2d | None = None, activation: Type[mindspore.nn.Cell] | None = None, initialize: Callable[[mindspore.Tensor], mindspore.Tensor] | None = None) Tuple[Sequence[Type[mindspore.nn.Cell]], Tuple][源代码]
xuance.mindspore.utils.layers.gru_block(input_dim: int, output_dim: int, num_layers: int = 1, dropout: float = 0, initialize: Callable[[mindspore.Tensor], mindspore.Tensor] | None = None) Tuple[mindspore.nn.Cell, int][源代码]
xuance.mindspore.utils.layers.lstm_block(input_dim: int, output_dim: int, num_layers: int = 1, dropout: float = 0, initialize: Callable[[mindspore.Tensor], mindspore.Tensor] | None = None) Tuple[mindspore.nn.Cell, int][源代码]
xuance.mindspore.utils.layers.mlp_block(input_dim: int, output_dim: int, normalize: mindspore.nn.BatchNorm1d | mindspore.nn.LayerNorm | None = None, activation: Type[mindspore.nn.Cell] | None = None, initialize: Callable[[mindspore.Tensor], mindspore.Tensor] | None = None) Tuple[Sequence[Type[mindspore.nn.Cell]], Tuple[int]][源代码]
xuance.mindspore.utils.layers.pooling_block(input_shape: Sequence[int], scale: int, pooling: mindspore.nn.AdaptiveMaxPool2d | mindspore.nn.AdaptiveAvgPool2d) Sequence[Type[mindspore.nn.Cell]][源代码]

layers4dreamer

Adapted from: https://github.com/thu-ml/tianshou/blob/master/tianshou/utils/net/common.py

class xuance.mindspore.utils.layers4dreamer.CNN(input_channels: int, hidden_channels: ~typing.Sequence[int], cnn_layer: ~typing.Type[~torch.nn.modules.module.Module] | None = <class 'torch.nn.modules.conv.Conv2d'>, layer_args: ~typing.Tuple[~typing.Any, ...] | ~typing.Dict[~typing.Any, ~typing.Any] | None | ~typing.List[~typing.Tuple[~typing.Any, ...] | ~typing.Dict[~typing.Any, ~typing.Any] | None] = None, dropout_layer: ~typing.Type[~torch.nn.modules.module.Module] | None | ~typing.Sequence[~typing.Type[~torch.nn.modules.module.Module] | None] = None, dropout_args: ~typing.Tuple[~typing.Any, ...] | ~typing.Dict[~typing.Any, ~typing.Any] | None | ~typing.List[~typing.Tuple[~typing.Any, ...] | ~typing.Dict[~typing.Any, ~typing.Any] | None] = None, norm_layer: ~typing.Type[~torch.nn.modules.module.Module] | None | ~typing.Sequence[~typing.Type[~torch.nn.modules.module.Module] | None] = None, norm_args: ~typing.Tuple[~typing.Any, ...] | ~typing.Dict[~typing.Any, ~typing.Any] | None | ~typing.List[~typing.Tuple[~typing.Any, ...] | ~typing.Dict[~typing.Any, ~typing.Any] | None] = None, activation: ~typing.Type[~torch.nn.modules.module.Module] | None | ~typing.Sequence[~typing.Type[~torch.nn.modules.module.Module] | None] = <class 'torch.nn.modules.activation.ReLU'>, act_args: ~typing.Tuple[~typing.Any, ...] | ~typing.Dict[~typing.Any, ~typing.Any] | None | ~typing.List[~typing.Tuple[~typing.Any, ...] | ~typing.Dict[~typing.Any, ~typing.Any] | None] = None)[源代码]

基类:Module

Simple CNN backbone.

参数:
  • input_channels (int) – dimensions of the input channels.

  • hidden_channels (Sequence[int], optional) – intermediate number of channels for the CNN, including the output channels.

  • dropout_layer (Union[ModuleType, Sequence[ModuleType]], optional) – which dropout layer to be used before activation (possibly before the normalization layer), e.g., nn.Dropout. You can also pass a list of dropout modules with the same length of hidden_sizes to use different dropout modules in different layers. If None, then no dropout layer is used. Defaults to None.

  • norm_layer (Union[ModuleType, Sequence[ModuleType]], optional) – which normalization layer to be used before activation, e.g., nn.LayerNorm and nn.BatchNorm1d. You can also pass a list of normalization modules with the same length of hidden_sizes to use different normalization modules in different layers. If None, then no normalization layer is used. Defaults to None.

  • activation (Union[ModuleType, Sequence[ModuleType]], optional) – which activation to use after each layer, can be both the same activation for all layers if a single nn.Module is passed, or different activations for different layers if a list is passed. Defaults to nn.ReLU.

forward(obs: Tensor) Tensor[源代码]

Define the computation performed at every call.

Should be overridden by all subclasses.

备注

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

property model: Module
property output_dim: int
training: bool
class xuance.mindspore.utils.layers4dreamer.DeCNN(input_channels: int, hidden_channels: ~typing.Sequence[int] = (), cnn_layer: ~typing.Type[~torch.nn.modules.module.Module] | None = <class 'torch.nn.modules.conv.ConvTranspose2d'>, layer_args: ~typing.Tuple[~typing.Any, ...] | ~typing.Dict[~typing.Any, ~typing.Any] | None | ~typing.List[~typing.Tuple[~typing.Any, ...] | ~typing.Dict[~typing.Any, ~typing.Any] | None] = None, dropout_layer: ~typing.Type[~torch.nn.modules.module.Module] | None | ~typing.Sequence[~typing.Type[~torch.nn.modules.module.Module] | None] = None, dropout_args: ~typing.Tuple[~typing.Any, ...] | ~typing.Dict[~typing.Any, ~typing.Any] | None | ~typing.List[~typing.Tuple[~typing.Any, ...] | ~typing.Dict[~typing.Any, ~typing.Any] | None] = None, norm_layer: ~typing.Type[~torch.nn.modules.module.Module] | None | ~typing.Sequence[~typing.Type[~torch.nn.modules.module.Module] | None] = None, norm_args: ~typing.Tuple[~typing.Any, ...] | ~typing.Dict[~typing.Any, ~typing.Any] | None | ~typing.List[~typing.Tuple[~typing.Any, ...] | ~typing.Dict[~typing.Any, ~typing.Any] | None] = None, activation: ~typing.Type[~torch.nn.modules.module.Module] | None | ~typing.Sequence[~typing.Type[~torch.nn.modules.module.Module] | None] = <class 'torch.nn.modules.activation.ReLU'>, act_args: ~typing.Tuple[~typing.Any, ...] | ~typing.Dict[~typing.Any, ~typing.Any] | None | ~typing.List[~typing.Tuple[~typing.Any, ...] | ~typing.Dict[~typing.Any, ~typing.Any] | None] = None)[源代码]

基类:Module

Simple DeCNN backbone.

参数:
  • input_channels (int) – dimensions of the input channels.

  • hidden_channels (Sequence[int], optional) – intermediate number of channels for the CNN, including the output channels.

  • dropout_layer (Union[ModuleType, Sequence[ModuleType]], optional) – which dropout layer to be used before activation (possibly before the normalization layer), e.g., nn.Dropout. You can also pass a list of dropout modules with the same length of hidden_sizes to use different dropout modules in different layers. If None, then no dropout layer is used. Defaults to None.

  • norm_layer (Union[ModuleType, Sequence[ModuleType]], optional) – which normalization layer to be used before activation, e.g., nn.LayerNorm and nn.BatchNorm1d. You can also pass a list of normalization modules with the same length of hidden_sizes to use different normalization modules in different layers. If None, then no normalization layer is used. Defaults to None.

  • activation (Union[ModuleType, Sequence[ModuleType]], optional) – which activation to use after each layer, can be both the same activation for all layers if a single nn.Module is passed, or different activations for different layers if a list is passed. Defaults to nn.ReLU.

forward(obs: Tensor) Tensor[源代码]

Define the computation performed at every call.

Should be overridden by all subclasses.

备注

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

property model: Module
property output_dim: int
training: bool
class xuance.mindspore.utils.layers4dreamer.LayerNorm(normalized_shape: int | List[int] | Size, eps: float = 1e-05, elementwise_affine: bool = True, bias: bool = True, device=None, dtype=None)[源代码]

基类:LayerNorm

elementwise_affine: bool
eps: float
forward(x: Tensor) Tensor[源代码]

Define the computation performed at every call.

Should be overridden by all subclasses.

备注

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

normalized_shape: Tuple[int, ...]
class xuance.mindspore.utils.layers4dreamer.LayerNormChannelLast(*args, **kwargs)[源代码]

基类:LayerNorm

elementwise_affine: bool
eps: float
forward(x: Tensor) Tensor[源代码]

Define the computation performed at every call.

Should be overridden by all subclasses.

备注

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

normalized_shape: Tuple[int, ...]
class xuance.mindspore.utils.layers4dreamer.LayerNormGRUCell(input_size: int, hidden_size: int, bias: bool = True, batch_first: bool = False, layer_norm_cls: ~typing.Callable[[...], ~torch.nn.modules.module.Module] = <class 'torch.nn.modules.linear.Identity'>, layer_norm_kw: ~typing.Dict[str, ~typing.Any] = {})[源代码]

基类:Module

A GRU cell with a LayerNorm, taken from https://github.com/danijar/dreamerv2/blob/main/dreamerv2/common/nets.py#L317.

This particular GRU cell accepts 3-D inputs, with a sequence of length 1, and applies a LayerNorm after the projection of the inputs.

参数:
  • input_size (int) – the input size.

  • hidden_size (int) – the hidden state size

  • bias (bool, optional) – whether to apply a bias to the input projection. Defaults to True.

  • batch_first (bool, optional) – whether the first dimension represent the batch dimension or not. Defaults to False.

  • layer_norm_cls (Callable[..., nn.Module]) – the layer norm to apply after the input projection. Defaults to nn.Identiy.

  • layer_norm_kw (Dict[str, Any]) – the kwargs of the layer norm. Default to {}.

forward(input: Tensor, hx: Tensor | None = None) Tensor[源代码]

Define the computation performed at every call.

Should be overridden by all subclasses.

备注

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class xuance.mindspore.utils.layers4dreamer.MLP(input_dims: int | ~typing.Sequence[int], output_dim: int | None = None, hidden_sizes: ~typing.Sequence[int] = (), layer_args: ~typing.Tuple[~typing.Any, ...] | ~typing.Dict[~typing.Any, ~typing.Any] | None | ~typing.List[~typing.Tuple[~typing.Any, ...] | ~typing.Dict[~typing.Any, ~typing.Any] | None] = None, dropout_layer: ~typing.Type[~torch.nn.modules.module.Module] | None | ~typing.Sequence[~typing.Type[~torch.nn.modules.module.Module] | None] = None, dropout_args: ~typing.Tuple[~typing.Any, ...] | ~typing.Dict[~typing.Any, ~typing.Any] | None | ~typing.List[~typing.Tuple[~typing.Any, ...] | ~typing.Dict[~typing.Any, ~typing.Any] | None] = None, norm_layer: ~typing.Type[~torch.nn.modules.module.Module] | None | ~typing.Sequence[~typing.Type[~torch.nn.modules.module.Module] | None] = None, norm_args: ~typing.Tuple[~typing.Any, ...] | ~typing.Dict[~typing.Any, ~typing.Any] | None | ~typing.List[~typing.Tuple[~typing.Any, ...] | ~typing.Dict[~typing.Any, ~typing.Any] | None] = None, activation: ~typing.Type[~torch.nn.modules.module.Module] | None | ~typing.Sequence[~typing.Type[~torch.nn.modules.module.Module] | None] = <class 'torch.nn.modules.activation.ReLU'>, act_args: ~typing.Tuple[~typing.Any, ...] | ~typing.Dict[~typing.Any, ~typing.Any] | None | ~typing.List[~typing.Tuple[~typing.Any, ...] | ~typing.Dict[~typing.Any, ~typing.Any] | None] = None, flatten_dim: int | None = None)[源代码]

基类:Module

Simple MLP backbone.

参数:
  • input_dims (Union[int, Sequence[int]]) – dimensions of the input vector.

  • output_dim (int, optional) – dimension of the output vector. If set to None, there is no final linear layer. Else, a final linear layer is added. Defaults to None.

  • hidden_sizes (Sequence[int], optional) – shape of MLP passed in as a list, not including input_dims and output_dim.

  • dropout_layer (Union[ModuleType, Sequence[ModuleType]], optional) – which dropout layer to be used before activation (possibly before the normalization layer), e.g., nn.Dropout. You can also pass a list of dropout modules with the same length of hidden_sizes to use different dropout modules in different layers. If None, then no dropout layer is used. Defaults to None.

  • norm_layer (Union[ModuleType, Sequence[ModuleType]], optional) – which normalization layer to be used before activation, e.g., nn.LayerNorm and nn.BatchNorm1d. You can also pass a list of normalization modules with the same length of hidden_sizes to use different normalization modules in different layers. If None, then no normalization layer is used. Defaults to None.

  • activation (Union[ModuleType, Sequence[ModuleType]], optional) – which activation to use after each layer, can be both the same activation for all layers if a single nn.Module is passed, or different activations for different layers if a list is passed. Defaults to nn.ReLU.

  • flatten_dim (int, optional) – whether to flatten input data. The flatten dimension starts from 1. Defaults to True.

property flatten_dim: int | None
forward(obs: Tensor) Tensor[源代码]

Define the computation performed at every call.

Should be overridden by all subclasses.

备注

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

property model: Module
property output_dim: int
training: bool
class xuance.mindspore.utils.layers4dreamer.MultiDecoder(cnn_decoder: Type[Module] | None, mlp_decoder: Type[Module] | None)[源代码]

基类:Module

property cnn_keys: Sequence[str]
forward(x: Tensor) Tensor[源代码]

Define the computation performed at every call.

Should be overridden by all subclasses.

备注

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

property mlp_keys: Sequence[str]
training: bool
class xuance.mindspore.utils.layers4dreamer.MultiEncoder(cnn_encoder: Type[Module] | None, mlp_encoder: Type[Module] | None)[源代码]

基类:Module

property cnn_keys: Sequence[str]
forward(obs: Dict[str, Tensor], *args, **kwargs) Tensor[源代码]

Define the computation performed at every call.

Should be overridden by all subclasses.

备注

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

property mlp_keys: Sequence[str]
training: bool
class xuance.mindspore.utils.layers4dreamer.NatureCNN(in_channels: int, features_dim: int, screen_size: int = 64)[源代码]

基类:CNN

CNN from DQN Nature paper: Mnih, Volodymyr, et al. “Human-level control through deep reinforcement learning.” Nature 518.7540 (2015): 529-533.

参数:
  • in_channels (int) – the input channels to the first convolutional layer

  • features_dim (int) – the features dimension in output from the last convolutional layer

  • screen_size (int, optional) – the dimension of the input image as a single integer. Needed to extract the features and compute the output dimension after all the convolutional layers. Defaults to 64.

forward(x: Tensor) Tensor[源代码]

Define the computation performed at every call.

Should be overridden by all subclasses.

备注

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

property output_dim: int
training: bool
xuance.mindspore.utils.layers4dreamer.cnn_forward(model: Module, input: Tensor, input_dim: Size | Tuple[int, ...], output_dim: Size | Tuple[int, ...]) Tensor[源代码]

Compute the forward of a Convolutional neural network. It flattens all the dimensions before the model input_size, i.e., the dimensions before the (C_in, H, W) dimensions for the encoder and the dimensions before the (feature_size,) dimension for the decoder.

参数:
  • model (nn.Module) – the model.

  • input (Tensor) – the input tensor of dimension (*, C_in, H, W) or (*, feature_size), where * means any number of dimensions including None.

  • input_dim (Union[torch.Size, Tuple[int, ...]]) – the input dimensions, i.e., either (C_in, H, W) or (feature_size,).

  • output_dim (Union[torch.Size, Tuple[int, ...]]) – the desired dimensions in output.

返回:

The output of dimensions (*, *output_dim).

示例

>>> encoder
CNN(
    (network): Sequential(
        (0): Conv2d(3, 4, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
        (1): ReLU()
        (2): Conv2d(4, 8, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
        (3): ReLU()
        (4): Flatten(start_dim=1, end_dim=-1)
        (5): Linear(in_features=128, out_features=25, bias=True)
    )
)
>>> input = torch.rand(10, 20, 3, 4, 4)
>>> cnn_forward(encoder, input, (3, 4, 4), -1).shape
torch.Size([10, 20, 25])
>>> decoder
Sequential(
    (0): Linear(in_features=230, out_features=1024, bias=True)
    (1): Unflatten(dim=-1, unflattened_size=(1024, 1, 1))
    (2): ConvTranspose2d(1024, 128, kernel_size=(5, 5), stride=(2, 2))
    (3): ReLU()
    (4): ConvTranspose2d(128, 64, kernel_size=(5, 5), stride=(2, 2))
    (5): ReLU()
    (6): ConvTranspose2d(64, 32, kernel_size=(6, 6), stride=(2, 2))
    (7): ReLU()
    (8): ConvTranspose2d(32, 3, kernel_size=(6, 6), stride=(2, 2))
)
>>> input = torch.rand(10, 20, 230)
>>> cnn_forward(decoder, input, (230,), (3, 64, 64)).shape
torch.Size([10, 20, 3, 64, 64])
xuance.mindspore.utils.layers4dreamer.create_layer_with_args(layer_type: Type[Module] | None, layer_args: Tuple[Any, ...] | Dict[Any, Any] | None) Module[源代码]

Create a single layer with given layer type and arguments.

参数:
  • layer_type (ModuleType) – the type of the layer to be created.

  • layer_args (ArgType, optional) – the arguments to be passed to the layer.

xuance.mindspore.utils.layers4dreamer.create_layers(layer_type: Type[Module] | None | List[Type[Module] | None], layer_args: Tuple[Any, ...] | Dict[Any, Any] | None | List[Tuple[Any, ...] | Dict[Any, Any] | None], num_layers: int) Tuple[List[Type[Module] | None], Tuple[Any, ...] | Dict[Any, Any] | None | List[Tuple[Any, ...] | Dict[Any, Any] | None]][源代码]

Create a list of layers with given layer type and arguments.

If a layer_type is not specified, then the lists will be filled with None. If the layer type or the layer arguments are specified only once, they will be cast to a sequence of length num_layers.

参数:
  • layer_type (Union[ModuleType, Sequence[ModuleType]]) – the type of the layer to be created.

  • layer_args (ArgsType, optional) – the arguments to be passed to the layer.

  • num_layers (int) – the number of layers to be created.

返回:

a list of layers and a list of args.

返回类型:

Tuple[Sequence[ModuleType], ArgsType]

示例

>>> create_layers(nn.Linear, None, 3)
([nn.Linear, nn.Linear, nn.Linear], [None, None, None])
>>> create_layers(nn.Linear, {"arg1":3, "arg2": "foo"}, 3)
(
    [nn.Linear, nn.Linear, nn.Linear],
    [{'arg1': 3, 'arg2': 'foo'}, {'arg1': 3, 'arg2': 'foo'}, {'arg1': 3, 'arg2': 'foo'}]
)
>>> create_layers([nn.Linear, nn.Conv2d], [{"bias":False}, {"kernel_size": 5, "bias": True}], 2)
([nn.Linear, nn.Conv2d], [{'bias': False}, {'kernel_size':5, 'bias': True}])
>>> create_layers([nn.Linear, nn.Linear], (64, 10), 2)
([nn.Linear, nn.Linear], [(64, 10), (64, 10)])
xuance.mindspore.utils.layers4dreamer.miniblock(input_size: int, output_size: int, layer_type: ~typing.Type[~torch.nn.modules.module.Module] = <class 'torch.nn.modules.linear.Linear'>, layer_args: ~typing.Tuple[~typing.Any, ...] | ~typing.Dict[~typing.Any, ~typing.Any] | None = None, dropout_layer: ~typing.Type[~torch.nn.modules.module.Module] | None = None, dropout_args: ~typing.Tuple[~typing.Any, ...] | ~typing.Dict[~typing.Any, ~typing.Any] | None = None, norm_layer: ~typing.Type[~torch.nn.modules.module.Module] | None = None, norm_args: ~typing.Tuple[~typing.Any, ...] | ~typing.Dict[~typing.Any, ~typing.Any] | None = None, activation: ~typing.Type[~torch.nn.modules.module.Module] | None = None, act_args: ~typing.Tuple[~typing.Any, ...] | ~typing.Dict[~typing.Any, ~typing.Any] | None = None) List[Module][源代码]

Construct a miniblock with given input/output-size, dropout layer, norm layer and activation function.

Based on Tianshou’s miniblock function (https://github.com/thu-ml/tianshou/blob/master/tianshou/utils/net/common.py).

参数:
  • input_size (int) – the input size of the miniblock (in_features for Linear and in_channels for Conv2d).

  • output_size (int) – the output size of the miniblock.

  • layer_type (Type[nn.Linear], optional) – the type of the layer to be created. Defaults to nn.Linear.

  • layer_args (ArgType, optional) – the arguments to be passed to the layer. Defaults to None.

  • dropout_layer (ModuleType, optional) – the type of the dropout layer to be created. Defaults to None.

  • dropout_args (ArgType, optional) – the arguments to be passed to the dropout layer. Defaults to None.

  • norm_layer (ModuleType, optional) – the type of the norm layer to be created. Defaults to None.

  • norm_args (ArgType, optional) – the arguments to be passed to the norm layer. Defaults to None.

  • activation (ModuleType, optional) – the type of the activation function to be created. Defaults to None.

  • act_args (Tuple[Any, ...] | Dict[Any, Any] | None, optional) – the arguments to be passed to the activation function. Defaults to None.

返回:

the miniblock as a list of layers.

返回类型:

List[nn.Module]

xuance.mindspore.utils.layers4dreamer.per_layer_ortho_init_weights(module: Module, gain: float = 1.0, bias: float = 0.0)[源代码]

Initialize the weights of a module with orthogonal weights.

参数:
  • module (nn.Module) – module to initialize

  • gain (float, optional) – gain of the orthogonal initialization. Defaults to 1.0.

  • bias (float, optional) – bias of the orthogonal initialization. Defaults to 0.0.

operations

xuance.mindspore.utils.operations.assign_from_flat_grads(flat_grads: mindspore.Tensor, model: mindspore.nn.Cell) mindspore.nn.Cell[源代码]
xuance.mindspore.utils.operations.assign_from_flat_params(flat_params: mindspore.Tensor, model: mindspore.nn.Cell) mindspore.nn.Cell[源代码]
xuance.mindspore.utils.operations.clip_grads(grads, low, high)[源代码]
xuance.mindspore.utils.operations.get_flat_grad(y: mindspore.Tensor, model: mindspore.nn.Cell) mindspore.Tensor[源代码]
xuance.mindspore.utils.operations.get_flat_params(model: mindspore.nn.Cell) mindspore.Tensor[源代码]
xuance.mindspore.utils.operations.set_seed(seed)[源代码]
xuance.mindspore.utils.operations.update_linear_decay(optimizer, step, total_steps, initial_lr, end_factor)[源代码]

value_norm

class xuance.mindspore.utils.value_norm.ValueNorm(*args: Any, **kwargs: Any)[源代码]

基类:Cell

Normalize a vector of observations - across the first norm_axes dimensions

denormalize(input_vector)[源代码]

Transform normalized data back into original distribution

normalize(input_vector)[源代码]
reset_parameters()[源代码]
running_mean_var()[源代码]
update(input_vector)[源代码]