Parameters

Parameters in Pyro are basically thin wrappers around PyTorch Variables that carry unique names. As such Parameters are the primary stateful objects in Pyro. Users typically interact with parameters via the Pyro primitive pyro.param. Parameters play a central role in stochastic variational inference, where they are used to represent point estimates for the parameters in parameterized families of models and guides.

ParamStore

class ParamStoreDict[source]

Bases: object

Global store for parameters in Pyro. This is basically a key-value store. The typical user interacts with the ParamStore primarily through the primitive pyro.param.

See Intro Part II for further discussion and SVI Part I for some examples.

Some things to bear in mind when using parameters in Pyro:

  • parameters must be assigned unique names
  • the init_tensor argument to pyro.param is only used the first time that a given (named) parameter is registered with Pyro.
  • for this reason, a user may need to use the clear() method if working in a REPL in order to get the desired behavior. this method can also be invoked with pyro.clear_param_store().
  • the internal name of a parameter within a PyTorch nn.Module that has been registered with Pyro is prepended with the Pyro name of the module. so nothing prevents the user from having two different modules each of which contains a parameter named weight. by contrast, a user can only have one top-level parameter named weight (outside of any module).
  • parameters can be ‘tagged’ with (string) tags. by default each parameter is tagged with the ‘default’ tag. this mechanism allows the user to group parameters together and e.g. customize learning rates for different tags. for an example where this is useful see the tutorial SVI Part III.
  • parameters can be saved and loaded from disk using save and load.
clear()[source]

Clear the ParamStore

delete_tag(tag)[source]

Removes the tag; any parameters with that tag are unaffected but are no longer associated with that tag.

Parameters:tag (str) – tag to remove
get_active_params(tags=None)[source]
Parameters:tag – optional argument specifying that only active params carrying a particular tag or any of several tags should be returned
Returns:all active params in the ParamStore, possibly filtered to a particular tag or tags
Return type:set
get_all_param_names()[source]

Get all parameter names in the ParamStore

get_param(name, init_tensor=None, tags='default')[source]

Get parameter from its name. If it does not yet exist in the ParamStore, it will be created and stored. The Pyro primitive pyro.param dispatches to this method.

Parameters:
  • name (str) – parameter name
  • init_tensor (torch.autograd.Variable) – initial tensor
  • tags (a string or iterable of strings) – the tag(s) to assign to the parameter
Returns:

parameter

Return type:

torch.autograd.Variable

get_param_tags(param_name)[source]

Return the tags associated with the parameter

Parameters:param_name (str) – a (single) parameter name
Return type:set
get_state()[source]

Get the ParamStore state.

load(filename)[source]

Loads parameters from disk

Parameters:filename – file name to load from
mark_params_active(params)[source]
Parameters:params – iterable of params the user wishes to mark as active in the ParamStore. this information is used to determine which parameters are being optimized, e.g. in the context of pyro.infer.SVI
mark_params_inactive(params)[source]
Parameters:params – iterable of params the user wishes to mark as inactive in the ParamStore. this information is used to determine which parameters are being optimized, e.g. in the context of pyro.infer.SVI
named_parameters()[source]

Returns an iterator over tuples of the form (name, parameter) for each parameter in the ParamStore

param_name(p)[source]

Get parameter name from parameter

Parameters:p – parameter
Returns:parameter name
replace_param(param_name, new_param, old_param)[source]

Replace the param param_name with current value old_param with the new value new_param

Parameters:
  • param_name (str) – parameter name
  • new_param (torch.autograd.Variable) – the paramater to be put into the ParamStore
  • old_param – the paramater to be removed from the ParamStore
save(filename)[source]

Save parameters to disk

Parameters:filename – file name to save to
set_state(state)[source]

Set the ParamStore state using state from a previous get_state() call

tag_params(param_names, tags)[source]

Tags the parameter(s) specified by param_names with the tag(s) specified by tags.

Parameters:
  • param_name – either a single parameter name or an iterable of parameter names
  • tags – either a single string or an iterable of strings
untag_params(param_names, tags)[source]

Disassociates the parameter(s) specified by param_names with the tag(s) specified by tags.

Parameters:
  • param_name – either a single parameter name or an iterable of parameter names
  • tags – either a single string or an iterable of strings