A lightweight training tool for pytorch projects.
Project description
TorchLiter
A freely customizable and truly lightweight training tool for any pytorch projects
Install
pip install torchliter
Example Usage:
import torchliter
import torch
import torch.nn as nn
import torch.nn.functional as F
cart = torchliter.Cart()
cart.model = nn.Linear(1, 3)
cart.train_loader = torch.utils.data.DataLoader(
[i for i in range(100)], batch_size=5
)
cart.eval_loader = torch.utils.data.DataLoader(
[i for i in range(100)], batch_size=5
)
cart.optimizer = torch.optim.AdamW(
cart.model.parameters(), lr=1e-3, weight_decay=1e-5
)
def train_step(_, batch, **kwargs):
image, target = batch
logits = _.model(image)
loss = F.cross_entropy(logits, target)
_.optimizer.zero_grad()
loss.backward()
_.optimizer.step()
yield "cross entropy loss", loss.item()
acc = (logits.max(-1).indices == target).float().mean()
yield "train acc", acc.item()
def eval_step(_, batch, **kwargs):
image, target = batch
with torch.no_grad():
logits = _.model(image)
acc = (logits.max(-1).indices == target).float().mean()
yield "eval acc", acc.item()
def hello(_):
print("hello")
train_buffers = torchliter.engine.AutoEngine.auto_buffers(
train_step, torchliter.buffers.ExponentialMovingAverage
)
eval_buffers = torchliter.engine.AutoEngine.auto_buffers(
eval_step, torchliter.buffers.ScalarSummaryStatistics
)
TestEngineClass = torchliter.engine.AutoEngine.build(
"TestEngine", train_step, eval_step, print_hello=hello
)
test_engine = TestEngineClass(**{**cart.kwargs, **train_buffers, **eval_buffers})
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
torchliter-0.3.0.tar.gz
(15.1 kB
view hashes)
Built Distribution
torchliter-0.3.0-py3-none-any.whl
(19.0 kB
view hashes)
Close
Hashes for torchliter-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1dbfcf75432455ec28a374a08541cc00dd309d9a8ddb3eed186656692acb20fb |
|
MD5 | ff3771ed9dec3816b1a0eff19d556aaa |
|
BLAKE2b-256 | f04abad2b5a8d78dc4b415f6ce7926911bc57d0ca86f457c2f49fe929c8045e9 |