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¶
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]¶