torchlayers package¶
-
torchlayers.build(module, *args, **kwargs)[source]¶ Build PyTorch layer or module by providing example input.
This method should be used always after creating module using
torchlayersand shape inference especially.Works similarly to
buildfunctionality provided bykeras.Provided module will be “compiled” to PyTorch primitives to remove any overhead.
torchlayersalso supportspost_buildfunction to perform some action after shape was inferred (weight initialization example below):import torch import torchlayers as tl class _MyModuleImpl(torch.nn.Linear): def post_build(self): # You can do anything here really torch.nn.init.eye_(self.weights) MyModule = tl.infer(_MyModuleImpl)
post_buildshould have no arguments other thanselfso all necessary data should be saved inmodulebeforehand.- Parameters
module (torch.nn.Module) – Instance of module to build
*args – Arguments required by module’s
forward**kwargs – Keyword arguments required by module’s
forward
-
torchlayers.infer(module_class, index: str = 1)[source]¶ Allows custom user modules to infer input shape.
Input shape should be the first argument after
self.Usually used as class decorator, e.g.:
import torch import torchlayers as tl class _StrangeLinearImpl(torch.nn.Linear): def __init__(self, in_features, out_features, bias: bool = True): super().__init__(in_features, out_features, bias) self.params = torch.nn.Parameter(torch.randn(out_features)) def forward(self, inputs): super().forward(inputs) + self.params # Now you can use shape inference of in_features StrangeLinear = tl.infer(_StrangeLinearImpl) # in_features can be inferred layer = StrangeLinear(out_features=64)
- Parameters
module_class (torch.nn.Module) – Class of module to be updated with shape inference capabilities.
index (int, optional) – Index into
tensor.shapeinput which should be inferred, e.g. tensor.shape[1]. Default:1(0being batch dimension)
-
class
torchlayers.Lambda(function: Callable)[source]¶ Use any function as
torch.nn.ModuleSimple proxy which allows you to use your own custom in
torch.nn.Sequentialand other requiringtorch.nn.Moduleas input:import torch import torchlayers as tl model = torch.nn.Sequential(tl.Lambda(lambda tensor: tensor ** 2)) model(torch.randn(64 , 20))
- Parameters
function (Callable) – Any user specified function
- Returns
Anything
functionreturns- Return type
Any
-
forward(*args, **kwargs) → Any[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.
-
class
torchlayers.Reshape(*shapes: int)[source]¶ Reshape tensor excluding
batchdimensionReshapes input
torch.Tensorfeatures while preserving batch dimension. Standardtorch.reshapevalues (e.g.-1) are supported, e.g.:import torch import torchlayers as tl layer = tl.Reshape(20, -1) layer(torch.randn(64, 80)) # shape (64, 20, 4)
All tensors must have the same shape (except in the concatenating dimension). If possible, no copy of
tensorwill be performed.- Parameters
shapes (*int) – Variable length list of shapes used in view function
- Returns
Concatenated tensor
- Return type
-
forward(tensor)[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.
-
class
torchlayers.Concatenate(dim: int)[source]¶ Concatenate list of tensors.
Mainly useful in
torch.nn.Sequentialwhen previous layer returns multiple tensors, e.g.:import torch import torchlayers as tl class Foo(torch.nn.Module): # Return same tensor three times # You could explicitly return a list or tuple as well def forward(tensor): return tensor, tensor, tensor model = torch.nn.Sequential(Foo(), tl.Concatenate()) model(torch.randn(64 , 20))
All tensors must have the same shape (except in the concatenating dimension).
- Parameters
dim (int) – Dimension along which tensors will be concatenated
- Returns
Concatenated tensor along specified
dim.- Return type
-
forward(inputs)[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.