Skip to main content

Advanced layers for TensorFlow Keras

Project description

Nexri

Nexri provides specialized neural network layers for TensorFlow that extend standard Keras layers with advanced features.

Installation

pip install nexri

Features

QPDense Layer

A Quadratic Penalty Dense layer (QPDense) with integrated batch normalization that offers:

  • Quadratic penalty terms that modify the optimization landscape
  • Integrated batch normalization for faster convergence
  • Built on top of Keras Dense layer for maximum compatibility

The layer computes:

output = BatchNorm(2 * (inputs · kernel) - α * (sum(inputs²)) - α * (sum(kernel²)) + bias)

Where:

  • α (alpha) is a trainable or fixed parameter that controls the strength of the quadratic penalty terms
  • BatchNorm applies normalization to stabilize and accelerate training (optional)

Usage

# Import nexRI advanced dense layer
from nexri import QPDense

# Import regular Tensorflow and Keras components
import tensorflow as tf
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Input, Flatten, Activation
from tensorflow.keras.utils import to_categorical

# Load MNIST dataset
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

# Normalize and reshape
train_images = train_images.reshape((60000, 28, 28, 1)).astype('float32') / 255
test_images = test_images.reshape((10000, 28, 28, 1)).astype('float32') / 255

# Prepare outputs
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)

# Create a simple model with QPDense layers
model = Sequential([
    Input(shape=(28, 28, 1)),
    Flatten(),
    QPDense(units=128, weight_mean=0.5),
    Activation('relu'),
    QPDense(units=10),
    Activation('softmax'),
])

# Compile and train as usual
model.compile(
    optimizer='adam',
    loss='categorical_crossentropy',
    metrics=['accuracy']
)

# Train the model
history = model.fit(train_images,
                    train_labels,
                    epochs=5,
                    batch_size=64,
                    validation_data=(test_images, test_labels),
                    callbacks=[])

Advanced Configuration

The QPDense layer accepts all parameters that a regular Dense layer accepts, plus these additional options:

  • alfa_initial: Initial value for the alpha parameter (default: 1.0)
  • alfa_trainable: Whether alpha should be trainable (default: False)
  • weight_mean: Mean value for the kernel initializer (default: 0.0)
  • use_batch_norm: Whether to apply batch normalization (default: True)
  • batch_norm_momentum: Momentum for the batch normalization layer (default: 0.99)
  • batch_norm_epsilon: Small float added to variance to avoid division by zero (default: 1e-3)

License

MIT

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

nexri-0.1.0.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

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

nexri-0.1.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file nexri-0.1.0.tar.gz.

File metadata

  • Download URL: nexri-0.1.0.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.7

File hashes

Hashes for nexri-0.1.0.tar.gz
Algorithm Hash digest
SHA256 37ce42787973f31ea0a9c1cf1abc831d79d5371bea8f8d9eeadf5ad04977ddca
MD5 49cd4fd82f144ea4065ed021faa524e6
BLAKE2b-256 56a1969cdf1ddb6ecd5c0196d08588fb1ed3279e7b40fbc989bde9055ef33411

See more details on using hashes here.

File details

Details for the file nexri-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: nexri-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.7

File hashes

Hashes for nexri-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0b09edf8d6a15072db08a7c17ca5d2fefc5689d74e137532c83e1ec2494e5fc2
MD5 590aced39e6eb69c54a5619f53a2223c
BLAKE2b-256 09674b385b0195524513a04bc896c005bc2179260bb23b52ea6123c621e4fe63

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