Package: xt-training
xt-trainingDescriptionThis repo contains utilities for training deep learning models in pytorch, developed byXtract AI.InstallationFrom PyPI:pipinstallxt-trainingFrom source:gitclonehttps://github.com/XtractTech/xt-training.git
pipinstall./xt-trainingUsageSee specific help on a class or function usinghelp. E.g.,help(Runner).Training a modelUsing xt-training (High Level)First, you must define a config file with the necessary items.
To generate a template config file, run:python-mxt_trainingtemplatepath/to/save/dirTo generate template files for nni, add the--nniflagInstructions 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-mxt_trainingtrainpath/to/config.py/path/to/save_dirYou can test the model by runningpython-mxt_trainingtestpath/to/config.py/path/to/save_dirUsing 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:fromxt_training.utilsimportfunctional# model =# train_loader =# optimizer =# scheduler =# loss_fn =# metrics =# epochs =# save_dir =defon_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 callreturnrunner,modelrunner,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 stepeval_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:fromxt_trainingimportRunner,metricsfromtorch.utils.tensorboardimportSummaryWriter# 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 printedbatch_metrics={'eps':metrics.EPS(),'acc':metrics.Accuracy(),'kappa':metrics.Kappa(),'cm':metrics.ConfusionMatrix()}# Define tensorboard writerwriter=SummaryWriter()# Create runnerrunner=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 =# Trainmodel.train()runner(train_loader)batch_metrics['cm'].print()# Evaluatemodel.eval()runner(val_loader)batch_metrics['cm'].print()# Print training and evaluation historyprint(runner)Scoring a modelimporttorchfromxt_trainingimportRunner# 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 =# Scoremodel.eval()y_pred,y=runner(test_loader,return_preds=True)