Tutorials

This section provides hands-on tutorials for using VGSLify to build and train deep learning models. Follow these step-by-step guides to get familiar with how VGSLify simplifies model creation through VGSL specifications.

Tutorial 1: Building a CNN for Image Classification

Overview

In this tutorial, you will build a Convolutional Neural Network (CNN) for image classification using the CIFAR-10 dataset. We will define the model using a VGSL spec string, which allows us to specify the architecture in a concise, human-readable format. By the end of this tutorial, you will have a fully trained CNN model for image classification.

Step-by-Step Instructions

  1. Import required libraries:

    Begin by importing the necessary libraries for TensorFlow and VGSLify.

    import tensorflow as tf
    from vgslify.generator import VGSLModelGenerator
    
  2. Load and preprocess the dataset:

    CIFAR-10 is a dataset of 60,000 32x32 color images in 10 classes, with 6,000 images per class. You can load and preprocess the dataset as follows:

    (x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
    
    # Normalize the images to the range [0, 1]
    x_train, x_test = x_train / 255.0, x_test / 255.0
    
    # Convert labels to one-hot encoding
    y_train = tf.keras.utils.to_categorical(y_train, 10)
    y_test = tf.keras.utils.to_categorical(y_test, 10)
    
  3. Define the VGSL spec string for the CNN:

    The VGSL spec string defines the layers of the CNN. Here’s a simple CNN architecture:

    vgsl_spec = "None,32,32,3 Cr3,3,32 Mp2,2 Rc2 Cr3,3,64 Mp2,2 Rc2 Fr128 D25 Fs10"
    

    Explanation:

    • None,32,32,3: Input layer for images of size 32x32 with 3 color channels (RGB).

    • Cr3,3,32: Convolutional layer with a 3x3 filter, ReLU activation, and 32 filters.

    • Mp2,2: MaxPooling layer with a 2x2 pool size (and default strides).

    • Cr3,3,64: Second convolutional layer with 64 filters.

    • Rc2: Reshape layer to flatten the output for the fully connected layer.

    • Fr128: Fully connected (dense) layer with 128 units and ReLU activation.

    • D25: Dropout layer with a 25% dropout rate.

    • Fs10: Output layer with 10 units and softmax activation for classification into 10 classes.

  4. Build and compile the model:

    Use VGSLify to build and compile the model. This step generates the CNN architecture based on the VGSL string and compiles it for training.

    vgsl_gn = VGSLModelGenerator(backend="tensorflow")
    model = vgsl_gn.generate_model(vgsl_spec)
    
    model.compile(optimizer='adam',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    
  5. Train the model:

    Now, train the CNN on the CIFAR-10 training set. You can adjust the batch size and number of epochs as needed.

    history = model.fit(x_train, y_train, epochs=10, batch_size=64, validation_data=(x_test, y_test))
    
  6. Evaluate the model performance:

    After training, evaluate the model on the test set to see how well it performs.

    test_loss, test_acc = model.evaluate(x_test, y_test)
    print(f'Test accuracy: {test_acc}')
    

    You can also plot the training history to visualize how the accuracy and loss evolve over time.

Tutorial 2: Creating an LSTM for Sequence Prediction

Overview

In this tutorial, you will build an LSTM (Long Short-Term Memory) model using VGSLify to predict the next value in a sequence. This is commonly used in time-series forecasting. We will generate synthetic data, define an LSTM model using a VGSL string, and train the model to predict future values in the sequence.

Step-by-Step Instructions

  1. Import necessary libraries:

    import numpy as np
    from vgslify.generator import VGSLModelGenerator
    
  2. Generate synthetic sequence data:

    For this example, let’s generate a sine wave as our synthetic sequence data. The LSTM will learn to predict the next value in this sequence.

    def generate_sine_wave(seq_length=1000):
        x = np.arange(seq_length)
        y = np.sin(x / 20.0)
        return y
    
    sine_wave = generate_sine_wave()
    
    # Prepare the data for LSTM input
    def create_sequences(data, seq_length):
        x = []
        y = []
        for i in range(len(data) - seq_length):
            x.append(data[i:i+seq_length])
            y.append(data[i+seq_length])
        return np.array(x), np.array(y)
    
    seq_length = 50
    x_train, y_train = create_sequences(sine_wave, seq_length)
    
    x_train = np.expand_dims(x_train, axis=-1)  # LSTM expects input shape (batch, time steps, features)
    y_train = np.expand_dims(y_train, axis=-1)
    
  3. Define the VGSL spec string for the LSTM model:

    Here’s the VGSL string to define an LSTM with 50 units, followed by dropout and an output layer:

    vgsl_spec = f"None,{seq_length},{x_train.shape[1]} Lf50 D20 Fl1"
    

    Explanation:

    • None,seq_length,x_train.shape[1]: Input shape with 50 sequence length and 50 features.

    • Lf50: LSTM with 50 units, without returning sequences.

    • D20: Dropout layer with 20% dropout rate.

    • Fl1: Output layer with 1 unit and linear activation for sequence prediction.

  4. Build and compile the model:

    vgsl_gn = VGSLModelGenerator(backend="tensorflow")
    model = vgsl_gn.generate_model(vgsl_spec)
    
    model.compile(optimizer='adam',
                  loss='mean_squared_error')
    
  5. Train the model:

    Train the model to predict the next value in the sine wave sequence.

    history = model.fit(x_train, y_train, epochs=20, batch_size=64)
    
  6. Evaluate the model:

    Once training is complete, evaluate the model by plotting the true vs predicted values in the sine wave sequence.

    y_pred = model.predict(x_train)
    
    import matplotlib.pyplot as plt
    plt.plot(y_train, label='True')
    plt.plot(y_pred, label='Predicted')
    plt.legend()
    plt.show()