Automatically Generate Machine Learning Code with Just a Few Clicks | by Kenneth Leung | Towards Data Science

Repeatedly writing template codes to train your machine learning (ML) models can be a huge pain.
While you may have base codes to work with (e.g., past projects, other teammates’ work), the manual customization of many code components like hyperparameters and input type can still cause unnecessary hassle.
This article introduces Traingenerator, a simple code generator for machine learning model training.
(1) About Traingenerator(2) How to Use(3) Conclusion
Traingenerator is a simple web application (deployed on Streamlit) that allows you to generate template codes in the two popular frameworks of PyTorch and scikit-learn.
The generated code covers various segments expected of a code template, including pre-processing, model setup, training, and visualization.
The templates available are currently limited to only image classification (with object detection on the way). Nonetheless, we can readily adapt these boilerplate codes to suit other ML tasks, even adding our own templates.
The beauty of this app is that it simplifies the work of data scientists while lowering the barrier for non-technical users to explore ML codes.
💻 Link to the web app: https://traingenerator.jrieke.com⌨️ Link to GitHub: https://github.com/jrieke/traingenerator🧑 Created by: Johannes Rieke
We will be using the PyTorch framework option for an image classification task to demonstrate how the Traingenerator web app works.
There are four neural network architectures to choose from, namely AlexNet, ResNet, DenseNet, and VGG.
For each of the latter three, you can select the specific variant based on the number of layers (e.g., ResNet 18, ResNet 34, ResNet 50, etc.).
After indicating the number of classes, you can decide if you want to use the ImageNet pre-trained model. If so, the generated code (e.g., for ResNet 18) will look like this:
model = models.resnet18(pretrained=True)
There are three options available for input data selection:
Public dataset: Choose from popular datasets like MNIST and CIFAR10
NumPy Arrays: Input data directly as NumPy arrays (organized in the appropriate array shapes)
Image Files: Provide directories (in the correct format) to your datasets
The pre-processing sequence is based on pre-defined Torchvision transformations as follows:
Resize the image to 256 x 256 pixelsCenter crop image to 224 x 224 pixelsNormalize image (after conversion to tensor) with mean and standard deviation specific to the selected neural network
Resize the image to 256 x 256 pixels
Center crop image to 224 x 224 pixels
Normalize image (after conversion to tensor) with mean and standard deviation specific to the selected neural network
Here is where you specify the details of model training:
Loss function (CrossEntropyLoss or BCEwithLogitsLoss)
Optimizer (e.g., Adam, Adagrad, SGD, etc.)
Hyperparameters (learning rate, batch size, epochs)
Logging (print progress every n batch)
Device (to use GPU or not)
You can choose to save model checkpoints for each epoch, but note that this can take up plenty of storage space.
You can select your preferred method of logging to track your model training runs. The available options include popular tools such as Tensorboard, Aim, and Weights&Biases.
And finally, you are now ready to download the training codes you have just customized.
Traingenerator provides three ways to do so, namely as a Jupyter notebook (.ipynb), Python script (.py), or Google Colab notebook.
In this writeup, we explored the auto code generation features provided by the Traingenerator web app. I have personally been using it for my PyTorch deep learning projects, so I hope you will find it helpful.
Given the limited templates available, you will be happy also to learn that you can add and save your templates without changing anything in the app. Just follow the simple steps outlined in the README.
I welcome you to join me on a data science learning journey! Follow this Medium page and check out my GitHub to stay in the loop of more exciting data science content. Meanwhile, have fun training your ML models!