Skip to main content

Easily training module for PyTorch.

Project description

:fire: Torcheasy 0.0.1

Torcheasy is a lightweight tool designed to simplify model training with PyTorch.

This project consists of two main components: Torcheasy.BaseConfig and Torcheasy.TrainableModule. The BaseConfig is responsible for creating a unified project configuration, while TrainableModule provides a framework for implementing any module that extends torch.nn.Module.

1. Easy Configuration

In deep learning, managing hyperparameters is crucial. Torcheasy offers a simple way to configure these parameters:

import torcheasy

config = torcheasy.BaseConfig()
config.add_param("batch_size", 16)
config.add_param("lr", 1e-4)
config.add_param("dim", 128)

batch_size = config.batch_size
...

Additionally, saving and loading configurations is straightforward:

# Save the configuration
config.save("ModelA")
...
# Load the configuration
config = torcheasy.BaseConfig()
config.load("ModelA")

batch_size = config.batch_size
...

The configurations are saved as a JSON file at ModelA/config.json:

{
  "batch_size": 16,
  "lr": 0.0001,
  "dim": 128
}

2. Easy Training

Training PyTorch models with Torcheasy is simple and intuitive:

import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
from torcheasy import TrainableModule, BaseConfig

# Define your model
class MyModel(TrainableModule):
    def __init__(self, config: BaseConfig):
        super().__init__(config)
        self.linear = nn.Linear(config.in_dim, config.out_dim)

    def forward(self, x):
        return self.linear(x)

# Define configurations
config = BaseConfig()
config.add_param("in_dim", 1)
config.add_param("out_dim", 16)
model = MyModel(config)

# Prepare your dataset
train_data = DataLoader(Dataset())  # Customize your train dataset
val_data = DataLoader(Dataset())  # Optional validation dataset
test_data = DataLoader(Dataset())  # Optional test dataset

model.prepare_data(train_data, test_data, val_data, config.batch_size)

# Start training
model.train_model(
    config.epoch, 
    torch.nn.MSELoss(), 
    optimizer=config.opt, 
    lr=config.lr,
    lr_scheduler=..., 
    early_stop=10
)

3. Easy Control

For more complex training processes, Torcheasy allows customization through various callback functions:

import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
from torcheasy import TrainableModule, BaseConfig

# Define your model
class MyModel(TrainableModule):
    def __init__(self, config: BaseConfig):
        super().__init__(config)
        self.linear = nn.Linear(config.in_dim, config.out_dim)

    def forward(self, x):
        return self.linear(x)

    # Override this method to customize the loss computation
    # The default loss computation looks like this:
    def compute_loss(self, x: torch.Tensor, y: torch.Tensor, criterion) -> torch.Tensor:
        return self.compute_loss(x, y, criterion)
    
    # Actions to perform at the start of each iteration
    def iter_start(self, iteration):
        print("Iteration started.")
        
    # Actions to perform before the optimizer's backward pass
    def iter_end_before_opt(self, iteration):
        print("Iteration ended.")
    
    # Actions to perform before training starts
    def train_start(self):
        print("Training started.")
    
    # Additional customization points...
    ...

More advanced features are still updating!


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

torcheasy-0.1.2.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

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

torcheasy-0.1.2-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file torcheasy-0.1.2.tar.gz.

File metadata

  • Download URL: torcheasy-0.1.2.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.15

File hashes

Hashes for torcheasy-0.1.2.tar.gz
Algorithm Hash digest
SHA256 2c666b80bb8b96bf855cadfa72a5723e92a72f6103b7e45f21f058de17a6693b
MD5 906d0f6e9f70ef38fa33800e385279b6
BLAKE2b-256 eef624e52d57c8eafe04ef076e8059649546f0c54ab0e47073e59dbd79c65e39

See more details on using hashes here.

File details

Details for the file torcheasy-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: torcheasy-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.15

File hashes

Hashes for torcheasy-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f0558c8552f26e34cc434fa5c5615a3fab872a3570ac97e3930d923c95612689
MD5 a9f32f0c1df42fe90961bc46c056c84b
BLAKE2b-256 e1190af8ae1cf688ea18dd4d564536663b332b6cfd3e39dcdc840471528239de

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