No project description provided
Project description
deepepochs
Pytorch模型简易训练工具
使用
常规训练流程
from deepepochs import Trainer, Checker
import torch
from torch import nn
from torch.nn import functional as F
from torchvision.datasets import MNIST
from torchvision import transforms
from torch.utils.data import DataLoader, random_split
from torchmetrics import functional as MF
# datasets
data_dir = './datasets'
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))])
mnist_full = MNIST(data_dir, train=True, transform=transform, download=True)
train_ds, val_ds = random_split(mnist_full, [55000, 5000])
test_ds = MNIST(data_dir, train=False, transform=transform, download=True)
# dataloaders
train_dl = DataLoader(train_ds, batch_size=32)
val_dl = DataLoader(val_ds, batch_size=32)
test_dl = DataLoader(test_ds, batch_size=32)
# pytorch model
channels, width, height = (1, 28, 28)
model = nn.Sequential(
nn.Flatten(),
nn.Linear(channels * width * height, 64),
nn.ReLU(),
nn.Dropout(0.1),
nn.Linear(64, 64),
nn.ReLU(),
nn.Dropout(0.1),
nn.Linear(64, 10)
)
def acc(preds, targets):
return MF.accuracy(preds, targets, task='multiclass', num_classes=10)
def r(preds, targets):
return MF.recall(preds, targets, task='multiclass', num_classes=10)
def f1(preds, targets):
return MF.f1_score(preds, targets, task='multiclass', num_classes=10)
checker = Checker('loss', mode='min', patience=2)
opt = torch.optim.Adam(model.parameters(), lr=2e-4)
trainer = Trainer(model, F.cross_entropy, opt=opt, epochs=100, checker=checker, metrics=[acc, r, f1])
progress = trainer.fit(train_dl, val_dl)
test_rst = trainer.test(test_dl)
非常规训练流程
- 第1步:继承
deepepochs.TrainerBase
类,定制满足需要的Trainer
,实现train_step
方法和evaluate_step
方法 - 第2步:调用定制
Trainer
训练模型。
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
deepepochs-0.1.4.tar.gz
(8.3 kB
view details)
Built Distribution
File details
Details for the file deepepochs-0.1.4.tar.gz
.
File metadata
- Download URL: deepepochs-0.1.4.tar.gz
- Upload date:
- Size: 8.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bdff89f76a8c94b4cd0c154a7d255705476aa61f24a18906abcd364dc765b5f4 |
|
MD5 | ab21b1e18cf29e81cfad98cbc9766b42 |
|
BLAKE2b-256 | 7ffd0720384a08a1170b644cc4dc74955d38267f1af1139115148e9f5482f3cf |
File details
Details for the file deepepochs-0.1.4-py3-none-any.whl
.
File metadata
- Download URL: deepepochs-0.1.4-py3-none-any.whl
- Upload date:
- Size: 8.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 91eec526338bac89a78cd0df720a2cb8169f4008374ba561ed49fed705ee022f |
|
MD5 | 63eb1e93ac45fc0fbdaa5382df0c8df3 |
|
BLAKE2b-256 | 7308c2dfdd17586c563d79e8206d695949db1376a868807fddf545329ad5ece0 |