Controlling Neural Network training from Web Interface

Parth Soni
Nybles
Published in
5 min readOct 1, 2021

We all know the process of deploying any trained Machine learning model and use the results in our Web Interface. But what if we wanna train our untrained model using a Web interface?

In this article, we’ll be discussing how to start, stop/pause and resume the training of a deep learning model and check the instantaneous training status, all from a Web user interface.

You might wonder 🤔…. How is it even useful?

Let’s see…

  • Similar to NVIDIA cloud computing, we could build a website that takes input data and gives a trained model with full control on training.
  • Or, multiple persons can build a small data center to store some hardware devices (GPUs) and use them using the web interface.
  • A training visualization website can be developed. This type of site can be helpful for beginners in understanding some model training, like updates in K-means, changing of 1D or 2D hyperplane in linear regression, etc.
  • The approach used in this might be pretty useful in the case of a continuously expanding database.

Here, I’ll use flask for the deployment of our project and Keras for training.

Prerequisites:

Prerequisites to understand this blog are basic knowledge of deep learning models using Keras, Threading in python, and the basics of Flask.

CONTENTS:

  1. About Flask
  2. Define parameters, variables for the model.
  3. Create functions to initialize model, perform training and finalize training.
  4. Create function for start, stop, resume, and status which gets called according to Web Interface path.
  5. Create a WEB Interface with buttons for controls and outputs.

FLASK

Flask is a small and lightweight Python web framework that provides useful tools and features that make creating web applications in Python easier.

Installing Flask on your Machine

Installing flask is pretty simple and straightforward. Just type this code in terminal or Powershell.

pip install flask

It would be better to install it in a virtual environment.

Why Flask?

  • Easy to use.
  • Built-in development server and debugger.
  • Integrated unit testing support.
  • RESTful request dispatching.
  • Extensively documented.

The Flask App

Import required libraries

Load or initialize data:

Since the aim of this blog is not to get into deep learning, I’ll take arbitrary data for simplicity.

On plotting, The dataset looks like this:

Declaring required Variables and parameters

Initialize Model

For simplicity, Let’s just consider a simple model with no hidden layer and a single output.

Functions to perform training:

perform_training() basically starts the training of our model based on the initialized model.

In this whole model, we’ll be using a bool variable training_status to control the start and stop of our training. perform_training() will terminate when either training is completed or training_status is changed to false .

In both of the above cases, finalize_training() will be called which finally outputs the results of our trained model.

We store the values of our weights and biases so that whenever we pause or stop our training, i.e.training_status is set to false, we can reuse them to resume training from the partially trained state.

Now comes the fun part….

Flask app rendering:

On visiting localhost:5000, the index function will be called that renders our HTML file. The buttons will redirect us to the path depending on the button clicked.

Ex: Clicking start will redirect to “/start”

Let’s discuss the process that will follow on clicking the buttons.

Start

Once we click the start button on our webpage, it will redirect to “/start” and the start_training function will get triggered. Here, we first initialize our model with init_model. We create a thread of perform_training function with training_status = true and training gets started. If the model is already trained, we would be having training = false from perform_training function itself.

Pause

When we click the pause button on our webpage, it will redirect to “/pause” and the pause_training function will get triggered. This function basically sets training = false and joins the thread that we created in our start_training function using training_thread.join(). On receiving training=false, our perform_training function will get terminated. The trained weights and number of completed epochs will be stored and will be used when we resume our training.

Resume:

When the resume button is clicked resume_training function will get triggered. This function again sets training = true. We initialize now initialize the weights of our model to the ones we got from paused training. Now, we again trigger our perform_training function with updated weights and epochs. Thus, our training will be resumed.

Status:

This status_training() will let us see the current weights of our model and tell whether our training is ongoing, paused, or completed.

Conclusion:

Once our model training completes, we get our final parameters as m = 0.301and c =5.017, which is almost the same as our starting values, i.e a=0.3 and b= 5.

There is no general strategy that fits every problem. Here, I have created and used a very simple dataset and just 2 parameters m and c. We can easily modify this for datasets requiring multiple parameters. Also, any model can be used in this with some minor changes. We can even try applying this for controlling multiples models concurrently. Also, Pytorch or TensorFlow can be used instead of Keras for even lower-level control.

So, That’s it…!

Thanks for the read. I hope this blog helps you in understanding the approach to this problem. Please let me know if you encounter any issues or are unable to follow the blog.

This is my first blog. If you liked it, 👏 and share it with your friends.

Please reach out for any comments or feedback. Cheers ..!

You can find the complete code (including the HTML file) on Github

About Me

I am Parth Soni, 2nd-year B-Tech student at IIIT Allahabad, an AI ML enthusiats, member at Artificial Intelligence Wing, Geekhaven IIIT Allahabad. Reach me out at LinkedIn

--

--