Generic Pytorch Trainer
Project description
Trainer-Pytorch
Implementation of a boilerplate pytorch trainer, with wandb and Huggingface accelerate integration.
Installation
pip install 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
Release history Release notifications | RSS feed
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.2.tar.gz
(8.3 kB
view details)
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 trainer_pytorch-0.0.2.tar.gz.
File metadata
- Download URL: trainer_pytorch-0.0.2.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6e5e41c5f3e466f99482c76f051c9101c5eb0b26f64cb58c5cd5a6c8500d8b3d
|
|
| MD5 |
063005cb602db4bac8cc0d5017cc5cfe
|
|
| BLAKE2b-256 |
d0ee3e98d816311a2b1762457e4ef2996962fe1ccde7c1e8496655e774162ddf
|
File details
Details for the file trainer_pytorch-0.0.2-py3-none-any.whl.
File metadata
- Download URL: trainer_pytorch-0.0.2-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
745ee1b32975ab54cca455b633fef1619762d169cef940c2bba0dca8ed42e7ce
|
|
| MD5 |
672f9d9d62f21aea1e67f20e1a9f8fc2
|
|
| BLAKE2b-256 |
0b3a237128c39ef0b2c5cfd3c5e75de7f1147e2a66ba0b9146a05908fdaeee55
|