Neural Networks

How to train your first model in 15 minutes or less

Thirteen lines of code! That’s all that is going to take to train your first model using Keras.

Keras is a high-level neural networks API, written in Python and capable of running on top of TensorFlow, CNTK, or Theano. Since Keras defaults to TensorFlow, that’s what we are going to use as a backend.

By the end of this, you will have built a neural network as complex as the image below that can predict whether someone has diabetes or not based on 8 variables.

Alright! Look at the time now, because in fifteen minutes you should have your first model trained and ready to go!

Project dependencies are tensorflow, keras and numpy, so make sure you have those installed before we start coding.

We will use a dataset that contains information about patients with diabetes, they contain the variables mentioned above on this order:

  1. Number of times pregnant
  2. Plasma glucose concentration a 2 hours in an oral glucose tolerance test
  3. Diastolic blood pressure (mm Hg)
  4. Triceps skinfold thickness (mm)
  5. 2-Hour serum insulin (mu U/ml)
  6. Body mass index (weight in kg/(height in m)^2)
  7. Diabetes pedigree function
  8. Age (years)


I used a public machine learning dataset repository to retrieve this data, you can download it here. The dataset is a CSV with 9 columns, 8 with the variables listed above and a ninth one containing a boolean informing whether the person has diabetes or not.


There’s a lot going on above, how are we doing with the time?



We should have some time left to talk about what’s going on above. From lines 1 to 3 we are just importing a few dependencies, no biggy.

On line 6 we are using numpy to load our dataset.

Then on lines 9 and 10 we are splitting the input from the output, making sure the variables that characterize someone with diabetes (or normal blood sugar levels) are separated from the actual information that the person has diabetes (or not).

On line 13, we are instantiating the sequence of layers of our neural network, on line 14 we are both defining our input layer and readying it to receive 8 inputs, and creating the first hidden layer using the activation function “relu” with 12 nodes.

We then define another hidden layer on line 15, containing 8 nodes, that feeds the output layer defined on line 16.

Taking us to line 19 where we are compiling our model, making it ready for training – or fitting – on line 22.

We finish up online 24 where we save the model so it can be used for predictions later.

In short, what we’ve done above is, we fed a system with the same 8 characteristics of several people and them told the system if each of these people had diabetes or not. We then generated a model that contains all of that information. A system that can read that model will be able to use the historical data contained in it to predict if a person has diabetes or not.

It is that easy. I linked a few resources above, in case you are just starting and aren’t familiar with the jargons, also left out a few details that felt like would only overwhelm you at this stage.

Just run the above and a trained model named “diabetes.h5” should be created in the same folder as your application. Quickly run it! It takes a bit to train the model! Will it make it in under 15 mins?


What is next?

On this post, we use the model we trained above to predict whether someone has diabetes or not.