xt-training
Description
This repo contains utilities for training deep learning models in pytorch, developed by Xtract AI.
Installation
From PyPI:
pip install xt-training
From source:
git clone https://github.com/XtractTech/xt-training.git
pip install ./xt-training
Usage
See specific help on a class or function using help. E.g., help(Runner).
Training a model
Using xt-training (High Level)
First, you must define a config file with the necessary items. To generate a template config file, run:
python -m xt_training template path/to/save/dir
To generate template files for nni, add the --nni flag
Instructions for defining a valid config file can be seen at the top of the config file.
After defining a valid config file, you can train your model by running:
python -m xt_training train path/to/config.py /path/to/save_dir
You can test the model by running
python -m xt_training test path/to/config.py /path/to/save_dir
Using functional train (Middle Level)
As of version >=2.0.0, xt-training has functional calls for the train and test functions This is useful if you want to run other code after training, or want any values/metrics returned after training. This can be called like so:
from xt_training.utils import functional
# model =
# train_loader =
# optimizer =
# scheduler =
# loss_fn =
# metrics =
# epochs =
# save_dir =
def on_exit(test_loaders, runner, save_dir, model):
# Do what you want after training.
# As of version >=2.0.0. whatever gets returned here will get returned from the functional call
return runner, model
runner, model = functional.train(
save_dir,
train_loader,
model,
optimizer,
epochs,
loss_fn,
val_loader=None,
test_loaders=None,
scheduler=scheduler,
is_batch_scheduler=False, # Whether or not to run scheduler.step() every epoch or every step
eval_metrics=metrics,
tokenizer=None,
on_exit=train_exit,
use_nni=False
)
# Do something after with runner and/or model...
A similar functional call exists for test.
Using Runner (Low Level)
If you want a little more control and want to define the trianing code yourself, you can utilize the Runner like so:
from xt_training import Runner, metrics
from torch.utils.tensorboard import SummaryWriter
# Here, define class instances for the required objects
# model =
# optimizer =
# scheduler =
# loss_fn =
# Define metrics - each of these will be printed for each iteration
# Either per-batch or running-average values can be printed
batch_metrics = {
'eps': metrics.EPS(),
'acc': metrics.Accuracy(),
'kappa': metrics.Kappa(),
'cm': metrics.ConfusionMatrix()
}
# Define tensorboard writer
writer = SummaryWriter()
# Create runner
runner = Runner(
model=model,
loss_fn=loss_fn,
optimizer=optimizer,
scheduler=scheduler,
batch_metrics=batch_metrics,
device='cuda:0',
writer=writer
)
# Define dataset and loaders
# dataset =
# train_loader =
# val_loader =
# Train
model.train()
runner(train_loader)
batch_metrics['cm'].print()
# Evaluate
model.eval()
runner(val_loader)
batch_metrics['cm'].print()
# Print training and evaluation history
print(runner)
Scoring a model
import torch
from xt_training import Runner
# Here, define the model
# model =
# model.load_state_dict(torch.load(<checkpoint file>))
# Create runner
# (alternatively, can use a fully-specified training runner as in the example above)
runner = Runner(model=model, device='cuda:0')
# Define dataset and loaders
# dataset =
# test_loader =
# Score
model.eval()
y_pred, y = runner(test_loader, return_preds=True)
Metadata
Release files for xt-training 2.4.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| xt-training-2.4.3.tar.gz | 20.1 kB | Without provenance | Details for xt-training-2.4.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| xt_training-2.4.3-py3-none-any.whl | Python 3 | none | any | Details for xt_training-2.4.3-py3-none-any.whl |
Total release size: 43.8 kB
Release files / xt-training-2.4.3.tar.gz
| Download URL | xt-training-2.4.3.tar.gz |
|---|---|
| Size | 20.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b109f8af571b17f0f2e7b2f7cc485505299f5abb92dc2aab09c4cb2efa129c4a
|
|
BLAKE2b-256 checksum How to use checksums |
9d50a84b61199c5fc6118370a7dc73c5dd253e3e2f3eb5f062c71beae04704ce
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0.post20200518 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7
|
Release files / xt_training-2.4.3-py3-none-any.whl
| Download URL | xt_training-2.4.3-py3-none-any.whl |
|---|---|
| Size | 23.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5e967e68f010dc5e724bb7245d1712be9e3fdca61e49be73c8a97539b966edef
|
|
BLAKE2b-256 checksum How to use checksums |
17496b0a53bc2c7586aaff7f6831b719ed4e578fb4b1c6e5e5b10cd91a61e1d3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0.post20200518 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7
|
Security for xt-training 2.4.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.