Models¶
Registered Models¶
Initializations
Module contents¶
-
class
olympus.models.Model(name=None, *, half=False, model=None, input_size=None, output_size=None, weight_init=<olympus.models.inits.Initializer object>, **kwargs)[source]¶ Bases:
torch.nn.modules.module.ModuleOlympus standardized Model interface
Parameters: - name: str
Name of a registered model
- half: bool
Convert the network to half/fp16
- model: Model
Custom model to use, mutually exclusive with :param name
Raises: - RegisteredModelNotFound
when using a name of an known model
- MissingArgument:
if name nor model were not set
Examples
Model wrappers that provide a wide range of utility built-in.
Can instantiate common model directly
>>> model = Model('resnet18', input_size=(1, 28, 28), output_size=(10,))
Handles mixed precision conversion for you
>>> model = Model('resnet18', input_size=(1, 28, 28), output_size=(10,), half=True)
Handles weight initialization
>>> model = Model('resnet18', input_size=(1, 28, 28), output_size=(10,), weight_init='glorot_uniform')
Supports your custom model
>>> class MyModel(nn.Module): ... def __init__(self, input_size, output_size): ... self.main = nn.Linear(input_size[0], output_size[0]) ... ... def forward(self, x): ... return self.main(x) >>> >>> model = Model(model=MyModel, input_size=(1, 28, 28), output_size=(10,))
Attributes: - device
- dtype
- model
Methods
__call__(*args, **kwargs)Call self as a function. add_moduleAdds a child module to the current module. applyApplies fnrecursively to every submodule (as returned by.children()) as well as self.bfloat16()Casts all floating point parameters and buffers to bfloat16datatype.buffers(recurse)Returns an iterator over module buffers. childrenReturns an iterator over immediate children modules. cpu()Moves all model parameters and buffers to the CPU. cuda(device, torch.device, NoneType] = None)Moves all model parameters and buffers to the GPU. double()Casts all floating point parameters and buffers to doubledatatype.eval()Sets the module in evaluation mode. extra_repr()Set the extra representation of the module float()Casts all floating point parameters and buffers to floatdatatype.forward(*input, **kwargs)Defines the computation performed at every call. get_buffer(target)Returns the buffer given by targetif it exists, otherwise throws an error.get_current_space()Get currently defined parameter space get_extra_state()Returns any extra state to include in the module’s state_dict. get_parameter(target)Returns the parameter given by targetif it exists, otherwise throws an error.get_space()Return hyper parameter space get_submodule(target)Returns the submodule given by targetif it exists, otherwise throws an error.half()Casts all floating point parameters and buffers to halfdatatype.load_state_dict(state_dict[, strict])Copies parameters and buffers from state_dictinto this module and its descendants.modulesReturns an iterator over all modules in the network. named_buffers(prefix, recurse)Returns an iterator over module buffers, yielding both the name of the buffer as well as the buffer itself. named_childrenReturns an iterator over immediate children modules, yielding both the name of the module as well as the module itself. named_modulesReturns an iterator over all modules in the network, yielding both the name of the module as well as the module itself. named_parameters(prefix, recurse)Returns an iterator over module parameters, yielding both the name of the parameter as well as the parameter itself. parameters(recurse)Returns an iterator over module parameters. register_backward_hookRegisters a backward hook on the module. register_buffer(name, tensor, NoneType], …)Adds a buffer to the module. register_forward_hook(hook, NoneType])Registers a forward hook on the module. register_forward_pre_hook(hook, NoneType])Registers a forward pre-hook on the module. register_full_backward_hookRegisters a backward hook on the module. register_parameter(name, param, NoneType])Adds a parameter to the module. requires_grad_(requires_grad)Change if autograd should record operations on parameters in this module. set_extra_state(state)This function is called from load_state_dict()to handle any extra state found within thestate_dict.share_memory()See torch.Tensor.share_memory_()state_dict([destination, prefix, keep_vars])Returns a dictionary containing a whole state of the module. to(*args, **kwargs)Moves and/or casts the parameters and buffers. to_empty(*, device, torch.device])Moves the parameters and buffers to the specified device without copying storage. train(mode)Sets the module in training mode. type(dst_type, str])Casts all parameters and buffers to dst_type.xpu(device, torch.device, NoneType] = None)Moves all model parameters and buffers to the XPU. zero_grad(set_to_none)Sets gradients of all model parameters to zero. act critic from_state init -
device¶
-
dtype¶
-
forward(*input, **kwargs)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
load_state_dict(state_dict, strict=True)[source]¶ Copies parameters and buffers from
state_dictinto this module and its descendants. IfstrictisTrue, then the keys ofstate_dictmust exactly match the keys returned by this module’sstate_dict()function.- Args:
- state_dict (dict): a dict containing parameters and
- persistent buffers.
- strict (bool, optional): whether to strictly enforce that the keys
- in
state_dictmatch the keys returned by this module’sstate_dict()function. Default:True
- Returns:
NamedTuplewithmissing_keysandunexpected_keysfields:- missing_keys is a list of str containing the missing keys
- unexpected_keys is a list of str containing the unexpected keys
- Note:
- If a parameter or buffer is registered as
Noneand its corresponding key exists instate_dict,load_state_dict()will raise aRuntimeError.
-
model¶
-
parameters(recurse: bool = True)[source]¶ Returns an iterator over module parameters.
This is typically passed to an optimizer.
- Args:
- recurse (bool): if True, then yields parameters of this module
- and all submodules. Otherwise, yields only parameters that are direct members of this module.
- Yields:
- Parameter: module parameter
Example:
>>> for param in model.parameters(): >>> print(type(param), param.size()) <class 'torch.Tensor'> (20L,) <class 'torch.Tensor'> (20L, 1L, 5L, 5L)
-
state_dict(destination=None, prefix='', keep_vars=False)[source]¶ Returns a dictionary containing a whole state of the module.
Both parameters and persistent buffers (e.g. running averages) are included. Keys are corresponding parameter and buffer names. Parameters and buffers set to
Noneare not included.- Returns:
- dict:
- a dictionary containing a whole state of the module
Example:
>>> module.state_dict().keys() ['bias', 'weight']
-
to(*args, **kwargs)[source]¶ Moves and/or casts the parameters and buffers.
This can be called as
-
to(device=None, dtype=None, non_blocking=False)[source]
-
to(dtype, non_blocking=False)[source]
-
to(tensor, non_blocking=False)[source]
-
to(memory_format=torch.channels_last)[source]
Its signature is similar to
torch.Tensor.to(), but only accepts floating point or complexdtypes. In addition, this method will only cast the floating point or complex parameters and buffers todtype(if given). The integral parameters and buffers will be moveddevice, if that is given, but with dtypes unchanged. Whennon_blockingis set, it tries to convert/move asynchronously with respect to the host if possible, e.g., moving CPU Tensors with pinned memory to CUDA devices.See below for examples.
Note
This method modifies the module in-place.
- Args:
- device (
torch.device): the desired device of the parameters - and buffers in this module
- dtype (
torch.dtype): the desired floating point or complex dtype of - the parameters and buffers in this module
- tensor (torch.Tensor): Tensor whose dtype and device are the desired
- dtype and device for all parameters and buffers in this module
- memory_format (
torch.memory_format): the desired memory - format for 4D parameters and buffers in this module (keyword only argument)
- device (
- Returns:
- Module: self
Examples:
>>> linear = nn.Linear(2, 2) >>> linear.weight Parameter containing: tensor([[ 0.1913, -0.3420], [-0.5113, -0.2325]]) >>> linear.to(torch.double) Linear(in_features=2, out_features=2, bias=True) >>> linear.weight Parameter containing: tensor([[ 0.1913, -0.3420], [-0.5113, -0.2325]], dtype=torch.float64) >>> gpu1 = torch.device("cuda:1") >>> linear.to(gpu1, dtype=torch.half, non_blocking=True) Linear(in_features=2, out_features=2, bias=True) >>> linear.weight Parameter containing: tensor([[ 0.1914, -0.3420], [-0.5112, -0.2324]], dtype=torch.float16, device='cuda:1') >>> cpu = torch.device("cpu") >>> linear.to(cpu) Linear(in_features=2, out_features=2, bias=True) >>> linear.weight Parameter containing: tensor([[ 0.1914, -0.3420], [-0.5112, -0.2324]], dtype=torch.float16) >>> linear = nn.Linear(2, 2, bias=None).to(torch.cdouble) >>> linear.weight Parameter containing: tensor([[ 0.3741+0.j, 0.2382+0.j], [ 0.5593+0.j, -0.4443+0.j]], dtype=torch.complex128) >>> linear(torch.ones(3, 2, dtype=torch.cdouble)) tensor([[0.6122+0.j, 0.1150+0.j], [0.6122+0.j, 0.1150+0.j], [0.6122+0.j, 0.1150+0.j]], dtype=torch.complex128)
-