glasses.utils package

Subpackages

Submodules

glasses.utils.ModuleTransfer module

class glasses.utils.ModuleTransfer.ModuleTransfer(src: torch.nn.modules.module.Module, dest: torch.nn.modules.module.Module, verbose: int = 0, src_skip: List = <factory>, dest_skip: List = <factory>)[source]

Bases: object

This class transfers the weight from one module to another assuming they have the same set of operations but they were defined in a different way.

:Examples

>>> import torch
>>> import torch.nn as nn
>>> from eyes.utils import ModuleTransfer
>>> model_a = nn.Sequential(nn.Linear(1, 64), nn.ReLU(), nn.Linear(64,10), nn.ReLU())
>>> def block(in_features, out_features):
>>>     return nn.Sequential(nn.Linear(in_features, out_features),
                        nn.ReLU())
>>> model_b = nn.Sequential(block(1,64), block(64,10))
>>> # model_a and model_b are the same thing but defined in two different ways
>>> x = torch.ones(1, 1)
>>> trans = ModuleTransfer(src=model_a, dest=model_b)
>>> trans(x)

# now module_b has the same weight of model_a

__call__(x: torch.Tensor)[source]

Transfer the weights of self.src to self.dest by performing a forward pass using x as input. Under the hood we tracked all the operations in booth modules. :param x: [The input to the modules] :type x: torch.tensor

dest: torch.nn.modules.module.Module
dest_skip: List
src: torch.nn.modules.module.Module
src_skip: List
verbose: int = 0

glasses.utils.Storage module

class glasses.utils.Storage.BackwardModuleStorage(*args, **kwargs)[source]

Bases: glasses.utils.Storage.ModuleStorage

class glasses.utils.Storage.ForwardModuleStorage(module, *args, **kwargs)[source]

Bases: glasses.utils.Storage.ModuleStorage

class glasses.utils.Storage.ModuleStorage(where2layers, debug=False)[source]

Bases: object

clear()[source]
hook(m, i, o, name)[source]
keys()[source]
property layers

Flat all the layers in the same array

property names
register_hooks(how='forward')[source]

Loop in all the layers and register a hook. There is ONLY one hook per layer to improve performance.

class glasses.utils.Storage.MutipleKeysDict[source]

Bases: collections.OrderedDict

Allow to get values from multiple keys. Example:

`python d = MutipleKeysDict({ 'a' : 1, 'b' : 2, 'c' : 3}) d[['a', 'b']] # out [1,2] `

glasses.utils.Tracker module

class glasses.utils.Tracker.Tracker(module: torch.nn.modules.module.Module, traced: List[torch.nn.modules.module.Module] = <factory>, handles: list = <factory>)[source]

Bases: object

This class tracks all the operations of a given module by performing a forward pass.

Example

>>> import torch
>>> import torch.nn as nn
>>> from glasses.utils import Tracker
>>> model = nn.Sequential(nn.Linear(1, 64), nn.ReLU(), nn.Linear(64,10), nn.ReLU())
>>> tr = Tracker(model)
>>> tr(x)
>>> print(tr.traced) # all operations
>>> print('-----')
>>> print(tr.parametrized) # all operations with learnable params

outputs

[Linear(in_features=1, out_features=64, bias=True), ReLU(), Linear(in_features=64, out_features=10, bias=True), ReLU()] ----- [Linear(in_features=1, out_features=64, bias=True), Linear(in_features=64, out_features=10, bias=True)]

handles: list
module: torch.nn.modules.module.Module
property parametrized
traced: List[torch.nn.modules.module.Module]

Module contents