How to install Keras and TensorFlow

How to install keras and tensorflow

This Python tutorial is a part of our series of Python packages related tutorials.

Keras and TensorFlow are open source Python libraries for working with neural networks, creating machine learning models and performing deep learning. Because Keras is a high level API for TensorFlow, they are installed together.

In general, there are two ways to install Keras and TensorFlow:

  • Install a Python distribution that includes hundreds of popular packages (including Keras and TensorFlow) such as ActivePython.
  • Use pip to install TensorFlow, which will also install Keras at the same time.

Pip Install TensorFlow

Instead of pip installing each package separately, the recommended approach is to install Keras as part of the TensorFlow installation. When you install TensorFlow 2.0+,  Keras will be automatically installed, as well. 

The simplest way to install TensorFlow is to install the binary version using one of the official releases on the Python Package Index (PyPI).

TensorFlow can be run on three different processor platforms, with the main difference being the speed at which your neural network will be trained. Each platform has different hardware requirements and offers different performance:

  • CPU – any modern computer can run this version, but it offers the slowest training speeds.
  • TPU – only available currently on Google’s Colaboratory (Colab) platform, Tensor Processing Units (TPUs) offer the highest training speeds.
  • GPU – most high end computers feature a separate Graphics Processing Unit (GPU) from Nvidia or AMD that offer training speeds much faster than CPUs, but not as fast as TPUs. 

TensorFlow Requirements

TensorFlow and Keras require Python 3.6+ (Python 3.8 requires TensorFlow 2.2+) , and the latest version of pip. You can determine the version of Python installed on your computer by running the following command:

python3 --version

Output should be similar to:

Python 3.8.2

Run the following command to ensure that the latest version of pip is installed:

pip install --upgrade pip

To install TensorFlow for CPU and GPU processors, run the following command:

pip install tensorflow

If you’re fine with using the CPU to train your neural network, your installation is done. If you want to use your GPU to the training, you’ll need to do the following:

 The installation installs a slew of TensorFlow and Keras dependencies:

tensorflow                                 
├── absl-py~=0.10                          
│   └── six                                
├── astunparse~=1.6.3                      
│   ├── six<2.0,>=1.6.1                    
│   └── wheel<1.0,>=0.23.0                 
├── flatbuffers~=1.12.0                    
├── gast==0.3.3                            
├── google-pasta~=0.2                      
│   └── six                                
├── grpcio~=1.32.0                         
│   └── six>=1.5.2                         
├── h5py~=2.10.0                           
│   ├── numpy>=1.7                         
│   └── six                                
├── keras-preprocessing~=1.1.2             
│   ├── numpy>=1.9.1                       
│   └── six>=1.9.0                         
├── numpy~=1.19.2                          
├── opt-einsum~=3.3.0                      
│   └── numpy>=1.7                         
├── protobuf>=3.9.2                        
│   └── six>=1.9                           
├── six~=1.15.0                            
├── tensorboard~=2.4                       
│   ├── absl-py>=0.4                       
│   │   └── six                            
│   ├── google-auth-oauthlib<0.5,>=0.4.1   
│   │   ├── google-auth>=1.0.0             
│   │   │   ├── cachetools<5.0,>=2.0.0     
│   │   │   ├── pyasn1-modules>=0.2.1      
│   │   │   │   └── pyasn1<0.5.0,>=0.4.6   
│   │   │   ├── rsa<5,>=3.1.4              
│   │   │   │   └── pyasn1>=0.1.3          
│   │   │   ├── setuptools>=40.3.0         
│   │   │   └── six>=1.9.0                 
│   │   └── requests-oauthlib>=0.7.0       
│   │       ├── oauthlib>=3.0.0            
│   │       └── requests>=2.0.0            
│   │           ├── certifi>=2017.4.17     
│   │           ├── chardet<5,>=3.0.2      
│   │           ├── idna<3,>=2.5           
│   │           └── urllib3<1.27,>=1.21.1  
│   ├── google-auth<2,>=1.6.3              
│   │   ├── cachetools<5.0,>=2.0.0         
│   │   ├── pyasn1-modules>=0.2.1          
│   │   │   └── pyasn1<0.5.0,>=0.4.6       
│   │   ├── rsa<5,>=3.1.4                  
│   │   │   └── pyasn1>=0.1.3              
│   │   ├── setuptools>=40.3.0             
│   │   └── six>=1.9.0                     
│   ├── grpcio>=1.24.3                     
│   │   └── six>=1.5.2                     
│   ├── markdown>=2.6.8                    
│   ├── numpy>=1.12.0                      
│   ├── protobuf>=3.6.0                    
│   │   └── six>=1.9                       
│   ├── requests<3,>=2.21.0                
│   │   ├── certifi>=2017.4.17             
│   │   ├── chardet<5,>=3.0.2              
│   │   ├── idna<3,>=2.5                   
│   │   └── urllib3<1.27,>=1.21.1          
│   ├── setuptools>=41.0.0                 
│   ├── six>=1.10.0                        
│   ├── tensorboard-plugin-wit>=1.6.0      
│   ├── werkzeug>=0.11.15                  
│   └── wheel>=0.26                        
├── tensorflow-estimator<2.5.0,>=2.4.0    
├── termcolor~=1.1.0                       
├── typing-extensions~=3.7.4               
├── wheel~=0.35                            
└── wrapt~=1.12.1

Update Tensorflow and Keras Using Pip

If you already have TensorFlow and Keras installed, they can be updated by running the following command:

pip install -U tensorflow

You can verify the TensorFlow installation with the following command:

python -m pip show tensorflow

Output should be similar to: 

Name: tensorflow
Version: 2.2.0
Summary: TensorFlow is an open source machine learning framework for everyone.
Home-page: https://www.tensorflow.org/
Author: Google Inc.
Author-email: packages@tensorflow.org
License: Apache 2.0
Location: c:\python38\lib\site-packages
Requires: google-pasta, gast, six, protobuf, tensorboard, h5py, termcolor, absl-py, opt-einsum, wrapt, grpcio, keras-preprocessing, tensorflow-estimator, numpy, astunparse, wheel, scipy
Required-by:

If you intend to create plots based on TensorFlow and Keras data, then consider installing Matplotlib. For information about Matplotlib and how to install it, refer to What is Matplotlib in Python?

How to Import Keras and TensorFlow

Once TensorFlow and Keras are installed, you can start working with them. 

# Begin a Keras script by importing the Keras library:
import keras

or 

from tensorflow import keras 
# Import TensorFlow:
import tensorflow as tf

It’s not necessary to import all of the Keras and Tensorflow library functions. Instead, import just the function(s) you need for your project.

# Import the Sequential model class from Keras
# to form the framework for a Sequential neural network:
from keras.models import Sequential

For more information on working with Keras, refer to What is a Keras Model. <– link needed

Get a version of Python, pre-compiled with Keras and other popular ML Packages

ActiveState Python is the trusted Python distribution for Windows, Linux and Mac, pre-bundled with top Python packages for machine learning – free for development use.

Some Popular ML Packages You Get Pre-compiled – With ActiveState Python

Machine Learning:

  • TensorFlow (deep learning with neural networks)*
  • scikit-learn (machine learning algorithms)
  • keras (high-level neural networks API)

Data Science:

  • pandas (data analysis)
  • NumPy (multidimensional arrays)
  • SciPy (algorithms to use with numpy)
  • HDF5 (store & manipulate data)
  • matplotlib (data visualization)

Get ActiveState Python for Machine Learning for Windows, macOS or Linux here.

Why use ActiveState Python instead of open source Python?

While the open source distribution of Python may be satisfactory for an individual, it doesn’t always meet the support, security, or platform requirements of large organizations.

This is why organizations choose ActiveState Python for their data science, big data processing and statistical analysis needs.

Pre-bundled with the most important packages Data Scientists need, ActiveState Python is pre-compiled so you and your team don’t have to waste time configuring the open source distribution. You can focus on what’s important–spending more time building algorithms and predictive models against your big data sources, and less time on system configuration.

ActiveState Python is 100% compatible with the open source Python distribution and provides the security and commercial support that your organization requires.

With ActiveState Python you can explore and manipulate data, run statistical analysis, and deliver visualizations to share insights with your business users and executives sooner–no matter where your data lives.

Download ActiveState Python to get started or contact us to learn more about using ActiveState Python in your organization.

 

Recent Posts

Scroll to Top