MXNet

API

 mxnet.gluon / ParameterDict / gluon.ParameterDict


gluon.ParameterDict

class mxnet.gluon.ParameterDict(prefix='', shared=None)[source]

Bases: object

A dictionary managing a set of parameters.

Parameters
  • prefix (str, default '') – The prefix to be prepended to all Parameters’ names created by this dict.

  • shared (ParameterDict or None) – If not None, when this dict’s get() method creates a new parameter, will first try to retrieve it from “shared” dict. Usually used for sharing parameters with another Block.

Methods

get(name, **kwargs)

Retrieves a Parameter with name self.prefix+name.

get_constant(name[, value])

Retrieves a Constant with name self.prefix+name.

initialize([init, ctx, verbose, force_reinit])

Initializes all Parameters managed by this dictionary to be used for NDArray API.

list_ctx()

Returns a list of all the contexts on which the underlying Parameters are initialized.

load(filename[, ctx, allow_missing, …])

Load parameters from file.

load_dict(param_dict[, ctx, allow_missing, …])

Load parameters from dict

reset_ctx(ctx)

Re-assign all Parameters to other contexts.

save(filename[, strip_prefix])

Save parameters to file.

setattr(name, value)

Set an attribute to a new value for all Parameters.

update(other)

Copies all Parameters in other to self.

zero_grad()

Sets all Parameters’ gradient buffer to 0.

Attributes

prefix

Prefix of this dict.

get(name, **kwargs)[source]

Retrieves a Parameter with name self.prefix+name. If not found, get() will first try to retrieve it from “shared” dict. If still not found, get() will create a new Parameter with key-word arguments and insert it to self.

Parameters
  • name (str) – Name of the desired Parameter. It will be prepended with this dictionary’s prefix.

  • **kwargs (dict) – The rest of key-word arguments for the created Parameter.

Returns

The created or retrieved Parameter.

Return type

Parameter

get_constant(name, value=None)[source]

Retrieves a Constant with name self.prefix+name. If not found, get() will first try to retrieve it from “shared” dict. If still not found, get() will create a new Constant with key-word arguments and insert it to self.

Parameters
  • name (str) – Name of the desired Constant. It will be prepended with this dictionary’s prefix.

  • value (array-like) – Initial value of constant.

Returns

The created or retrieved Constant.

Return type

Constant

initialize(init=<mxnet.initializer.Uniform object>, ctx=None, verbose=False, force_reinit=False)[source]

Initializes all Parameters managed by this dictionary to be used for NDArray API. It has no effect when using Symbol API.

Parameters
  • init (Initializer) – Global default Initializer to be used when Parameter.init() is None. Otherwise, Parameter.init() takes precedence.

  • ctx (Context or list of Context) – Keeps a copy of Parameters on one or many context(s).

  • verbose (bool, default False) – Whether to verbosely print out details on initialization.

  • force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.

list_ctx()[source]

Returns a list of all the contexts on which the underlying Parameters are initialized.

load(filename, ctx=None, allow_missing=False, ignore_extra=False, restore_prefix='', cast_dtype=False, dtype_source='current')[source]

Load parameters from file.

Parameters
  • filename (str) – Path to parameter file.

  • ctx (Context or list of Context) – Context(s) initialize loaded parameters on.

  • allow_missing (bool, default False) – Whether to silently skip loading parameters not represents in the file.

  • ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this ParameterDict.

  • restore_prefix (str, default '') – prepend prefix to names of stored parameters before loading.

  • cast_dtype (bool, default False) – Cast the data type of the parameter

  • dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters

load_dict(param_dict, ctx=None, allow_missing=False, ignore_extra=False, restore_prefix='', filename=None, cast_dtype=False, dtype_source='current')[source]

Load parameters from dict

Parameters
  • param_dict (dict) – Dictionary containing model parameters, preprended with arg: and aux: names

  • ctx (Context or list of Context) – Context(s) initialize loaded parameters on.

  • allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.

  • ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this ParameterDict.

  • restore_prefix (str, default '') – prepend prefix to names of stored parameters before loading

  • filename (str, default None) –

  • cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any

property prefix

Prefix of this dict. It will be prepended to Parameter`s' name created with :py:func:`get.

reset_ctx(ctx)[source]

Re-assign all Parameters to other contexts.

Parameters

ctx (Context or list of Context, default context.current_context().) – Assign Parameter to given context. If ctx is a list of Context, a copy will be made for each context.

save(filename, strip_prefix='')[source]

Save parameters to file.

Parameters
  • filename (str) – Path to parameter file.

  • strip_prefix (str, default '') – Strip prefix from parameter names before saving.

setattr(name, value)[source]

Set an attribute to a new value for all Parameters.

For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:

model.collect_params().setattr('grad_req', 'null')

or change the learning rate multiplier:

model.collect_params().setattr('lr_mult', 0.5)
Parameters
  • name (str) – Name of the attribute.

  • value (valid type for attribute name) – The new value for the attribute.

update(other)[source]

Copies all Parameters in other to self.

zero_grad()[source]

Sets all Parameters’ gradient buffer to 0.


此页内容是否对您有帮助