Keraflow
Deep Learning for Python.
 All Classes Namespaces Functions Pages
keraflow.layers.base.Layer Class Reference

Building block of a model. More...

Inheritance diagram for keraflow.layers.base.Layer:
keraflow.layers.base.Input keraflow.layers.base.MultiInputLayer keraflow.layers.base.SequentialLayer keraflow.layers.wrappers.TimeDistributed

Public Member Functions

def __init__
 
def set_trainable_params
 Register the layer's trainable parameters. More...
 
def input_dimension
 Return the expected dimension of input tensor. More...
 
def check_input_shape
 Check if the input shape(s) are valid. More...
 
def init_param
 Initializes trainable parameter(s) of the layer. More...
 
def output
 Calculates the output tensor of the layer given the input tensor(s). More...
 
def output_shape
 Calculates the shape of the output tensor given the shape of the input tensor(s). More...
 
def support_mask
 Whether the layer supports to carry on to the output tensor the mask embedded in input tensor. More...
 
def pack_init_param
 Check keraflow.utils.generic_utils.serialize.
 
def get_tensor_shape
 Get the shape of a tensor. More...
 
def get_weights
 Gets trainable parameter name, value (numpy arrays) pairs. More...
 
def embed
 Embeds the target layer such that its trainable parameters (along with regularizers and constraints on the parameters) are treated as the host layer's parameters and are updated during traing process. More...
 
def __call__
 Feed an input kensor or multiple input kensors to the layer and outputs another Kensor. More...
 

Static Public Member Functions

def unpack_init_param
 Check keraflow.utils.generic_utils.unserialize. More...
 

Detailed Description

Building block of a model.

A Layer transform input Kensor (s) into an output Kensor. The transformation could be one-to-one or many-to-one.

See Also
An Overview of Layer

Constructor & Destructor Documentation

def keraflow.layers.base.Layer.__init__ (   self,
  name = None,
  trainable = True,
  initial_weights = None,
  regularizers = None,
  constraints = None 
)
Parameters
namestr. The name of the layer.
trainableboolean. Whether parameters of the layer will be updated during the training process.
initial_weightsdict/list/single numpy array(s) or an Nd list. The initial value of the parameters of the layer. If None, a default initialization method (defined in each layer's init) is used.
regularizersdict/list of regularizer instance(s)/str(alias names of regurizers). The regularizers of the parameters of the layer.
constraintsdict/list of constraint instance(s)/str(alias names of constraints). The constraints of the parameters of the layer.

Trainable parameters of a Layer will be defined in a certain order (check init of each layer). For assigning initial_weights, regularizers, constraints, the following rules apply:

  1. If a dict is passed, e.g. {'W': np.array([[1,2],[3,4]]), 'b': np.array([1,2])}, the values are assigned according to the keys (i.e. 'W', 'b'). Missing values indicates a default behavior for the corresponding parameters.
  2. If a list is passed, e.g. [np.array([[1,2],[3,4]]), np.array([1,2])]. The values are assigned to the parameters in the same order as they are defined (could be looked up in each layer's init). Missing values indicates a default behavior for the corresponding parameters.
  3. For initial_weights, passing a single numpy array (or Nd list, will be casted into numpy array) is equivalent to passing a list with a single numpy array.
See Also
Argument Passing Summarization

Member Function Documentation

def keraflow.layers.base.Layer.__call__ (   self,
  input_kensors 
)

Feed an input kensor or multiple input kensors to the layer and outputs another Kensor.

Parameters
input_kensorsKensor/list of Kensor. The input kensor(s) of the layer
Returns
Kensor. An output kensor.
def keraflow.layers.base.Layer.check_input_shape (   self,
  input_shapes 
)

Check if the input shape(s) are valid.

Recommended to implement if inheriting MultiInputLayer (multiple inputs) to help keraflow detect incorrect input shapes. Default behaviors:

  1. Single input: check input dimension equal to Layer.input_dimension()
  2. Multiple inputs: check equal shape for all input kensors.
    Parameters
    input_shapestuple/list of tuple. The input shape(s) of input kensor(s)
def keraflow.layers.base.Layer.embed (   self,
  target_layer 
)

Embeds the target layer such that its trainable parameters (along with regularizers and constraints on the parameters) are treated as the host layer's parameters and are updated during traing process.

Use it in a custmoized layer's output() function like:

1 def output(self, x):
2  dx = self.embed(Dense(64, regularizers=['l1']))(x)
Parameters
target_layerLayer. The target layer.
Returns
callable: take a tensor as input and return the result of output tensor transformed by the target layer.
def keraflow.layers.base.Layer.get_tensor_shape (   self,
  tensor 
)

Get the shape of a tensor.

Note that the we could only get the shape if the tensor is outputted by a layer or an embedded layer.

Parameters
tensorbackend tensor.
Returns
tuple. Shape of the tensor.
def keraflow.layers.base.Layer.get_weights (   self)

Gets trainable parameter name, value (numpy arrays) pairs.

The names are decided by Layer.init_param of each layer.

Returns
dict.
def keraflow.layers.base.Layer.init_param (   self,
  input_shape 
)

Initializes trainable parameter(s) of the layer.

Inherited layer class should implement this function if the layer has trainable parameter(s). Always use Layer.set_trainable_params to register parameters.

Parameters
input_shapelist of tuple(s).
def keraflow.layers.base.Layer.input_dimension (   self)

Return the expected dimension of input tensor.

Recommended to implement if inheriting Layer (single input) to help keraflow detect incorrect input shape.

Returns
int. Expected dimension of the input tensor.
See Also
Layer.check_input_shape
def keraflow.layers.base.Layer.output (   self,
  input_tensors 
)

Calculates the output tensor of the layer given the input tensor(s).

All layers must implement this function.

Parameters
input_tensorsbackend tensor/list of backend tensors.
Returns
backend tensor. The output tesor.
def keraflow.layers.base.Layer.output_shape (   self,
  input_shapes 
)

Calculates the shape of the output tensor given the shape of the input tensor(s).

Parameters
input_shapestuple/list of tuple. The shape(s) of the input tensor(s).
Returns
tuple. The shape of the output tensor.
def keraflow.layers.base.Layer.set_trainable_params (   self,
  args 
)

Register the layer's trainable parameters.

Note that the order of args affect how user initialize the layer with initial_weights, regularizers, and constraints.

Parameters
argsarbitrary length of arguments in the pattern of ('param_name1': tensor1, 'param_name2': tensor2, ...)
def keraflow.layers.base.Layer.support_mask (   self)

Whether the layer supports to carry on to the output tensor the mask embedded in input tensor.

If return True, the output tensor will be assigned a .keraflow_mask attribute same as the input tensor. This mask is currently used only by recurrent layers.

def keraflow.layers.base.Layer.unpack_init_param (   params)
static

Check keraflow.utils.generic_utils.unserialize.

Parameters
paramsdict. The result from pack_init_param