This chapter explains about how to install Keras on your machine. Before moving to installation, let us go through the basic requirements of Keras.
Prerequisites
You must satisfy the following requirements −
- Any kind of OS (Windows, Linux or Mac)
- Python version 3.5 or higher.
Python
Keras is python based neural network library so python must be installed on your machine. If python is properly installed on your machine, then open your terminal and type python, you could see the response similar as specified below,
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>
As of now the latest version is ‘3.7.2’. If Python is not installed, then visit the official python link – and download the latest version based on your OS and install it immediately on your system.
Keras Installation Steps
Keras installation is quite easy. Follow below steps to properly install Keras on your system.
Step 1: Create virtual environment
Virtualenv is used to manage Python packages for different projects. This will be helpful to avoid breaking the packages installed in the other environments. So, it is always recommended to use a virtual environment while developing Python applications.
Linux/Mac OS
Linux or mac OS users, go to your project root directory and type the below command to create virtual environment,
python3 -m venv kerasenv
After executing the above command, “kerasenv” directory is created with bin,lib and include folders in your installation location.
Windows
Windows user can use the below command,
py -m venv keras
Step 2: Activate the environment
This step will configure python and pip executables in your shell path.
Linux/Mac OS
Now we have created a virtual environment named “kerasvenv”. Move to the folder and type the below command,
$ cd kerasvenv kerasvenv $ source bin/activate
Windows
Windows users move inside the “kerasenv” folder and type the below command,
.envScriptsactivate
Step 3: Python libraries
Keras depends on the following python libraries.
- Numpy
- Pandas
- Scikit-learn
- Matplotlib
- Scipy
- Seaborn
Hopefully, you have installed all the above libraries on your system. If these libraries are not installed, then use the below command to install one by one.
numpy
pip install numpy
you could see the following response,
Collecting numpy Downloading https://files.pythonhosted.org/packages/cf/a4/d5387a74204542a60ad1baa84cd2d3353c330e59be8cf2d47c0b11d3cde8/ numpy-3.1.1-cp36-cp36m-macosx_10_6_intel. macosx_10_9_intel.macosx_10_9_x86_64. macosx_10_10_intel.macosx_10_10_x86_64.whl (14.4MB) |████████████████████████████████| 14.4MB 2.8MB/s
pandas
pip install pandas
We could see the following response,
Collecting pandas Downloading https://files.pythonhosted.org/packages/cf/a4/d5387a74204542a60ad1baa84cd2d3353c330e59be8cf2d47c0b11d3cde8/ pandas-3.1.1-cp36-cp36m-macosx_10_6_intel. macosx_10_9_intel.macosx_10_9_x86_64. macosx_10_10_intel.macosx_10_10_x86_64.whl (14.4MB) |████████████████████████████████| 14.4MB 2.8MB/s
matplotlib
pip install matplotlib
We could see the following response,
Collecting matplotlib Downloading https://files.pythonhosted.org/packages/cf/a4/d5387a74204542a60ad1baa84cd2d3353c330e59be8cf2d47c0b11d3cde8/ matplotlib-3.1.1-cp36-cp36m-macosx_10_6_intel. macosx_10_9_intel.macosx_10_9_x86_64. macosx_10_10_intel.macosx_10_10_x86_64.whl (14.4MB) |████████████████████████████████| 14.4MB 2.8MB/s
scipy
pip install scipy
We could see the following response,
Collecting scipy Downloading https://files.pythonhosted.org/packages/cf/a4/d5387a74204542a60ad1baa84cd2d3353c330e59be8cf2d47c0b11d3cde8 /scipy-3.1.1-cp36-cp36m-macosx_10_6_intel. macosx_10_9_intel.macosx_10_9_x86_64. macosx_10_10_intel.macosx_10_10_x86_64.whl (14.4MB) |████████████████████████████████| 14.4MB 2.8MB/s
scikit-learn
It is an open source machine learning library. It is used for classification, regression and clustering algorithms. Before moving to the installation, it requires the following −
- Python version 3.5 or higher
- NumPy version 1.11.0 or higher
- SciPy version 0.17.0 or higher
- joblib 0.11 or higher.
Now, we install scikit-learn using the below command −
pip install -U scikit-learn
Seaborn
Seaborn is an amazing library that allows you to easily visualize your data. Use the below command to install −
pip install seaborn
You could see the message similar as specified below −
Collecting seaborn Downloading https://files.pythonhosted.org/packages/a8/76/220ba4420459d9c4c9c9587c6ce607bf56c25b3d3d2de62056efe482dadc /seaborn-0.9.0-py3-none-any.whl (208kB) 100% |████████████████████████████████| 215kB 4.0MB/s Requirement already satisfied: numpy> = 1.9.3 in ./lib/python3.7/site-packages (from seaborn) (1.17.0) Collecting pandas> = 0.15.2 (from seaborn) Downloading https://files.pythonhosted.org/packages/39/b7/441375a152f3f9929ff8bc2915218ff1a063a59d7137ae0546db616749f9/ pandas-0.25.0-cp37-cp37m-macosx_10_9_x86_64. macosx_10_10_x86_64.whl (10.1MB) 100% |████████████████████████████████| 10.1MB 1.8MB/s Requirement already satisfied: scipy>=0.14.0 in ./lib/python3.7/site-packages (from seaborn) (1.3.0) Collecting matplotlib> = 1.4.3 (from seaborn) Downloading https://files.pythonhosted.org/packages/c3/8b/af9e0984f 5c0df06d3fab0bf396eb09cbf05f8452de4e9502b182f59c33b/ matplotlib-3.1.1-cp37-cp37m-macosx_10_6_intel. macosx_10_9_intel.macosx_10_9_x86_64 .macosx_10_10_intel.macosx_10_10_x86_64.whl (14.4MB) 100% |████████████████████████████████| 14.4MB 1.4MB/s ...................................... ...................................... Successfully installed cycler-0.10.0 kiwisolver-1.1.0 matplotlib-3.1.1 pandas-0.25.0 pyparsing-2.4.2 python-dateutil-2.8.0 pytz-2019.2 seaborn-0.9.0
Keras Installation Using Python
As of now, we have completed basic requirements for the installtion of Kera. Now, install the Keras using same procedure as specified below −
pip install keras
Quit virtual environment
After finishing all your changes in your project, then simply run the below command to quit the environment −
deactivate
Anaconda Cloud
We believe that you have installed anaconda cloud on your machine. If anaconda is not installed, then visit the official link, and choose download based on your OS.
Create a new conda environment
Launch anaconda prompt, this will open base Anaconda environment. Let us create a new conda environment. This process is similar to virtualenv. Type the below command in your conda terminal −
conda create --name PythonCPU
If you want, you can create and install modules using GPU also. In this tutorial, we follow CPU instructions.
Activate conda environment
To activate the environment, use the below command −
activate PythonCPU
Install spyder
Spyder is an IDE for executing python applications. Let us install this IDE in our conda environment using the below command −
conda install spyder
Install python libraries
We have already known the python libraries numpy, pandas, etc., needed for keras. You can install all the modules by using the below syntax −
Syntax
conda install -c anaconda <module-name>
For example, you want to install pandas −
conda install -c anaconda pandas
Like the same method, try it yourself to install the remaining modules.
Install Keras
Now, everything looks good so you can start keras installation using the below command −
conda install -c anaconda keras
Launch spyder
Finally, launch spyder in your conda terminal using the below command −
spyder
To ensure everything was installed correctly, import all the modules, it will add everything and if anything went wrong, you will get module not found error message.