A package for prequential coding and clustering in PyTorch.
Project description
PreqTorch
A PyTorch-based library for prequentially encoding datasets and clustering.
Overview
PreqTorch provides tools for prequential encoding in PyTorch. Prequential encoding is a technique for evaluating datasets in an online learning setting, where the model is updated after each prediction.
The library includes:
- Prequential encoders (BlockEncoder, MIREncoder)
Installation
From PyPI
pip install preqtorch
From Source
git clone https://github.com/torrescj/preqtorch.git
cd preqtorch
pip install -e .
Requirements
PreqTorch has the following requirements:
- Python 3.6+
- PyTorch 1.7+
- NumPy
Dataset Requirements
For PreqTorch to work properly, your datasets must:
- Be organized as tuples of tensors or tuples of tuples including tensors
- Return data in one of the following formats:
(inputs, targets)- Basic format without masks(inputs, targets, mask)- Format with a shared mask for both inputs and targets(inputs, targets, output_mask, target_mask)- Format with separate masks for outputs and targets
- Be compatible with PyTorch's Dataset class
Usage
Collate Function Requirement
When using PreqTorch encoders, you must provide your own collate function at creation time. This function should:
- Take a batch of samples and combine them into a single batch
- Return data in one of the supported formats:
(inputs, targets)(inputs, targets, mask)- shared mask for both inputs and targets(inputs, targets, output_mask, target_mask)- separate masks for outputs and targets
- Handle any specific requirements of your dataset
Examples of collate functions for different dataset formats:
# Basic collate function (inputs, targets)
def basic_collate_fn(batch):
# Unpack the batch
inputs = [item[0] for item in batch]
targets = [item[1] for item in batch]
# Stack inputs and targets into tensors
inputs = torch.stack(inputs)
targets = torch.stack(targets)
return inputs, targets
# Collate function with shared mask (inputs, targets, mask)
def masked_collate_fn(batch):
# Unpack the batch
inputs = [item[0] for item in batch]
targets = [item[1] for item in batch]
# Create or extract masks (example: mask based on non-zero values)
masks = [torch.ones_like(item[1], dtype=torch.bool) for item in batch]
# Stack inputs, targets, and masks into tensors
inputs = torch.stack(inputs)
targets = torch.stack(targets)
masks = torch.stack(masks)
return inputs, targets, masks
# Collate function with separate masks (inputs, targets, output_mask, target_mask)
def separate_masks_collate_fn(batch):
# Unpack the batch
inputs = [item[0] for item in batch]
targets = [item[1] for item in batch]
# Create or extract masks (example: different masks for outputs and targets)
# Note: output_mask will be applied to model outputs, which should have the same shape as inputs
output_masks = [torch.ones_like(item[0], dtype=torch.bool) for item in batch]
target_masks = [torch.ones_like(item[1], dtype=torch.bool) for item in batch]
# Stack inputs, targets, and masks into tensors
inputs = torch.stack(inputs)
targets = torch.stack(targets)
output_masks = torch.stack(output_masks)
target_masks = torch.stack(target_masks)
return inputs, targets, output_masks, target_masks
Block Encoding
Block encoding divides the dataset into blocks and trains the model on each block sequentially.
import torch
from preqtorch import BlockEncoder
# Define a model class
class MyModel(torch.nn.Module):
def __init__(self):
super().__init__()
self.linear = torch.nn.Linear(10, 2)
def forward(self, x):
return self.linear(x)
# Create a block encoder
encoder = BlockEncoder(
model_class=MyModel,
loss_fn=torch.nn.functional.cross_entropy
)
# Encode a dataset using block encoding
model, code_length, history = encoder.encode(
dataset=my_dataset,
set_name="My Dataset",
stop_points=[0.25, 0.5, 0.75, 1.0], # Points where to stop and evaluate
batch_size=32,
seed=42,
learning_rate=0.001,
epochs=50,
patience=20,
collate_fn=my_collate_fn # Your custom collate function
)
MIR Encoding
MIR (Mini-batch Incremental/Replay) encoding uses replay buffers or streams to revisit previous data.
from preqtorch import MIREncoder
# Create a MIR encoder
encoder = MIREncoder(
model_class=MyModel,
loss_fn=torch.nn.functional.cross_entropy
)
# Encode a dataset using MIR encoding
model, code_length, history, ema_params, beta, replay = encoder.encode(
dataset=my_dataset,
set_name="My Dataset",
n_replay_streams=2, # Number of replay streams or buffer size
learning_rate=0.001,
batch_size=32,
seed=42,
alpha=0.1, # EMA update rate
collate_fn=my_collate_fn, # Your custom collate function
use_beta=True, # Whether to use learnable temperature parameter
use_ema=True, # Whether to use exponential moving average
replay_type="buffer" # Type of replay: "buffer" or "streams"
)
License
This project is licensed under the MIT License - see the LICENSE file for details.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file preqtorch-0.0.2.tar.gz.
File metadata
- Download URL: preqtorch-0.0.2.tar.gz
- Upload date:
- Size: 10.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fb274ee6394bd7f4aefea0b3b7b11fff7e9a723a08045b0c594b9a26a4458876
|
|
| MD5 |
664b2c28962312afbc0f079f6a33f77d
|
|
| BLAKE2b-256 |
3821fa533d11ddab366e8f1932b3daf71e8a51d997c1f2ad4d1cd1bddec1298c
|
File details
Details for the file preqtorch-0.0.2-py3-none-any.whl.
File metadata
- Download URL: preqtorch-0.0.2-py3-none-any.whl
- Upload date:
- Size: 3.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
65039b0e5c3e7d365f40a46155644777a63f410270ca3cbfa154c870c92b9de1
|
|
| MD5 |
ab0dbab8dfd41d58439f31639a89eed7
|
|
| BLAKE2b-256 |
37d12582817e52865eb3b7e3cc81734c20518d6ce59565c797463ff8fb4fe69a
|