vgslify.tensorflow package

Submodules

vgslify.tensorflow.layers module

class vgslify.tensorflow.layers.TensorFlowLayerFactory[source]

Bases: LayerFactory

TensorFlowLayerFactory is responsible for creating TensorFlow-specific layers based on parsed VGSL (Variable-size Graph Specification Language) specifications. This factory handles the creation of various types of layers, including convolutional layers, pooling layers, RNN layers, dense layers, activation layers, and more.

This class abstracts the layer creation logic, allowing the VGSLModelGenerator to dynamically build models without needing to know the specifics of TensorFlow operations.

static activation(spec)[source]

Create an Activation layer based on the VGSL specification string.

Parameters:

spec (str) – The VGSL specification string for the Activation layer.

Returns:

The created Activation layer.

Return type:

tf.keras.layers.Activation

Examples

>>> from vgslify.tensorflow.layers import TensorFlowLayerFactory
>>> activation_layer = TensorFlowLayerFactory.activation("Ar")
>>> print(activation_layer)
<keras.src.layers.core.activation.Activation object at 0x7f8b1c0b1d30>
static avgpool2d(spec)[source]

Create an AvgPool2D layer based on the VGSL specification string.

Parameters:

spec (str) – The VGSL specification string for the AvgPool2D layer.

Returns:

The created AvgPool2D layer.

Return type:

tf.keras.layers.AvgPool2D

Raises:

ValueError – If the provided VGSL spec string does not match the expected format.

Examples

>>> from vgslify.tensorflow.layers import TensorFlowLayerFactory
>>> avgpool_layer = TensorFlowLayerFactory.avgpool2d("Ap2,2,2,2")
>>> print(avgpool_layer)
<keras.src.layers.pooling.average_pooling2d.AveragePooling2D object at 0x7f8b1c0b1d30>
static batchnorm(spec)[source]

Create a BatchNormalization layer based on the VGSL specification string.

Parameters:

spec (str) – The VGSL specification string for the BatchNormalization layer.

Returns:

The created BatchNormalization layer.

Return type:

tf.keras.layers.BatchNormalization

Raises:

ValueError – If the provided VGSL spec string does not match the expected format.

Examples

>>> from vgslify.tensorflow.layers import TensorFlowLayerFactory
>>> batchnorm_layer = TensorFlowLayerFactory.batchnorm("Bn")
>>> print(batchnorm_layer)
<keras.src.layers.normalization.batch_normalization.BatchNormalization object at
0x7f8b1c0b1d30>
static bidirectional(spec)[source]

Create a Bidirectional RNN layer based on the VGSL specification string.

Parameters:

spec (str) – The VGSL specification string for the Bidirectional layer.

Returns:

The created Bidirectional RNN layer.

Return type:

tf.keras.layers.Bidirectional

Raises:

ValueError – If the provided VGSL spec string does not match the expected format.

Examples

>>> from vgslify.tensorflow.layers import TensorFlowLayerFactory
>>> bidirectional_layer = TensorFlowLayerFactory.bidirectional("Blr64,D20,Rd10")
>>> print(bidirectional_layer)
<keras.src.layers.wrappers.bidirectional.Bidirectional object at 0x7f8b1c0b1d30>
static build_final_model(inputs, outputs, name='VGSL_Model')[source]

Build the final model using the provided input and output layers.

Parameters:
  • inputs (tf.keras.layers.Input) – The input layer of the model.

  • outputs (tf.keras.layers.Layer) – The output layer (or the final processed layer before output) of the model.

  • name (str, optional) – The name of the model, by default “VGSL_Model

Returns:

The constructed TensorFlow model.

Return type:

tf.keras.models.Model

Examples

>>> from vgslify.tensorflow.layers import TensorFlowLayerFactory
>>> inputs = TensorFlowLayerFactory.input("None,32,3,32")
>>> outputs = TensorFlowLayerFactory.dense("Fr64")(inputs)
>>> model = TensorFlowLayerFactory.build_final_model(inputs, outputs)
>>> print(model)
<keras.engine.training.Model object at 0x7f8b1c0b1d30>
static conv2d(spec)[source]

Create a Conv2D layer based on the VGSL specification string.

Parameters:

spec (str) – The VGSL specification string for the Conv2D layer.

Returns:

The created Conv2D layer.

Return type:

tf.keras.layers.Conv2D

Raises:

ValueError – If the provided VGSL spec string does not match the expected format.

Examples

>>> from vgslify.tensorflow.layers import TensorFlowLayerFactory
>>> conv_layer = TensorFlowLayerFactory.conv2d("Cr3,3,64")
>>> print(conv_layer)
<keras.src.layers.convolutional.conv2d.Conv2D object at 0x7f8b1c0b1d30>
static dense(spec)[source]

Create a Dense layer based on the VGSL specification string.

Parameters:

spec (str) – The VGSL specification string for the Dense layer.

Returns:

The created Dense layer.

Return type:

tf.keras.layers.Dense

Raises:

ValueError – If the provided VGSL spec string does not match the expected format.

Examples

>>> from vgslify.tensorflow.layers import TensorFlowLayerFactory
>>> dense_layer = TensorFlowLayerFactory.dense("Fr64")
>>> print(dense_layer)
<keras.src.layers.core.dense.Dense object at 0x7f8b1c0b1d30>
static dropout(spec)[source]

Create a Dropout layer based on the VGSL specification string.

Parameters:

spec (str) – The VGSL specification string for the Dropout layer.

Returns:

The created Dropout layer.

Return type:

tf.keras.layers.Dropout

Raises:

ValueError – If the provided VGSL spec string does not match the expected format.

Examples

>>> from vgslify.tensorflow.layers import TensorFlowLayerFactory
>>> dropout_layer = TensorFlowLayerFactory.dropout("D50")
>>> print(dropout_layer)
<keras.src.layers.core.dropout.Dropout object at 0x7f8b1c0b1d30>
static flatten(spec)[source]

Create a Flatten layer based on the VGSL specification string.

Parameters:

spec (str) – The VGSL specification string for the Flatten layer. Expected format: ‘Flt’.

Returns:

The created Flatten layer.

Return type:

tf.keras.layers.Flatten

Raises:

ValueError – If the provided VGSL spec string does not match the expected format.

Examples

>>> from vgslify.tensorflow.layers import TensorFlowLayerFactory
>>> flatten_layer = TensorFlowLayerFactory.flatten("Flt")
>>> print(flatten_layer)
<keras.src.layers.core.flatten.Flatten object at 0x7f8b1c0b1d30>
static gru(spec)[source]

Create a GRU layer based on the VGSL specification string.

Parameters:

spec (str) – The VGSL specification string for the GRU layer.

Returns:

The created GRU layer.

Return type:

tf.keras.layers.GRU

Raises:

ValueError – If the provided VGSL spec string does not match the expected format.

Examples

>>> from vgslify.tensorflow.layers import TensorFlowLayerFactory
>>> gru_layer = TensorFlowLayerFactory.gru("Gr64,D20,Rd10")
>>> print(gru_layer)
<keras.src.layers.recurrent.gru.GRU object at 0x7f8b1c0b1d30>
static input(spec)[source]

Create an Input layer based on the VGSL specification string.

Parameters:

spec (str) – The VGSL specification string for the Input layer. Supported formats: - 1D: <batch_size>,<width> - 2D: <batch_size>,<height>,<width> - 3D: <batch_size>,<height>,<width>,<channels> - 4D: <batch_size>,<depth>,<height>,<width>,<channels>

Returns:

The created Input layer.

Return type:

tf.keras.layers.Input

Raises:

ValueError – If the provided VGSL spec string does not match the expected format.

Examples

>>> from vgslify.tensorflow.layers import TensorFlowLayerFactory
>>> input_layer = TensorFlowLayerFactory.input("None,32,3,32")
>>> print(input_layer)
<keras.src.layers.core.input.Input object at 0x7f8b1c0b1d30>
>>> input_layer = TensorFlowLayerFactory.input("None,128")
>>> print(input_layer)
<keras.src.layers.core.input.Input object at 0x7f8b1c0b1d30>
>>> input_layer = TensorFlowLayerFactory.input("None,16,64,64,3")
>>> print(input_layer)
<keras.src.layers.core.input.Input object at 0x7f8b1c0b1d30>
static lstm(spec)[source]

Create an LSTM layer based on the VGSL specification string.

Parameters:

spec (str) – The VGSL specification string for the LSTM layer.

Returns:

The created LSTM layer.

Return type:

tf.keras.layers.LSTM

Raises:

ValueError – If the provided VGSL spec string does not match the expected format.

Examples

>>> from vgslify.tensorflow.layers import TensorFlowLayerFactory
>>> lstm_layer = TensorFlowLayerFactory.lstm("Lr64,D20,Rd10")
>>> print(lstm_layer)
<keras.src.layers.recurrent.lstm.LSTM object at 0x7f8b1c0b1d30>
static maxpooling2d(spec)[source]

Create a MaxPooling2D layer based on the VGSL specification string.

Parameters:

spec (str) – The VGSL specification string for the MaxPooling2D layer.

Returns:

The created MaxPooling2D layer.

Return type:

tf.keras.layers.MaxPooling2D

Raises:

ValueError – If the provided VGSL spec string does not match the expected format.

Examples

>>> from vgslify.tensorflow.layers import TensorFlowLayerFactory
>>> maxpool_layer = TensorFlowLayerFactory.maxpooling2d("Mp2,2,2,2")
>>> print(maxpool_layer)
<keras.src.layers.pooling.max_pooling2d.MaxPooling2D object at 0x7f8b1c0b1d30>
static reshape(spec, prev_layer=None)[source]

Create a Reshape layer based on the VGSL specification string.

Parameters:
  • spec (str) – VGSL specification string for the Reshape layer. Can be: - ‘Rc’: Collapse spatial dimensions (height, width, and channels). - ‘R<x>,<y>,<z>’: Reshape to the specified target shape.

  • prev_layer (tf.keras.layers.Layer, optional) – The previous layer in the model, used for spatial collapsing, by default None

Returns:

The created Reshape layer.

Return type:

tf.keras.layers.Reshape

Raises:

ValueError – If the previous layer is required for spatial collapsing but is not provided, or if the provided shape is incompatible for spatial collapsing.

Examples

>>> from vgslify.tensorflow.layers import TensorFlowLayerFactory
>>> reshape_layer = TensorFlowLayerFactory.reshape("R10,10,3")
>>> print(reshape_layer)
<keras.src.layers.core.reshape.Reshape object at 0x7f8b1c0b1d30>

Module contents