Skip to main content

A PyTorch training loop utility

Project description

Cognixis

Cognixis is a Python package that provides a set of utilities for building and training regression models using PyTorch. It includes functionality for creating a regression model using a Transformer Encoder, preparing data, and training the model on provided datasets. This package aims to streamline the process of implementing regression tasks with neural networks, focusing on ease of use and flexibility.

Installation

To install Cognixis, use,

pip install cognixis

Usage

Once installed, you can use the package to import the Regression class, its components (Encoder, DataGenerator, Trainer), and various utilities for model training and evaluation.

Importing the Package

from cognixis import regression

Creating a Regression Model

You can create a regression model by initializing the Encoder class:

Initialize Encoder for regression task
input_dim = 10  # Number of input features
output_dim = 1  # Number of output dimensions (single value)
model = regression.Encoder(input_dim, output_dim, d_model:int=32, nhead:int=2, num_layers:int=2, dim_feedforward:int=64, dropout:int=0.1)

Preparing the Data

You can prepare your dataset using the DataGenerator class, which will split the data into training and testing sets and load it into PyTorch DataLoader objects:

# Example numpy arrays for X (features) and y (targets)
import numpy as np

X = np.random.randn(1000, 10)  # 1000 samples, 10 features
y = np.random.randn(1000)      # 1000 target values

# Initialize DataGenerator
data_generator = regression.DataGenerator(X, y, test_size:int=0.2, shuffle:bool=True)

# Access DataLoaders for training and testing
train_loader = data_generator.train_loader
test_loader = data_generator.test_loader

Training the Model

You can use the Trainer class to train your model on the dataset:

import torch.optim as optim
import torch.nn as nn

# Define loss function and optimizer
criterion = nn.MSELoss()  # Mean Squared Error Loss for regression
optimizer = optim.Adam(model.parameters(), lr=0.001)

# Initialize the Trainer
trainer = regression.Trainer(model, optimizer, criterion, device="cpu")

# Train the model
train_losses = trainer.train(train_loader, num_epochs=50)

Evaluating the Model

Once the model is trained, you can evaluate it on the test dataset:

# Evaluate the model
test_loss = trainer.evaluate(test_loader)

Example: Full Workflow

Here is a complete example that shows how to train and evaluate a model:

from cognixis import regression
import numpy as np
import torch.optim as optim
import torch.nn as nn

# Generate random dataset
X = np.random.randn(1000, 10)
y = np.random.randn(1000)

# Initialize DataGenerator
data_generator = regression.DataGenerator(X, y)

# Create a model instance
input_dim = 10
output_dim = 1
model = regression.Encoder(input_dim, output_dim)

# Define loss function and optimizer
criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

# Initialize Trainer
trainer = regression.Trainer(model, optimizer, criterion, device="cpu")

# Train the model
train_losses = trainer.train(data_generator.train_loader, num_epochs=50)

# Evaluate the model
test_loss = trainer.evaluate(data_generator.test_loader)

Utilities

Model Summary

You can get a summary of your model using the summary function:

from cognixis import summary

# Get model summary
model_summary = summary(model, input_shape=(1, 10), depth=2)  # Example input shape
print(model_summary)

Displaying the Source Code of the Train Function You can also view the source code of the train function:

from cognixis import train_code

# Display the train function source code
train_code()

License This project is licensed under the MIT License

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

cognixis-0.1.9.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

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

cognixis-0.1.9-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file cognixis-0.1.9.tar.gz.

File metadata

  • Download URL: cognixis-0.1.9.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.12

File hashes

Hashes for cognixis-0.1.9.tar.gz
Algorithm Hash digest
SHA256 2214dbd4f317e2775be7bdc2b695b8d97ea7712a65df890d3c44ddf57b321be8
MD5 4a71a562dbc314fbe1fba605854e2ec8
BLAKE2b-256 b242e175897bacd36a9bded7bddbd8b9ed9fc3e6e00d8c485a2accda6cdfa88e

See more details on using hashes here.

File details

Details for the file cognixis-0.1.9-py3-none-any.whl.

File metadata

  • Download URL: cognixis-0.1.9-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.12

File hashes

Hashes for cognixis-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 fb07a8bad186ccbc154c41f09aa336dbb72f3537226106b64396b1ee2bfd9144
MD5 2457e2c58c304ed249f55ed98a7fd762
BLAKE2b-256 26590aac4a7116df436e99914fa624c9b8b0284ce3696fd8d787ff7bfcb1281e

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