Skip to main content

Nothing

Project description

Example of Using Layers in kdl_pre_built

Introduction

The following example demonstrates how to use different layers from the kdl_pre_built library with Keras. The model utilizes GMPLPBlock and PolynomialDense layers to showcase their capabilities.


Installation

First, install the required libraries if you haven't already:

pip install tensorflow keras kdl_pre_built

Loading the MNIST Dataset

The MNIST dataset consists of handwritten digits from 0 to 9. We will load the data using keras.datasets:

import keras
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

Example Model Using kdl_pre_built Layers

This example model includes the following components:

  • Rescaling: Normalizes image data to the [0, 1] range.
  • GMPLPBlock: An advanced neural network layer from kdl_pre_built.
  • Flatten: Flattens the input before passing it to dense layers.
  • Dropout: Helps prevent overfitting.
  • PolynomialDense: A dense layer with nonlinear processing capabilities.
  • Dense (Softmax): The output layer with 10 units corresponding to the 10 MNIST digits.

Model Creation Function

import kdl_pre_built as kdl
import keras

def create_model(input_shape, num_blocks, output_shape):
    inputs = keras.layers.Input(shape=input_shape)
    x = keras.layers.Rescaling(scale=1./255)(inputs)
    
    for _ in range(num_blocks):
        x = kdl.layers.gmplp.GMPLPBlock(
            units=64,
            activation="gelu",
            drop_rate=0.1,
            extra_args={"kernel_initializer": "random_normal"}
        )(x)
    
    x = keras.layers.Flatten()(x)
    x = keras.layers.Dropout(0.5)(x)
    x = kdl.layers.gmplp.PolynomialDense(units=64, activation="gelu", degree=2)(x)
    outputs = keras.layers.Dense(units=output_shape, activation="softmax")(x)
    
    return keras.models.Model(inputs=inputs, outputs=outputs)

Training the Example Model

We initialize the model with an input size of 28x28 (MNIST images) and use 2 GMPLPBlock layers. Then, we compile the model and start training:

model = create_model(input_shape=(28, 28), num_blocks=2, output_shape=10)
model.compile(
    optimizer=keras.optimizers.Adam(learning_rate=1e-4),
    loss=keras.losses.SparseCategoricalCrossentropy(),
    metrics=["accuracy"]
)

history = model.fit(
    x=x_train, y=y_train,
    batch_size=32,
    epochs=10,
    validation_data=(x_test, y_test)
)

Evaluation

After training, evaluate the model on the test set:

loss, accuracy = model.evaluate(x_test, y_test)
print(f"Test Accuracy: {accuracy * 100:.2f}%")

To visualize the training process:

import matplotlib.pyplot as plt

plt.plot(history.history['accuracy'], label='Train Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.show()

Conclusion

  • This example demonstrates how to use GMPLPBlock and PolynomialDense from kdl_pre_built.
  • This is an example of two layers in kdl_pre_built. You can explore other layers and experiment with different parameters to achieve better results.

Happy coding! 🚀

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kdl_pre_built-0.1.1.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kdl_pre_built-0.1.1-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file kdl_pre_built-0.1.1.tar.gz.

File metadata

  • Download URL: kdl_pre_built-0.1.1.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.12.6 Windows/11

File hashes

Hashes for kdl_pre_built-0.1.1.tar.gz
Algorithm Hash digest
SHA256 99bcd2970e5ee6fe615625515b1dcc58dba7c409d4b98b498016a3e880a3df3f
MD5 237f6980747f756824aea4894f964479
BLAKE2b-256 fc5eab677cb8f745e8525ed6cfa276507ba0be125556df4ced0b7ec0dc911cdf

See more details on using hashes here.

File details

Details for the file kdl_pre_built-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: kdl_pre_built-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.12.6 Windows/11

File hashes

Hashes for kdl_pre_built-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 50b30927e84336b818f76b040fe5513ca0095f24c6e04bdf9800c5d1597def92
MD5 31037d66346fcff09866efe5acd2fef5
BLAKE2b-256 ae8dbd7460a1d4d375ed5b142bf1f9e42be3a9eb7b0772c66ea4d17158d0acb0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page