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.1.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.1-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: torcheasy-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 06a0284aad220524f7a182bb224eb17713e3f2529550ec70e42fcedcc6c1c638
MD5 32582c13c71ee6c36ebbc08d1e6628a5
BLAKE2b-256 ea1d590bc15ca4741aefad09d556778bb3e39cf873e26bd59a7ee9a590cb7b9c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: torcheasy-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1dea16e26258c39a01e4df3e9c90b0f801564e28af3339a3ee56d0c429d55f9b
MD5 d8af7b5577fd47e9f4e7751bb6dc58ec
BLAKE2b-256 bc5d221e71d78b263dc127690fc5ddae3dee879eadae41735e7d9daf5638b190

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