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, clone it to your local machine using:

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

Optional: Local pip installation

If you want to install the library locally using pip, navigate to the root directory of the project and run:

pip install .

Alternatively, to install it in editable mode for development purposes, run:

pip install -e .

Usage

Required Support Functions and Variables

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

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 earlystopping 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.0.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.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: librestopping-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 dc1aad45dc2c4fe4f0a8371bc42558b02442c3cd2be2d57b6fabf7c0757f1e42
MD5 77b2bd8bd56a0af0bdfd37d57ba78bba
BLAKE2b-256 8514c817c12b19dc0395b56b09d68e9bb7cfe09af7c5cd7c3a15eab983f898e8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: LibreStopping-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 15d164b0cac2a29eedd948166d3cb557257aa98909f14124718c3d4128ed33bd
MD5 e617792959798fb5253513c5bf20f7db
BLAKE2b-256 ece0bc89d072bbe585026c23a5b20f84cad3d007b94529781f224b441f34c790

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