Skip to main content

Generic Pytorch Trainer

Project description

Trainer-Pytorch

Implementation of a boilerplate pytorch trainer, with wandb and Huggingface accelerate integration.

Installation

pip instal trainer_pytorch

Usage

If you want to use accelerate and wandb:

# initialize wandb
wandb login

# initialize accelerate
accelerate launch

Then, you can initialize the trainer with your model, optimizer, loss function, and scheduler:

# Initialize the TrainerPytorch with a simple model, optimizer, and loss function
model = nn.Linear(10, 1)
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
loss_fn = nn.MSELoss()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=1, gamma=0.1)
trainer = TrainerPytorch(model, optimizer, loss_fn, scheduler=scheduler, device=device, 
                         use_wandb=True, use_accelerate=True, save_dir='my_experiment')

# generate dummy data
inputs = torch.randn(100, 10)
targets = torch.randn(100, 1)
dataset = TensorDataset(inputs, targets)
train_loader = DataLoader(dataset, batch_size=10, shuffle=True)
eval_loader = DataLoader(dataset, batch_size=10, shuffle=False)

# Train the model
trainer.train(train_loader, eval_loader=eval_loader, epochs=5, patience=2)

# Evaluate the model
outputs = trainer.predict_samples(inputs[:5])

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

trainer_pytorch-0.0.1.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

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

trainer_pytorch-0.0.1-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file trainer_pytorch-0.0.1.tar.gz.

File metadata

  • Download URL: trainer_pytorch-0.0.1.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for trainer_pytorch-0.0.1.tar.gz
Algorithm Hash digest
SHA256 3f869faef92cdb79dda764d7819dec6966f30cc4298e003bf8474e31eb40cf62
MD5 6e430e91f9f11c7ec82f8724bf8cb080
BLAKE2b-256 56169d5b28088d07676f6dde616fcc08b9fabdc6d9556446cd6896a5c3742d87

See more details on using hashes here.

File details

Details for the file trainer_pytorch-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for trainer_pytorch-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 78b737e3f5ea61a82cc83e608ea1ee8fe86ed1b04a374a58c16f0abdc3c4b44f
MD5 71f5e2722cc19f8961ebb02f6a078b8b
BLAKE2b-256 6240e7de3770c490dada41a9e0261c98ec541e7120d8032ef04278e4a44c0c90

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