Your First Deep Learning Project in Python with Keras Step-by-Step
- Get link
- X
- Other Apps
Last Updated on August 16, 2023
Keras is a strong and easy-to-use free open provide Python library for rising and evaluating deep learning fashions.
It is part of the TensorFlow library and means you can define and put together neural neighborhood fashions in only some traces of code.
In this tutorial, you will uncover strategies to create your first deep learning neural neighborhood model in Python using Keras.
Kick-start your enterprise with my new e-book Deep Learning With Python, along with step-by-step tutorials and the Python provide code recordsdata for all examples.
Let’s get started.
- Update Feb/2023: Updated prediction occasion, so rounding works in Python 2 and three.
- Update Mar/2023: Updated occasion for the latest variations of Keras and TensorFlow.
- Update Mar/2023: Added alternate hyperlink to acquire the dataset.
- Update Jul/2023: Expanded and added additional useful sources.
- Update Sep/2023: Updated for Keras v2.2.5 API.
- Update Oct/2023: Updated for Keras v2.3.0 API and TensorFlow v2.0.0.
- Update Aug/2023: Updated for Keras v2.4.3 and TensorFlow v2.3.
- Update Oct/2023: Deprecated predict_class syntax
- Update Jun/2023: Updated to modern TensorFlow syntax

Develop your first neural neighborhood in Python with Keras step-by-step
Photo by Phil Whitehouse, some rights reserved.
Keras Tutorial Overview
There should not be a lot of code required, nonetheless we’ll go over it slowly so that you will know strategies to create your particular person fashions in the end.
The steps you will be taught on this tutorial are as follows:
- Load Data
- Define Keras Model
- Compile Keras Model
- Fit Keras Model
- Evaluate Keras Model
- Tie It All Together
- Make Predictions
This Keras tutorial makes numerous assumptions. You may need to have:
- Python 2 or 3 put in and configured
- SciPy (along with NumPy) put in and configured
- Keras and a backend (Theano or TensorFlow) put in and configured
If you want help collectively together with your environment, see the tutorial:
- How to Setup a Python Environment for Deep Learning
Create a model new file known as keras_first_network.py and type or copy-and-paste the code into the file as you go.
Need help with Deep Learning in Python?
Take my free 2-week e-mail course and uncover MLPs, CNNs and LSTMs (with code).
Click to sign-up now and likewise get a free PDF Ebook mannequin of the course.
1. Load Data
The first step is to stipulate the options and programs you intend to utilize on this tutorial.
You will use the NumPy library to load your dataset and two programs from the Keras library to stipulate your model.
The imports required are listed below.
1 2 3 4 5 | # first neural neighborhood with keras tutorial from numpy import loadtxt from tensorflow.keras.fashions import Sequential from tensorflow.keras.layers import Dense ... |
You can now load our dataset.
In this Keras tutorial, you will use the Pima Indians onset of diabetes dataset. This is a daily machine learning dataset from the UCI Machine Learning repository. It describes affected particular person medical file data for Pima Indians and whether or not or not they’d an onset of diabetes inside 5 years.
As such, it is a binary classification draw back (onset of diabetes as 1 or not as 0). All of the enter variables that describe each affected particular person are numerical. This makes it easy to utilize immediately with neural networks that anticipate numerical enter and output values and is an ideal various for our first neural neighborhood in Keras.
The dataset is obtainable proper right here:
Download the dataset and place it in your native working itemizing, the an identical location as your Python file.
Save it with the filename:
1 | pima-indians-diabetes.csv |
Take a look contained within the file; you will need to see rows of data like the subsequent:
1 2 3 4 5 6 | 6,148,72,35,0,33.6,0.627,50,1 1,85,66,29,0,26.6,0.351,31,0 8,183,64,0,0,23.3,0.672,32,1 1,89,66,23,94,28.1,0.167,21,0 0,137,40,35,168,43.1,2.288,33,1 … |
You can now load the file as a matrix of numbers using the NumPy carry out loadtxt().
There are eight enter variables and one output variable (the ultimate column). You will possible be learning a model to map rows of enter variables (X) to an output variable (y), which is usually summarized as y = f(X).
The variables may very well be summarized as follows:
Input Variables (X):
- Number of events pregnant
- Plasma glucose focus at 2 hours in an oral glucose tolerance check out
- Diastolic blood pressure (mm Hg)
- Triceps pores and pores and skin fold thickness (mm)
- 2-hour serum insulin (mu U/ml)
- Body mass index (weight in kg/(peak in m)^2)
- Diabetes pedigree carry out
- Age (years)
Output Variables (y):
- Class variable (0 or 1)
Once the CSV file is loaded into memory, you might break up the columns of data into enter and output variables.
The data will possible be saved in a 2D array the place the first dimension is rows and the second dimension is columns, e.g., [rows, columns].
You can break up the array into two arrays by deciding on subsets of columns using the same old NumPy slice operator or “:”. You can select the first eight columns from index 0 to index 7 by way of the slice 0:8. We can then select the output column (the ninth variable) by way of index 8.
1 2 3 4 5 6 7 | ... # load the dataset dataset = loadtxt(‘pima-indians-diabetes.csv’, delimiter=‘,’) # break up into enter (X) and output (y) variables X = dataset[:,0:8] y = dataset[:,8] ... |
You for the time being are capable of define your neural neighborhood model.
Note: The dataset has 9 columns, and the range 0:8 will select columns from 0 to 7, stopping sooner than index 8. If that’s new to you, then you might be taught additional about array slicing and ranges on this put up:
- How to Index, Slice, and Reshape NumPy Arrays for Machine Learning in Python
2. Define Keras Model
Models in Keras are outlined as a sequence of layers.
We create a Sequential model and add layers individually until we’re happy with our neighborhood construction.
The very very first thing to get correct is to ensure the enter layer has the correct number of enter choices. This may very well be specified when creating the first layer with the input_shape argument and setting it to (8,) for presenting the eight enter variables as a vector.
How do everyone knows the number of layers and their varieties?
This is a tough question. There are heuristics that it’s good to use, and generally probably the greatest neighborhood building is found by means of a method of trial and error experimentation (I make clear additional about this proper right here). Generally, you desire a neighborhood big adequate to grab the development of the difficulty.
In this occasion, let’s use a fully-connected neighborhood building with three layers.
Fully associated layers are outlined using the Dense class. You can specify the number of neurons or nodes throughout the layer as the first argument and the activation carry out using the activation argument.
Also, you will use the rectified linear unit activation carry out often called ReLU on the first two layers and the Sigmoid carry out throughout the output layer.
It was as soon as the case that Sigmoid and Tanh activation options had been hottest for all layers. These days, larger effectivity is achieved using the ReLU activation carry out. Using a sigmoid on the output layer ensures your neighborhood output is between 0 and 1 and is easy to map to each a probability of sophistication 1 or snap to a tricky classification of each class with a default threshold of 0.5.
You can piece all of it collectively by together with each layer:
- The model expects rows of data with 8 variables (the input_shape=(8,) argument).
- The first hidden layer has 12 nodes and makes use of the relu activation carry out.
- The second hidden layer has 8 nodes and makes use of the relu activation carry out.
- The output layer has one node and makes use of the sigmoid activation carry out.
1 2 3 4 5 6 7 | ... # define the keras model model = Sequential() model.add(Dense(12, input_shape=(8,), activation=‘relu’)) model.add(Dense(8, activation=‘relu’)) model.add(Dense(1, activation=‘sigmoid’)) ... |
Note: The most intricate issue proper right here is that the type of the enter to the model is printed as an argument on the first hidden layer. This signifies that the street of code that gives the first Dense layer is doing two points, defining the enter or seen layer and the first hidden layer.
3. Compile Keras Model
Now that the model is printed, you might compile it.
Compiling the model makes use of the surroundings pleasant numerical libraries beneath the covers (the so-called backend) resembling Theano or TensorFlow. The backend mechanically chooses the simplest strategy to signify the neighborhood for teaching and making predictions to run in your {{hardware}}, resembling CPU, GPU, and even distributed.
When compiling, it is important to specify some additional properties required when teaching the neighborhood. Remember teaching a neighborhood means discovering probably the greatest set of weights to map inputs to outputs in your dataset.
You ought to specify the loss carry out to utilize to guage a set of weights, the optimizer used to look by means of completely totally different weights for the neighborhood, and any non-compulsory metrics it’s good to collect and report all through teaching.
In this case, use cross entropy as a result of the loss argument. This loss is for a binary classification points and is printed in Keras as “binary_crossentropy“. You might be taught additional about deciding on loss options based in your draw back proper right here:
- How to Choose Loss Functions When Training Deep Learning Neural Networks
We will define the optimizer as a result of the surroundings pleasant stochastic gradient descent algorithm “adam“. This is a popular mannequin of gradient descent on account of it mechanically tunes itself and offers good ends in quite a lot of points. To be taught additional regarding the Adam mannequin of stochastic gradient descent, see the put up:
- Gentle Introduction to the Adam Optimization Algorithm for Deep Learning
Finally, on account of it is a classification draw back, you will collect and report the classification accuracy outlined by way of the metrics argument.
1 2 3 4 | ... # compile the keras model model.compile(loss=‘binary_crossentropy’, optimizer=‘adam’, metrics=[‘accuracy’]) ... |
4. Fit Keras Model
You have outlined your model and compiled it to organize for surroundings pleasant computation.
Now it is time to execute the model on some data.
You can put together or fit your model in your loaded data by calling the match() carry out on the model.
Training occurs over epochs, and each epoch is break up into batches.
- Epoch: One cross by means of all of the rows throughout the teaching dataset
- Batch: One or additional samples thought-about by the model inside an epoch sooner than weights are updated
One epoch accommodates numerous batches, primarily based totally on the chosen batch dimension, and the model is match for lots of epochs. For additional on the excellence between epochs and batches, see the put up:
- What is the Difference Between a Batch and an Epoch in a Neural Network?
The teaching course of will run for a set number of epochs (iterations) by means of the dataset that it is important to specify using the epochs argument. You ought to moreover set the number of dataset rows which might be thought-about sooner than the model weights are updated inside each epoch, known as the batch dimension, and set using the batch_size argument.
This draw back will run for a small number of epochs (150) and use a relatively small batch dimension of 10.
These configurations may very well be chosen experimentally by trial and error. You want to organize the model adequate so that it learns a superb (or enough) mapping of rows of enter data to the output classification. The model will on a regular basis have some error, nonetheless the amount of error will diploma out after some stage for a given model configuration. This often called model convergence.
1 2 3 4 | ... # match the keras model on the dataset model.match(X, y, epochs=150, batch_size=10) ... |
This is the place the work happens in your CPU or GPU.
No GPU is required for this occasion, nonetheless do you have to’re fascinated about strategies to run big fashions on GPU {{hardware}} cheaply throughout the cloud, see this put up:
- How to Setup Amazon AWS EC2 GPUs to Train Keras Deep Learning Models
5. Evaluate Keras Model
You have educated our neural neighborhood on the entire dataset, and you will think about the effectivity of the neighborhood on the an identical dataset.
This will solely give you an idea of how properly you’ve got modeled the dataset (e.g., put together accuracy), nonetheless no idea of how properly the algorithm might perform on new data. This was executed for simplicity, nonetheless ideally, you probably can separate your data into put together and check out datasets for teaching and evaluation of your model.
You can think about your model in your teaching dataset using the think about() carry out and cross it the an identical enter and output used to educate the model.
This will generate a prediction for each enter and output pair and collect scores, along with the standard loss and any metrics you’ve got configured, resembling accuracy.
The think about() carry out will return a list with two values. The first could be the dearth of the model on the dataset, and the second could be the accuracy of the model on the dataset. You are solely fascinated about reporting the accuracy so ignore the loss price.
1 2 3 4 | ... # think about the keras model _, accuracy = model.think about(X, y) print(‘Accuracy: %.2f’ % (accuracy*100)) |
6. Tie It All Together
You have merely seen how one can merely create your first neural neighborhood model in Keras.
Let’s tie all of it collectively into a whole code occasion.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | # first neural neighborhood with keras tutorial from numpy import loadtxt from tensorflow.keras.fashions import Sequential from tensorflow.keras.layers import Dense # load the dataset dataset = loadtxt(‘pima-indians-diabetes.csv’, delimiter=‘,’) # break up into enter (X) and output (y) variables X = dataset[:,0:8] y = dataset[:,8] # define the keras model model = Sequential() model.add(Dense(12, input_shape=(8,), activation=‘relu’)) model.add(Dense(8, activation=‘relu’)) model.add(Dense(1, activation=‘sigmoid’)) # compile the keras model model.compile(loss=‘binary_crossentropy’, optimizer=‘adam’, metrics=[‘accuracy’]) # match the keras model on the dataset model.match(X, y, epochs=150, batch_size=10) # think about the keras model _, accuracy = model.think about(X, y) print(‘Accuracy: %.2f’ % (accuracy*100)) |
You can copy the entire code into your Python file and reserve it as “keras_first_network.py” within the an identical itemizing as your data file “pima-indians-diabetes.csv“. You can then run the Python file as a script out of your command line (command speedy) as follows:
1 | python keras_first_network.py |
Running this occasion, you will need to see a message for each of the 150 epochs, printing the loss and accuracy, adopted by the last word evaluation of the educated model on the teaching dataset.
It takes about 10 seconds to execute on my workstation engaged on the CPU.
Ideally, you need to the loss to go to zero and the accuracy to go to 1.0 (e.g., 100%). This should not be attainable for any nonetheless in all probability probably the most trivial machine learning points. Instead, you will on a regular basis have some error in your model. The goal is to determine on a model configuration and training configuration that get hold of the underside loss and highest accuracy attainable for a given dataset.
1 2 3 4 5 6 7 8 9 10 11 12 | … 768/768 [==============================] – 0s 63us/step – loss: 0.4817 – acc: 0.7708 Epoch 147/150 768/768 [==============================] – 0s 63us/step – loss: 0.4764 – acc: 0.7747 Epoch 148/150 768/768 [==============================] – 0s 63us/step – loss: 0.4737 – acc: 0.7682 Epoch 149/150 768/768 [==============================] – 0s 64us/step – loss: 0.4730 – acc: 0.7747 Epoch 150/150 768/768 [==============================] – 0s 63us/step – loss: 0.4754 – acc: 0.7799 768/768 [==============================] – 0s 38us/step Accuracy: 76.56 |
Note: If you try working this occasion in an IPython or Jupyter pocket guide, it is potential you will get an error.
The motive is the output progress bars all through teaching. You can merely flip these off by setting verbose=0 throughout the identify to the match() and think about() options; as an example:
1 2 3 4 5 6 | ... # match the keras model on the dataset with out progress bars model.match(X, y, epochs=150, batch_size=10, verbose=0) # think about the keras model _, accuracy = model.think about(X, y, verbose=0) ... |
Note: Your outcomes may fluctuate given the stochastic nature of the algorithm or evaluation course of, or variations in numerical precision. Consider working the occasion numerous events and look at the standard closing end result.
What ranking did you get?
Post your ends within the suggestions below.
Neural networks are stochastic algorithms, that implies that the an identical algorithm on the an identical data can put together a definite model with completely totally different potential each time the code is run. This is a perform, not a bug. You might be taught additional about this throughout the put up:
- Embrace Randomness in Machine Learning
The variance throughout the effectivity of the model signifies that to get a reasonable approximation of how properly your model is performing, it is potential you will need to go well with it many events and calculate the standard of the accuracy scores. For additional on this methodology to evaluating neural networks, see the put up:
- How to Evaluate the Skill of Deep Learning Models
For occasion, below are the accuracy scores from re-running the occasion 5 events:
1 2 3 4 5 | Accuracy: 75.00 Accuracy: 77.73 Accuracy: 77.60 Accuracy: 78.12 Accuracy: 76.17 |
You can see that each one accuracy scores are spherical 77%, and the standard is 76.924%.
7. Make Predictions
The major question I get requested is:
“After I train my model, how can I use it to make predictions on new data?”
Great question.
You can adapt the above occasion and use it to generate predictions on the teaching dataset, pretending it is a new dataset you have not seen sooner than.
Making predictions is as easy as calling the predict() carry out on the model. You are using a sigmoid activation carry out on the output layer, so the predictions will possible be a probability throughout the range between 0 and 1. You can merely convert them proper right into a crisp binary prediction for this classification job by rounding them.
For occasion:
1 2 3 4 5 | ... # make probability predictions with the model predictions = model.predict(X) # spherical predictions rounded = [round(x[0]) for x in predictions] |
Alternately, you might convert the probability into 0 or 1 to predict crisp programs immediately; as an example:
1 2 3 | ... # make class predictions with the model predictions = (model.predict(X) > 0.5).astype(int) |
The full occasion below makes predictions for each occasion throughout the dataset, then prints the enter data, predicted class, and anticipated class for the first 5 examples throughout the dataset.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | # first neural neighborhood with keras make predictions from numpy import loadtxt from tensorflow.keras.fashions import Sequential from tensorflow.keras.layers import Dense # load the dataset dataset = loadtxt(‘pima-indians-diabetes.csv’, delimiter=‘,’) # break up into enter (X) and output (y) variables X = dataset[:,0:8] y = dataset[:,8] # define the keras model model = Sequential() model.add(Dense(12, input_shape=(8,), activation=‘relu’)) model.add(Dense(8, activation=‘relu’)) model.add(Dense(1, activation=‘sigmoid’)) # compile the keras model model.compile(loss=‘binary_crossentropy’, optimizer=‘adam’, metrics=[‘accuracy’]) # match the keras model on the dataset model.match(X, y, epochs=150, batch_size=10, verbose=0) # make class predictions with the model predictions = (model.predict(X) > 0.5).astype(int) # summarize the first 5 circumstances for i in range(5): print(‘%s => %d (anticipated %d)’ % (X[i].tolist(), predictions[i], y[i])) |
Running the occasion does not current the progress bar as sooner than, as a result of the verbose argument has been set to 0.
After the model is match, predictions are made for all examples throughout the dataset, and the enter rows and predicted class price for the first 5 examples is printed and as compared with the anticipated class price.
You can see that the majority rows are appropriately predicted. In fact, you might anticipate about 76.9% of the rows to be appropriately predicted based in your estimated effectivity of the model throughout the earlier half.
1 2 3 4 5 | [6.0, 148.0, 72.0, 35.0, 0.0, 33.6, 0.627, 50.0] => 0 (anticipated 1) [1.0, 85.0, 66.0, 29.0, 0.0, 26.6, 0.351, 31.0] => 0 (anticipated 0) [8.0, 183.0, 64.0, 0.0, 0.0, 23.3, 0.672, 32.0] => 1 (anticipated 1) [1.0, 89.0, 66.0, 23.0, 94.0, 28.1, 0.167, 21.0] => 0 (anticipated 0) [0.0, 137.0, 40.0, 35.0, 168.0, 43.1, 2.288, 33.0] => 1 (anticipated 1) |
If you want to know additional about strategies to make predictions with Keras fashions, see the put up:
- How to Make Predictions with Keras
Keras Tutorial Summary
In this put up, you discovered strategies to create your first neural neighborhood model using the extremely efficient Keras Python library for deep learning.
Specifically, you found the six key steps in using Keras to create a neural neighborhood or deep learning model step-by-step, along with:
- How to load data
- How to stipulate a neural neighborhood in Keras
- How to compile a Keras model using the surroundings pleasant numerical backend
- How to educate a model on data
- How to guage a model on data
- How to make predictions with the model
Do you’ve got any questions on Keras or about this tutorial?
Ask your question throughout the suggestions, and I’ll do my most interesting to answer.
Keras Tutorial Extensions
Well executed, you’ve got effectively developed your first neural neighborhood using the Keras deep learning library in Python.
This half affords some extensions to this tutorial that you just may have to find.
- Tune the Model. Change the configuration of the model or teaching course of and see ought to you possibly can improve the effectivity of the model, e.g., get hold of larger than 76% accuracy.
- Save the Model. Update the tutorial to avoid wasting a lot of the model to a file, then load it later and use it to make predictions (see this tutorial).
- Summarize the Model. Update the tutorial to summarize the model and create a plot of model layers (see this tutorial).
- Separate, Train, and Test Datasets. Split the loaded dataset proper into a training and check out set (break up primarily based totally on rows) and use one set to educate the model and the alternative set to estimate the effectivity of the model on new data.
- Plot Learning Curves. The match() carry out returns a historic previous object that summarizes the loss and accuracy on the end of each epoch. Create line plots of this information, known as learning curves (see this tutorial).
- Learn a New Dataset. Update the tutorial to utilize a definite tabular dataset, perhaps from the UCI Machine Learning Repository.
- Use Functional API. Update the tutorial to utilize the Keras Functional API for outlining the model (see this tutorial).
Further Reading
Are you looking for some additional Deep Learning tutorials with Python and Keras?
Take a check out a number of of those:
Related Tutorials
- 5 Step Life-Cycle for Neural Network Models in Keras
- Multi-Class Classification Tutorial with the Keras Deep Learning Library
- Regression Tutorial with the Keras Deep Learning Library in Python
- How to Grid Search Hyperparameters for Deep Learning Models in Python With Keras
Books
- Deep Learning (Textbook), 2023.
- Deep Learning with Python (my e-book).
APIs
How did you go? Do you’ve got any questions on deep learning?
Post your questions throughout the suggestions below, and I’ll do my most interesting to help.

TensorFlow 2 Tutorial: Get Started in Deep Learning…

How to Develop a CNN From Scratch for CIFAR-10 Photo…

Multi-Label Classification of Satellite Photos of…

9 Ways to Get Help with Deep Learning in Keras

How to Develop a GAN for Generating MNIST Handwritten Digits

Applied Deep Learning in Python Mini-Course
- Get link
- X
- Other Apps
Comments
Post a Comment