vgslify.core package
Submodules
vgslify.core.config module
- class vgslify.core.config.ActivationConfig(activation)[source]
Bases:
object
Configuration for Activation layer.
- Parameters:
activation (str) – Activation function to use.
-
activation:
str
- class vgslify.core.config.Conv2DConfig(activation, kernel_size, strides, filters)[source]
Bases:
object
Configuration for 2D Convolutional layer.
- Parameters:
activation (str) – Activation function to use.
kernel_size (tuple) – Size of the convolution kernels.
strides (tuple) – Stride length of the convolution.
filters (int) – Number of output filters in the convolution.
-
activation:
str
-
filters:
int
-
kernel_size:
tuple
-
strides:
tuple
- class vgslify.core.config.DenseConfig(activation, units)[source]
Bases:
object
Configuration for Dense (Fully Connected) layer.
- Parameters:
activation (str) – Activation function to use.
units (int) – Number of neurons in the dense layer.
-
activation:
str
-
units:
int
- class vgslify.core.config.DropoutConfig(rate)[source]
Bases:
object
Configuration for Dropout layer.
- Parameters:
rate (float) – Fraction of the input units to drop.
-
rate:
float
- class vgslify.core.config.InputConfig(batch_size, depth, height, width, channels)[source]
Bases:
object
Configuration for Input layer.
- Parameters:
batch_size (int) – Size of the batches of data.
depth (int) – Depth of the input (for 3D inputs).
height (int) – Height of the input.
width (int) – Width of the input.
channels (int) – Number of channels in the input.
-
batch_size:
int
-
channels:
int
-
depth:
int
-
height:
int
-
width:
int
- class vgslify.core.config.Pooling2DConfig(pool_type, pool_size, strides)[source]
Bases:
object
Configuration for 2D Pooling layer.
- Parameters:
pool_type (str) – Type of pooling operation (e.g., ‘max’, ‘average’).
pool_size (tuple) – Size of the pooling window.
strides (tuple) – Stride length of the pooling operation.
-
pool_size:
tuple
-
pool_type:
str
-
strides:
tuple
- class vgslify.core.config.RNNConfig(units, return_sequences, go_backwards, dropout, recurrent_dropout, rnn_type=None, bidirectional=False)[source]
Bases:
object
Configuration for Recurrent Neural Network layer.
- Parameters:
units (int) – Number of RNN units.
return_sequences (bool) – Whether to return the last output or the full sequence.
go_backwards (bool) – If True, process the input sequence backwards.
dropout (float) – Fraction of the units to drop for the linear transformation of the inputs.
recurrent_dropout (float) – Fraction of the units to drop for the linear transformation of the recurrent state.
rnn_type (str, optional) – Type of RNN (e.g., ‘simple’, ‘lstm’, ‘gru’).
bidirectional (bool, optional) – If True, create a bidirectional RNN.
-
bidirectional:
bool
= False
-
dropout:
float
-
go_backwards:
bool
-
recurrent_dropout:
float
-
return_sequences:
bool
-
rnn_type:
str
= None
-
units:
int
vgslify.core.factory module
- class vgslify.core.factory.LayerFactory(input_shape=None, data_format='channels_last')[source]
Bases:
ABC
Abstract base class for creating neural network layers from VGSL specifications.
This class defines the interface that must be implemented by concrete factories for different frameworks (e.g., TensorFlow, PyTorch). It also provides common methods for output shape calculations to be used by subclasses.
- Parameters:
input_shape (tuple of int, optional) – The initial input shape for the model.
data_format (str, default 'channels_last') – The data format for the input tensor. Either ‘channels_last’ or ‘channels_first’.
- layers
A list to store the created layers.
- Type:
list
- data_format
The data format for the input tensor.
- Type:
str
- shape
The current shape of the output tensor.
- Type:
tuple of int
- _input_shape
The initial input shape for the model.
- Type:
tuple of int
Notes
This is an abstract base class. Use a concrete implementation like TensorFlowLayerFactory or PyTorchLayerFactory in your code.
This class uses a naming convention where public methods for creating layers (e.g., conv2d) have corresponding private methods with an underscore prefix (e.g., _conv2d) that handle the actual layer creation.
Examples
>>> # Assuming we have a TensorFlowLayerFactory implementation >>> factory = TensorFlowLayerFactory(input_shape=(224, 224, 3)) >>> factory.conv2d('Cr3,3,32') >>> factory.pooling2d('Mp2,2,2,2') >>> factory.flatten('Flt') >>> factory.dense('Fs128') >>> model = factory.build('my_model')
- 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:
Any
Examples
>>> # Using a hypothetical concrete implementation >>> factory = SomeConcreteLayerFactory(input_shape=(128,)) >>> factory.activation('Ar')
- 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:
Any
Examples
>>> # Using a hypothetical concrete implementation >>> factory = SomeConcreteLayerFactory(input_shape=(28, 28, 32)) >>> factory.batchnorm('Bn')
- abstract build(name)[source]
Abstract method to build the final model using the created layers.
- Parameters:
name (str) – The name of the model.
- Returns:
The final built model.
- Return type:
Any
Examples
>>> # Using a hypothetical concrete implementation >>> factory = SomeConcreteLayerFactory(input_shape=(28, 28, 1)) >>> factory.conv2d('Cr3,3,32') >>> factory.flatten('Flt') >>> factory.dense('Fs10') >>> model = factory.build('my_model')
- conv2d(spec)[source]
Create a 2D Convolutional 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:
Any
Examples
>>> # Using a hypothetical concrete implementation >>> factory = SomeConcreteLayerFactory(input_shape=(28, 28, 1)) >>> factory.conv2d('Cr3,3,32')
- dense(spec)[source]
Create a Dense (Fully Connected) 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:
Any
Examples
>>> # Using a hypothetical concrete implementation >>> factory = SomeConcreteLayerFactory(input_shape=(7*7*32,)) >>> factory.dense('Fs128')
- 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:
Any
Examples
>>> # Using a hypothetical concrete implementation >>> factory = SomeConcreteLayerFactory(input_shape=(128,)) >>> factory.dropout('D50')
- flatten(spec)[source]
Create a Flatten layer based on the VGSL specification string.
- Parameters:
spec (str) – The VGSL specification string for the Flatten layer.
- Returns:
The created Flatten layer.
- Return type:
Any
Examples
>>> # Using a hypothetical concrete implementation >>> factory = SomeConcreteLayerFactory(input_shape=(7, 7, 64)) >>> factory.flatten('Flt')
- input(spec)[source]
Create an Input layer based on the VGSL specification string.
- Parameters:
spec (str) – The VGSL specification string for the Input layer.
- Returns:
The created Input layer.
- Return type:
Any
Examples
>>> # Using a hypothetical concrete implementation >>> factory = SomeConcreteLayerFactory() >>> factory.input('1,28,28,1')
- pooling2d(spec)[source]
Create a 2D Pooling layer based on the VGSL specification string.
- Parameters:
spec (str) – The VGSL specification string for the Pooling2D layer.
- Returns:
The created Pooling2D layer.
- Return type:
Any
Examples
>>> # Using a hypothetical concrete implementation >>> factory = SomeConcreteLayerFactory(input_shape=(28, 28, 32)) >>> factory.pooling2d('Mp2,2,2,2')
- reshape(spec)[source]
Create a Reshape layer based on the VGSL specification string.
- Parameters:
spec (str) – VGSL specification string for the Reshape layer. Can be: - ‘Rc(2|3)’: Collapse spatial dimensions (height, width, and channels). - ‘R<x>,<y>,<z>’: Reshape to the specified target shape.
- Returns:
The created Reshape layer.
- Return type:
Any
Examples
>>> # Using a hypothetical concrete implementation >>> factory = SomeConcreteLayerFactory(input_shape=(28, 28, 1)) >>> factory.reshape('Rc3')
- rnn(spec)[source]
Create an RNN layer (LSTM or GRU), either unidirectional or bidirectional, based on the VGSL specification string.
- Parameters:
spec (str) – The VGSL specification string for the RNN layer.
- Returns:
The created RNN layer (either unidirectional or bidirectional).
- Return type:
Any
Examples
>>> # Using a hypothetical concrete implementation >>> factory = SomeConcreteLayerFactory(input_shape=(28, 28)) >>> factory.rnn('Ls128') # Unidirectional LSTM >>> factory.rnn('Bl128') # Bidirectional LSTM
vgslify.core.parser module
- vgslify.core.parser.parse_activation_spec(spec)[source]
Parses a VGSL specification string for an Activation layer and returns the activation function.
- Parameters:
spec (str) – VGSL specification for the Activation layer. Expected format: A(s|t|r|l|m) - s: softmax - t: tanh - r: relu - l: linear - m: sigmoid
- Returns:
The activation function name.
- Return type:
str
- Raises:
ValueError – If the provided VGSL spec string does not match the expected format.
Examples
>>> activation = parse_activation_spec("Ar") >>> print(activation) 'relu'
- vgslify.core.parser.parse_conv2d_spec(spec)[source]
Parses a VGSL specification string for a Conv2D layer and returns the parsed configuration.
- Parameters:
spec (str) – VGSL specification for the convolutional layer. Expected format: C(s|t|r|l|m)<x>,<y>,[<s_x>,<s_y>,]<d> - (s|t|r|l|m): Activation type. - <x>,<y>: Kernel size. - <s_x>,<s_y>: Optional strides (defaults to (1, 1) if not provided). - <d>: Number of filters (depth).
- Returns:
Parsed configuration for the Conv2D layer.
- Return type:
- Raises:
ValueError: – If the provided VGSL spec string does not match the expected format.
Examples
>>> from vgslify.core.parser import parse_conv2d_spec >>> config = parse_conv2d_spec("Cr3,3,64") >>> print(config) Conv2DConfig(activation='relu', kernel_size=(3, 3), strides=(1, 1), filters=64)
- vgslify.core.parser.parse_dense_spec(spec)[source]
Parses a VGSL specification string for a Dense layer and returns the parsed configuration.
- Parameters:
spec (str) –
VGSL specification for the dense layer. Expected format: F(s|t|r|l|m)<d> - (s|t|r|l|m): Non-linearity type. One of sigmoid, tanh, relu,
linear, or softmax.
<d>: Number of outputs (units).
- Returns:
Parsed configuration for the Dense layer.
- Return type:
- Raises:
ValueError – If the provided VGSL spec string does not match the expected format.
Examples
>>> config = parse_dense_spec("Fr64") >>> print(config) DenseConfig(activation='relu', units=64)
- vgslify.core.parser.parse_dropout_spec(spec)[source]
Parses a VGSL specification string for a Dropout layer and returns the parsed configuration.
- Parameters:
spec (str) – VGSL specification for the Dropout layer. Expected format: D<rate> where <rate> is the dropout percentage (0-100).
- Returns:
Parsed configuration for the Dropout layer.
- Return type:
- Raises:
ValueError – If the provided VGSL spec string does not match the expected format.
Examples
>>> config = parse_dropout_spec("D50") >>> print(config) DropoutConfig(rate=0.5)
- vgslify.core.parser.parse_input_spec(spec)[source]
Parses a VGSL specification string for an Input layer and returns the parsed configuration.
- Parameters:
spec (str) – VGSL specification for the Input layer. Supported format: <batch_size>,<depth>,<height>,<width>,<channels> for 4D inputs, <batch_size>,<height>,<width>,<channels> for 3D inputs, <batch_size>,<height>,<width> for 2D inputs, <batch_size>,<width> for 1D inputs.
- Returns:
Parsed configuration for the Input layer.
- Return type:
- Raises:
ValueError – If the provided VGSL spec string does not match the expected format.
Examples
>>> config = parse_input_spec("None,224,224,3") >>> print(config) InputConfig(batch_size=None, width=224, depth=None, height=224, channels=3)
- vgslify.core.parser.parse_pooling2d_spec(spec)[source]
Parses a VGSL specification string for a Pooling2D layer and returns the parsed configuration.
- Parameters:
spec (str) – VGSL specification for the pooling layer. Expected format: Mp<x>,<y>[,<s_x>,<s_y>] or Ap<x>,<y>[,<s_x>,<s_y>] - <x>,<y>: Pool size. - <s_x>,<s_y>: Strides. If not specified, defaults to pool size.
- Returns:
Parsed configuration for the Pooling2D layer.
- Return type:
- Raises:
ValueError: – If the provided VGSL spec string does not match the expected format.
Examples
>>> config = parse_pooling2d_spec("Mp2,2") >>> print(config) Pooling2DConfig(pool_size=(2, 2), strides=(2, 2)) >>> config = parse_pooling2d_spec("Mp2,2,1,1") >>> print(config) Pooling2DConfig(pool_size=(2, 2), strides=(1, 1))
- vgslify.core.parser.parse_reshape_spec(spec)[source]
Parses a VGSL specification string for a Reshape layer and returns the target shape.
- Parameters:
spec (str) – VGSL specification for the Reshape layer. Expected format: R<x>,<y>,<z>
- Returns:
Parsed configuration for the Reshape layer.
- Return type:
- Raises:
ValueError – If the provided VGSL spec string does not match the expected format.
Examples
>>> config = parse_reshape_spec("R64,64,3") >>> print(config) ReshapeConfig(target_shape=(64, 64, 3))
- vgslify.core.parser.parse_rnn_spec(spec)[source]
Parses a VGSL specification string for an RNN layer (LSTM, GRU, Bidirectional) and returns the parsed configuration. :type spec:
str
:param spec: VGSL specification for the RNN layer. Expected format:For LSTM/GRU: (L|G)(f|r)[s]<n>[,D<rate>,Rd<rate>] For Bidirectional: B(g|l)<n>[,D<rate>,Rd<rate>]
- Returns:
Parsed configuration for the RNN layer.
- Return type:
Examples
>>> config = parse_rnn_spec("Lf64,D50,Rd25") # Forward LSTM with 64 units >>> config = parse_rnn_spec("Gr64") # Reverse GRU with 64 units >>> config = parse_rnn_spec("Bl64") # Bidirectional LSTM with 64 units
vgslify.core.utils module
- vgslify.core.utils.get_activation_function(activation_char)[source]
Maps a VGSL activation character to the corresponding Keras activation function.
- Parameters:
activation_char (str) – The character representing the activation function in the VGSL spec.
- Returns:
The name of the Keras activation function.
- Return type:
str
- Raises:
ValueError – If the provided activation character is not recognized.
Examples
>>> activation = get_activation_function('r') >>> print(activation) 'relu'