Cyber Deals Start Now: 50% Off Dedicated*Shop Now >
Cyber Deals Start Now: Up to 50% Off VPS* ††† Shop Now >
Limited Inventory: High-Performance AMD-Powered Servers Now Available.* Shop Now >

How to Install Keras Using Python and TensorFlow

Reading Time: 4 minutes

What is Keras?

Keras is a tool for machine learning specialists who work with Python, mostly used due to the convenience of mathematical calculations. Developers use Keras to create, configure, and test machine learning and artificial intelligence systems, primarily neural networks.

What are Keras Models?

Keras works with models or schemes by which information is distributed and transformed. Machine learning is essentially processing information using a programmed network, where certain conclusions are drawn based on certain data. The network structure is called a model and is often presented as a graph, diagram, or table.

Requirements

  • A server with root-level access.
  • This tutorial is performed with CentOS 7.
  • Python installed.
  • TensorFlow installed.

Installing Python on CentOS

Step 1:

Log in to your CentOS system as a root user or a user with sudo privileges.

# sudo su -

You can also log in via secure shell (SSH) using the following command. Replace username with your username and serveripaddress with your server’s IP address.

# ssh username@serveripaddress

Step 2:

Update the system packages to avoid errors. This tutorial uses the Yum package manager.

# yum update -y

Step 3:

CentOS SCL is a tool that allows you to install updated versions of the same software. Install CentOS SCL using this command.

# sudo yum install centos-release-scl

Python is typically installed by default on most systems. To check, verify the version using the following command.

# python --version

You probably see a Python version 2.X.X. Keep this installed on your machine as other software may use this particular Python version.

Step 4:

Tensorflow and Keras require a python version of at least 3.7. Use the following command to install Python 3.8.

# sudo yum install rh-python38

After installing Python 3.8, assign this version as the standard version.

# scl enable rh-python38 bash

Step5:

Recheck the version to ensure 3.8 is the standard version.

# python --version

Installing TensorFlow

Step1:

You need to create a virtual environment when installing TensorFlow. First, create a folder for your project using the following commands.

# mkdir test
# cd test

Use the following command to create a virtual environment with python. It creates a virtual environment named tf-virtual-env. Replace this with your chosen name.

# python3 -m venv tf-virtual-env

Inside this environment are many pre-installed Python libraries and tools needed in the project, such as Package installer for Python (pip). The following command activates the environment.

# source tf-virtual-env/bin/activate

The prompt changes in the terminal, which means you activated it successfully.

(tf-virtual-env) # _

Type deactivate at the prompt and press Enter to exit the environment.

TensorFlow requires at least version 19.0 of pip. Use the following command to ensure you have the latest version.

# pip install --upgrade pip

Step 2:

This tutorial installs a version that does not use your GPU. You can install a TensorFlow version that offers GPU support. Use the following command to install TensorFlow without GPU support.

# pip install --upgrade tensorflow

Use the same command for updating TensorFlow.

Step 3:

To test your environment, open Python bash.

# python

Then, type the following command. The first line will import the TensorFlow packages into the Python interpreter session, while the second line will print the TensorFlow version.

import tensorflow as tf

 print(tf.__version__)'

Install Keras

Thanks to a new update in TensorFlow 2.0+, if you installed TensorFlow as instructed, you don’t need to install Keras anymore because it is installed with TensorFlow.

To confirm it, open Python bash.

# python

At the prompt, run the following commands.

import keras

keras.__version__

For those using TensorFlow versions before 2.0, here are the instructions for installing Keras using pip.

Pip Install

Before beginning, update pip.

# pip install --upgrade pip

Then, install Keras with the following command.

# pip install keras

Deprecation of the Git Clone Keras Install Method

Keras was previously installed by cloning the GitHub repository, unpacking the packages, and installing the software. The Keras team deprecated the GitHub repository and moved the applications into the core Keras repository and the TensorFlow pip package.

The recommended Keras installation method from the Keras team is via TensorFlow version 2+.

Importing Keras Libraries

When we create a python virtual environment, it already contains most of the important libraries. Here is the list of the most important Python libraries:

  • Numpy.
  • Pandas.
  • Scikit-learn.
  • Matplotlib.
  • Scipy.
  • Seabord.

To confirm the Numpy library, use the following command.

import numpy as np

ndarray = np.ones([3, 3])

print("TensorFlow operations convert numpy arrays to Tensors automatically")
tensor = tf.math.multiply(ndarray, 42)
print(tensor)

The output should be as follows.

tf.Tensor( [[42. 42. 42.] 
     [42. 42. 42.]
     [42. 42. 42.]], shape=(3, 3), dtype=float64)

Updating Keras

Since Keras installs alongside Tensorflow, they also update together. Use the command below, the same one to update TensorFlow, to update Keras.

# pip install --upgrade tensorflow

Wrapping Up

There are ways to install Karas and Tensorflow without a virtual environment. Still, it can be risky and more difficult than the commands of pre-configured environments. Whether installing Keras using pip via Python or TensorFlow, this tutorial helps you get it up and running for your next deep learning project.

Do you need a CentOS or AlmaLinux machine for your next Keras install? Liquid Web these options for VPS Hosting, Cloud Dedicated Servers, and Dedicated Servers. Contact our sales team today for setup options.

Avatar for Zachary Armstrong

About the Author: Zachary Armstrong

Zachary is a writer who specializes in breaking down complex subjects and making them easy to understand. He has a passion for technology, believes it can change the world for the better, and wants to tell the whole world about it.

Latest Articles

PHP 7 vs PHP 8: A Comparison

Read Article

How to Create a Symbolic Link

Read Article

What Is A Private VPS Parent?

Read Article

Managed Hosting vs Unmanaged VPS Hosting

Read Article

Why Is Your IP Address Blocked?

Read Article