A transparent boilerplate + bag of tricks to ease my (yours?) (our?) PyTorch dev time.
Some parts here are inspired/copied from fast.ai. However, I've tried to keep is such that the control of model (model architecture), vocabulary, preprocessing is always maintained outside of this library. The training loop, data samplers etc can be used independent of anything else in here, but ofcourse work better together.
I'll be adding proper documentation, examples here, gradually.
pip install my-torch
(Added hyphen because someone beat me to the mytorch package name.)
Use/Ignore most parts of the library. Will not hide code from you, and you retain control over your models. If you need just one thing, no fluff, feel free to copy-paste snippets of the code from this repo to yours. I'd be delighted if you drop me a line, if you found this stuff helpful.
-
Customizable Training Loop
- Callbacks @ epoch start and end
- Weight Decay (see this blog post )
- ✂️ Gradient Clipping
- 💾 Model Saving
- 🔔 Mobile push notifications @ the end of training 👻 ( See Usage) )
-
Sortist Sampling
-
Custom Learning Rate Schedules
-
Customisability & Flat Hierarchy
import torch, torch.nn as nn, numpy as np
# Assuming that you have a torch model with a predict and a forward function.
# model = MyModel()
assert type(model) is nn.Module
# X, Y are input and output labels for a text classification task with four classes. 200 examples.
X_trn = np.random.randint(0, 100, (200, 4))
Y_trn = np.random.randint(0, 4, (200, 1))
X_val = np.random.randint(0, 100, (100, 4))
Y_val = np.random.randint(0, 4, (100, 1))
# Preparing data
data = {"train":{"x":X_trn, "y":Y_trn}, "valid":{"x":X_val, "y":Y_val} }
# Specifying other hyperparameters
epochs = 10
optimizer = torch.optim.SGD(model.parameters(), lr=0.001)
loss_function = nn.functional.cross_entropy
train_function = model # or model.forward
predict_function = model.predict
train_acc, valid_acc, train_loss = loops.simplest_loop(epochs=epochs, data=data, opt=optimizer,
loss_fn=loss_function,
train_fn=train_function,
predict_fn=predict_function)
@TODO: They exist! Just need to add examples 😅
- Custom eval
- Custom data sampler
- Custom learning rate annealing schedules
@TODO
The training loop can send notifications to your phone informing you that your model's done training and report metrics alongwith. We use push.techulus.com to do so and you'll need the app on your phone. If you're not bothered, this part of the code will stay out of your way. But If you'd like this completely unnecessary gimmick, follow along:
- Get the app. Play Store | AppStore
- Sign In/Up and get yout api key
- Making the key available. Options:
- in a file, named
./push-techulus-key
, in plaintext at the root dir of this folder. You could justecho 'your-api-key' >> ./push-techulus-ley
. - through arguments to the training loop as a string
- in a file, named
- Pass flag to loop, to enable notifications
- Done 🎈 You'll be notified when your model's done training.
- Interfaced some metrics from torchmetrics, and implemented some more into a neat little package pending
- Added negative sampling
- [TODO] Added multiple evaluation functions
- [TODO] Logging
- [TODO] Typing all functions
- Added some tests.
- Wrapping spaCy tokenizers, with some vocab management.
- Packaging :confetti:
- Models
- Classifiers
- Encoders
Transformers(USE pytorch-transformers by :huggingface:)
- Using FastProgress for progress + live plotting
- W&B integration
- ?? (tell me here)
I'm eager to implement more tricks/features in the library, while maintaining the flat structure (and ensuring backward compatibility). Open to suggestions and contributions. Thanks!
PS: Always appreciate more tests.
An important part of the code was designed, and tested by :
Gaurav Maheshwari · GitHub @saist1993 · Twitter @__gauravm