Skip to main content

A library for implementing early stopping functionality in LibRecommender models

Project description

LibreStopping | LibRecommender Early Stopping

This repository provides an implementation of early stopping functionality for models in the LibRecommender framework. Early stopping is a form of regularization used to avoid overfitting when training a model. This implementation allows users to monitor a specified performance metric and halt training when performance ceases to improve.

Table of Contents

Features

  • Customizable Monitoring: Choose the metric to monitor for early stopping (e.g., ROC AUC, loss, precision).
  • Patience Parameter: Specify the number of epochs with no improvement before training is halted.
  • Model Saving: Automatically saves the best model based on the monitored metric.

Installation

To install the repository, install it using pip:

pip install LibreStopping

or clone it to your local machine using:

git clone https://github.com/GitJvG/LibreStopping.git

Usage

Required Support Functions and Variables

Before using the EarlyStopping class, ensure you have the required support functions and variables set up:

# These libraries are only examples
from libreco.algorithms import TwoTower
from libreco.evaluation import evaluate
from libreco.data import DatasetFeat

# Define example data (replace with your actual data loading)
train_data, user_col, item_col, sparse_col, dense_col, eval_data = ['...']
train_data, data_info = DatasetFeat.build_trainset(train_data, user_col, item_col, sparse_col, dense_col)
eval_data = DatasetFeat.build_evalset(eval_data)

# Example functions for creating, fitting, and evaluating a model
def create_model(data_info, n_epochs):
    # Create and return a TwoTower model
    return TwoTower(
        "ranking",
        data_info,
        n_epochs=n_epochs,
        embed_size=16,
        norm_embed=True,
    )

def fit_model(model, train_data, eval_data):
    # Fit the model using training and evaluation data
    model.fit(train_data, 
              neg_sampling=True, 
              eval_data=eval_data, 
              shuffle=True, 
              metrics=["roc_auc"])

def evaluate_model(model, eval_data):
    # Evaluate the model and return evaluation results
    evaluation_results = evaluate(
        model=model,
        data=eval_data,
        neg_sampling=True,
        metrics=["loss", "roc_auc", "precision", "recall", "ndcg"],
    )
    print(f"Evaluation Results: {evaluation_results}")
    return evaluation_results

Using EarlyStopping to Train Your Model

from LibreStopping.LibreStopping import EarlyStopping  # Import the EarlyStopping class

# Initialize EarlyStopping
early_stopping = EarlyStopping(
    model_path="path/to/save/model",  # Path to save the best model
    model_name="best_model",           # Name of the model
    data_info=data_info,               # Dataset information
    patience=5,                        # Number of epochs to wait before stopping
    monitor_metric="loss"           # Metric to monitor
)

# Start training with early stopping
best_model = early_stopping.train_with_early_stopping(
    create_model=create_model,  # Function to create the model
    fit_model=fit_model,        # Function to fit the model
    train_data=train_data,      # Training data
    eval_data=eval_data,        # Evaluation data
    evaluate_model=evaluate_model # Function to evaluate the model
)

License

This project is licensed under the MIT License. See the LICENSE file for more 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

librestopping-0.1.1.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

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

LibreStopping-0.1.1-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: librestopping-0.1.1.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.14

File hashes

Hashes for librestopping-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f3e08269cbf9dc9a3099bd93ed3ef62b31909646deaeb0394ece1fb2305de529
MD5 c2ce09699cd290455b9e52227d61ac3f
BLAKE2b-256 d62cdad2f74c777edb04939b054fe630455c54cc0e0aa74c5693665ae3605ab2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: LibreStopping-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.14

File hashes

Hashes for LibreStopping-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4c926397661cbc0b4bad49521eb28803039b4ad9707efd594c12cefe4cfd991f
MD5 7251d3e6410a5d976c17c096cd92753c
BLAKE2b-256 726df23da66b06c41cbb03aa4291227f878bc9806913556af3eea528a011b47d

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