Layer base class

Base layer class and helper functions to print network summary and aggregate auxiliary inputs from a list of layers.

Layer Base class for implementing a layer in berry.
class braid.berry.layers.Layer(incoming, W=None, b=None, name=None)

Base class for implementing a layer in berry.

Layer is a simple helper class for implementing new layers. It defines a set of functions which enable weight initialization, printing layer summary, etc. Due to berry‘s transparent coupling with tensorflow, incoming layers can be Layer objects, or tf.Tensor objects.

incoming
: Layer or tf.Tensor
Parent layer, whose output is given as input to the current layer.
W
: tf.Variable, optional (default = None)
Weight tensor in case if the layer has any trainable parameters.
b
: tf.Variable, optional (default = None)
Bias vector in case of trainable parameters.
name
: string, optional (default = None)
Name of the layer. Should be specified for better readability.
input_layer
: Layer or tf.Tensor
Input layer to this layer.
input_shape
: tuple
Shape of the incoming layer.
output
: tf.Tensor
The Tensor obtained after performing the transformation applied by this layer.
output_shape
: tuple
Shape of the output tensor.
type
: string
Return the name of the class.
bias_variable(shape, initializer=None, val=0.0)

Create a bias vector with appropriate initialization.

shape
: list
Shape of the weight tensor to create.
initializer
: string, optional (default = None)
Not used
val
: float, optional (default = 0.0)
Constant value for initializing the biases.
tf.Variable
Symbolic bias vector initialized with a value of val.
get_W_shape()

Shape of the weight tensor required for the layer.

Note

Should be overwritten for layers which have trainable weight tensors.

get_b_shape()

Number of bias units

Note

Should be overwritten for layers which have trainable weight tensors.

get_fan_in()

Number of input units to the layer.

int
The fan in of the layer.

Note

Should be overwritten if the layer has trainable weights.

get_fan_out()

Number of output units to the layer.

int
The fan out of the layer.

Note

Should be overwritten if the layer has trainable weights.

get_output_for()

Compute the output transformation on the input tensor.

The main logic of the layer lies here. The required sequence of tensorflow operations for transforming the input to output are defined here. This should be overwritten in the inherited layer.

tf.Tensor
Output tensor of this layer.

Note

For easy access to all the layer pre-activations/outputs, the Tensor objects are added to a tensorflow collection with BerryKeys.LAYER_OUTPUTS key. Example:

>>> # Add to collection
>>> tf.add_to_collection(BerryKeys.LAYER_OUTPUTS, output)
>>> # Retrieve all from the collection
>>> vars = tf.get_collection(BerryKeys.LAYER_OUTPUTS)
get_output_shape_for(input_shape)

Shape of the output tensor produced by this layer.

This method should be overwritten in the inherited layer.

input_shape
: tuple or list
Shape of the input layer.
tuple
Shape of the output tensor.

Note

By default, this will return the input_shape.

print_summary(print_fmt)

Print out a summary of the layer - layer type, name, weight shape, input shape and output shape.

print_fmt
: string
Formatted string with 5 fields.
>>> fmt = "|{:^20}|{:^15}|{:^20}|{:^20}|{:^20}|"
>>> print l.print_summary(fmt)
|   Convolution2D    |     conv1     |   [5, 5, 3, 64]    |(None, \
224, 224, 3) |(None, 113, 113, 64)|
validate_input_layer()

This function ensures valid layer-layer connections are made.

Typically, this function would perform assertions on the shape of the input layer. This function must be overwritten in the inherited class.

bool
True if input layer is valid, False otherwise.
weight_variable(shape, initializer=None, stddev=0.01)

Create a weight tensor with appropriate initialization.

shape
: list
Shape of the weight tensor to create.
initializer
: string, optional (default = None)
Initialize using a pre-defined method in berry.initializations.
stddev
: float, optional (default = 0.01)
Standard deviation of Gaussian distribution for initialization.
tf.Variable
Symbolic weight tensor initialized according to initializer method specified or from a Gaussian distribution with a mean of 0 and a standard deviation of stddev.