Skip to main content

Lightning Factory

Lightning Factory

PyTorch Lightning is great, but model building can be a bit...verbose.
Lightning Factory is a Python library designed to simplify the creation of PyTorch Lightning models for various types of neural networks. It follows the parameterized factory pattern and allows users to specify custom configurations or use common defaults for quick prototyping.

Usage

pip install lightning_factory

To create a feed-forward neural network model:

import lightning_factory as lf

model = lf.ffnn(layers=[5, 3, 3, 1])

Easily define the layer structure:

layers example

Set default parameters when constructing the factory:

from lightning_factory import LightningFactory
from lightning_factory import LossFunction
from lightning_factory import ActivationFunction

# setting the defaults.  These values will always be used unless otherwise specified
lf = LightningFactory(
    loss_function=LossFunction.MSE,
    batch_size=32,
    activation_function=ActivationFunction.Softplus
)
model1 = lf.ffnn(layers=[5,3,3,1])
model2 = lf.ffnn(layers=[5,8,4,2,1], activation_function=ActivationFunction.Tanh)

Full example of building, training, testing and predicting

Video tutorial

Example stock data for training used in the tutorial: Download

Lightning Factory Video Tutorial

Full Example Code

import lightning_factory as lf
from lightning_factory import d_at

# Loading stock data built for NNs by D.AT
# download sample data at: https://d.at/example-data
d_at.load_data(
    'data/train.csv',  # Training data
    'data/test.csv',   # Data time-separated from training; used to get precision, accuracy, etc
    'data/latest.csv'  # The most recent data.  The model will be predicting the labels
)

# creating our model
model = lf.ffnn(layers=[30, 3, 3, 1])

# training with our model
d_at.train(model)

# precision, accuracy, p-value of precision, confusion matrix
d_at.print_statistics()

# stocks ordered by which are most likely to have a `true` label
d_at.print_predictions()

The LightningFactory class uses the following defaults when making a class:

default = {
    'layers': None,
    'learning_rate': 0.001,
    'max_epochs': 8,
    'batch_size': 64,
    'loss_function': 'BCE',
    'activation_function': 'ReLU',
    'optimizer': 'Adam',
    'dropout': 0,
    'l1_regularization': 0,
    'l2_regularization': 0,
    'weight_initialization': 'xavier_uniform'
}

Testing Coverage

Name Stmts Miss Branch BrPart Cover Missing
lightning_factory/LightningFactory.py 18 0 8 0 100%
lightning_factory/__init__.py 3 0 0 0 100%
lightning_factory/create_ffnn.py 41 29 14 0 22% 19-30, 34-36, 40-46, 50-59, 63-71
lightning_factory/enums.py 121 0 18 0 100%
lightning_factory/functions.py 4 2 0 0 50% 6-7
TOTAL 187 31 40 0 80%

To run a coverage report

coverage report --format=markdown

History

This code is born from the hyper-parameter tuning portion of Stock Prediction Neural Network and Machine Learning Examples

Future Work

This library is in early stages. Future work involves adding factory methods for LSTMs, RNNs and more.

If you have ideas for this, please fork and contribute!

Metadata

Release files for lightning-factory 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lightning-factory 0.1.3
File Size Uploaded
lightning_factory-0.1.3.tar.gz 341.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for lightning-factory 0.1.3
File Interpreter ABI Platform
lightning_factory-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 354.9 kB

Release files / lightning_factory-0.1.3.tar.gz

Download URL lightning_factory-0.1.3.tar.gz
Size 341.4 kB
Tags Source
SHA-256 checksum
How to use checksums
e728f0d1894a95c453fcb64a59decde7a82e68022b6fcb813d04e47e806c53d0
BLAKE2b-256 checksum
How to use checksums
1819f0677aa456b5ca24e80f89562c973030ff15393d84d3084c1583a146231b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / lightning_factory-0.1.3-py3-none-any.whl

Download URL lightning_factory-0.1.3-py3-none-any.whl
Size 13.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
56e3bce9b7ed8a4bce7c6435e4b892dbf0adcd81402f65a81a831834961a1c15
BLAKE2b-256 checksum
How to use checksums
565d9289b848752f063b2541a94abdbc86c843858bca04599ddac8736abce6cf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page