Creating and training a regression model

This section provides instructions on how to create and train a regression model with trainable blocks that encode and decode data. The model is created using the ternary layers of the ANN2SNN package.

You can use this instruction in the development of application solutions.

To create and train a regression model:

  1. In the directory of your project, create an application file with the .py extension in which the model will be created and trained.
  2. In the application file, import the modules of the libraries required for the creation and training of the model.

    If necessary, define aliases by means of the as statement.

    An example of connecting objects to create and train a regression model:

    Python

    from knp_ann2snn.altainn import TernaryDense

    from knp_ann2snn.altainn import heaviside

    from knp_ann2snn.altainn import Clip

    from tensorflow import keras

    from keras.models import Sequential, load_model

    from keras.callbacks import ModelCheckpoint

    from keras.layers import Dense

    import matplotlib.pyplot as plt

    import numpy as np

  3. Create a sine wave and prepare the data for training.

    An example of data preparation:

    Python

    # Define the number of elements in the data set.

    N = 4000

    # Define the index up to which the elements of the data set will be used for training.

    train_idx = 3000

    # Define the index up to which elements, starting from the train_idx index, form test data.

    test_idx = 4000

    # Define the number of elements in a batch.

    elements_in_batch = 10

    # Define the dimension of the output of the encoding block.

    encoder_output_shape = 25

    # Define the dimension of the output of the neural network.

    snn_output_shape = 35

    # Define the dimension of the output of the decoding block.

    decoder_output_shape = 1

    # Create a sine wave.

    x = np.arange(N)

    arr = np.sin(x)

    # Generate training data based on elements_in_batch values.

    train_range = range(train_idx - elements_in_batch - 1)

    x_train = np.array([arr[i : i + elements_in_batch] for i in train_range])

    y_train = np.array([[arr[i + elements_in_batch + 1]] for i in train_range])

    # Generate test data based on elements_in_batch values.

    test_range = range(train_idx, test_idx - elements_in_batch - 1)

    x_test = np.array([arr[i : i + elements_in_batch] for i in test_range])

    y_test = np.array([[arr[i + elements_in_batch + 1]] for i in test_range])

  4. Create a regression model. To do this, create an encoding block, a spiking neural network, and a decoding block.

    An example of creating a regression model:

    Python

    # Create a single-layer coding block that converts data to spikes.

    encoder = Sequential(

    [

    Dense(

    encoder_output_shape,

    activation=heaviside,

    input_shape=(elements_in_batch,)

    )

    ]

    )

    # Create a spiking neural network.

    snn = Sequential(

    [

    TernaryDense(

    snn_output_shape,

    activation=heaviside,

    input_shape=(encoder_output_shape,),

    use_bias=False

    )

    ]

    )

    # Create a single-layer decoding block that converts spikes to a target value.

    decoder = Sequential(

    [

    Dense(

    decoder_output_shape,

    activation="linear",

    input_shape=(snn_output_shape,)

    )

    ]

    )

    # Create a model with a coding block, neural network, and decoding block.

    model = Sequential([encoder, snn, decoder])

    model.compile(optimizer="adam", loss="mse")

  5. Train the model.

    An example of model training:

    Python

    # Train the model on the training data.

    checkpoint = ModelCheckpoint(

    "sin_model.keras",

    monitor="val_loss",

    verbose=1,

    save_best_only=True,

    mode="min"

    )

    model.fit(x_train, y_train, epochs=50, validation_data=(x_test, y_test), callbacks=[])

    model.save("sin_model.keras")

    # Load the model.

    model = load_model(

    "sin_model.keras",

    custom_objects={

    "heaviside_mod": heaviside,

    "Clip": Clip,

    "TernaryDense": TernaryDense,

    },

    )

    # Obtain predictions.

    predict_test = model.predict(x_test)

    predict_test = predict_test.flatten()

    # Plot graphs based on predicted and target values.

    plt.plot(predict_test, "b")

    plt.plot(y_test, "r", alpha=0.5)

    plt.savefig("sin_train.png")

  6. Save the coding block, neural network, and decoding block.

    An example of saving:

    Python

    # Save the encoding block to the encoder_sin.keras file.

    model.layers[0].save("encoder_sin.keras")

    # Save the neural network to the snn_sin.keras file.

    model.layers[1].save("snn_sin.keras")

    # Save the decoding block to the decoder_sin.keras file.

    model.layers[2].save("decoder_sin.keras")

  7. Start the application.
Page top