Custom Layer

neuralpy.utils.CustomLayer(layer_class, layer_type, layer_name=None)
info

Custom Layer is mostly stable and can be used for any project. In the future, any chance of breaking changes is very low.

In NeuralPy, anyone can use the CustomLayer class to build custom layers. All NeuralPy layers are built using this CustomLayer class.

Supported Arguments

  • layer_class : (PyTorch layer class) PyTorch layer class
  • layer_type : (String) Type of the layer
  • layer_name=None : (String) Name of the layer, if not provided NeuralPy automatically creates the name of the layer based on the layer_type

Supported Methods

._get_layer_details() method:

neuralpy.utils.CustomLayer._get_layer_details(layer_details=None, keyword_arguments=None)

This method is used to get the details of the model, NeuralPy model class needs these details to build the layer.

Supported Arguments

  • layer_details=None : (Tuple) Some values in a tuple that can be passed here. These values are available in the next layer's get_input_dim method
  • keyword_arguments=None : (String) Multiple arguments needed by the PyTorch layer

._get_layer_class() method:

This method can be used for getting the PyTorch class

._get_layer_type() method:

This method can be used for getting the Layer type

._get_layer_name() method:

This method can be used for getting the Layer name

Code Example

from torch.nn import <PyTorch_Class_Name>
from neuralpy.utils import CustomLayer
class <ClassName>(CustomLayer):
def __init__(
self,
name=None
...
):
super().__init__(<PyTorch_Class_Name>, "Conv1D", layer_name=name)
def get_input_dim(self, prev_input_dim, prev_layer_type):
# This method calculates the input shape for the layer based on the previous output layer.
return <>
def get_layer(self):
# Returning all the details of the layer
return self._get_layer_details(
<input_size>
{
...
},
)