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>)[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(MyModel, input_size=(1, 28, 28), output_size=(10,))
Attributes: - device
- dtype
- model
Methods
__call__(self, \*args, \*\*kwargs)Call self as a function. add_module(self, name, module)Adds a child module to the current module. apply(self, fn)Applies fnrecursively to every submodule (as returned by.children()) as well as self.buffers(self[, recurse])Returns an iterator over module buffers. children(self)Returns an iterator over immediate children modules. cpu(self)Moves all model parameters and buffers to the CPU. cuda(self[, device])Moves all model parameters and buffers to the GPU. double(self)Casts all floating point parameters and buffers to doubledatatype.eval(self)Sets the module in evaluation mode. extra_repr(self)Set the extra representation of the module float(self)Casts all floating point parameters and buffers to float datatype. forward(self, \*input, \*\*kwargs)Defines the computation performed at every call. get_current_space(self)Get currently defined parameter space get_space(self)Return hyper parameter space half(self)Casts all floating point parameters and buffers to halfdatatype.load_state_dict(self, state_dict[, strict])Copies parameters and buffers from state_dictinto this module and its descendants.modules(self)Returns an iterator over all modules in the network. named_buffers(self[, prefix, recurse])Returns an iterator over module buffers, yielding both the name of the buffer as well as the buffer itself. named_children(self)Returns an iterator over immediate children modules, yielding both the name of the module as well as the module itself. named_modules(self[, memo, prefix])Returns an iterator over all modules in the network, yielding both the name of the module as well as the module itself. named_parameters(self[, prefix, recurse])Returns an iterator over module parameters, yielding both the name of the parameter as well as the parameter itself. parameters(self, recurse)Returns an iterator over module parameters. register_backward_hook(self, hook)Registers a backward hook on the module. register_buffer(self, name, tensor)Adds a persistent buffer to the module. register_forward_hook(self, hook)Registers a forward hook on the module. register_forward_pre_hook(self, hook)Registers a forward pre-hook on the module. register_parameter(self, name, param)Adds a parameter to the module. requires_grad_(self[, requires_grad])Change if autograd should record operations on parameters in this module. state_dict(self[, destination, prefix, …])Returns a dictionary containing a whole state of the module. to(self, \*args, \*\*kwargs)Moves and/or casts the parameters and buffers. train(self[, mode])Sets the module in training mode. type(self, dst_type)Casts all parameters and buffers to dst_type.zero_grad(self)Sets gradients of all model parameters to zero. act critic init share_memory -
MODEL_BASE_SPACE= {'initializer': "choices(['uniform', 'normal', 'orthogonal', 'glorot_uniform', 'glorot_normal', 'kinit_uniform', 'kinit_normal'])"}¶
-
device¶
-
dtype¶
-
forward(self, *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(self, 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.- Arguments:
- 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
-
model¶
-
parameters(self, 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.data), param.size()) <class 'torch.FloatTensor'> (20L,) <class 'torch.FloatTensor'> (20L, 1L, 5L, 5L)
-
state_dict(self, 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.
- Returns:
- dict:
- a dictionary containing a whole state of the module
Example:
>>> module.state_dict().keys() ['bias', 'weight']
-
to(self, *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]
Its signature is similar to
torch.Tensor.to(), but only accepts floating point desireddtypes. In addition, this method will only cast the floating point 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 type of - the floating point 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
- device (
- Returns:
- Module: self
Example:
>>> 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)
-