Language: EN

machine-learning-con-tensorflow-y-keras-en-python

Machine learning with TensorFlow and Keras in Python

Today we are going to enter the world of Machine Learning with TensorFlow and Keras, two Open Source libraries that allow us to delve into Deep Learning in a simple way.

As we know, learning systems are trendy. Although there is “a lot of smoke” around, the truth is that there are more than enough reasons for its boom and interest.

Learning systems have a huge field of application that ranges from mathematical analysis, making predictions, Business Intelligence, vision systems, autonomous vehicles, robotics, and even medical applications.

Until just a few years ago, learning systems were complex, slow, and required a lot of programming even for a simple neural network.

But, progressively and driven by the rise of AI and Big Data, libraries like TensorFlow or Keras have appeared that make the process simpler and improve the speed of the systems.

Currently, anyone can run and practice with learning systems on a conventional computer, and even on a mini computer like Raspberry Pi.

So, we are going to dedicate this post to present these two Machine Learning tools that we will use in future tutorials on the blog.

What is TensorFlow?

Among the many available libraries, the undisputed queen is TensorFlow, which has become the most popular library in Deep Learning. Currently, it would be difficult to imagine tackling a learning project without it.

TensorFlow is a library developed by Google Brain for its machine learning applications and deep neural networks, released as open-source software on November 9, 2015.

TensorFlow is a library for mathematical computation, which executes graphs of flow quickly and efficiently. A flow graph is formed by mathematical operations represented on nodes, and whose input and output is a multidimensional vector (or tensor) of data.

The name of the library comes precisely from the combination of “tensor” and “flow”

You can find a lot more information and tutorials on its page https://www.tensorflow.org and the code is available at https://github.com/tensorflow/tensorflow.

What is Keras?

On the other hand, Keras is a Python library for neural networks. It has been mainly developed by François Chollet, a Google engineer, and released as open-source software.

Keras is an abstraction, a High-level API, for creating learning models. It provides a homogeneous syntax and a simple, modular, and expandable interface for creating neural networks.

Neural networks are a particular type of data flow graph. Therefore, TensorFlow and Keras combine perfectly making a tandem that combines power, ease of use, and speed of execution. Keras can also run in combination with other “base” frameworks, such as Microsoft Cognitive Toolkit or Theano.

The project’s website is https://keras.io/, and the source code is available at https://github.com/keras-team/keras/.

Installing TensorFlow and Keras

Installing TensorFlow and Keras is very simple, we just need to have Python previously installed on our computer.

To install TensorFlow, from a terminal, we run the following command,

pip install tensorflow

Or if we want TensorFlow with GPU support the following command,

pip install tensorflow_gpu

To install Keras we simply use the command,

pip install keras

At the time of writing this post, the latest supported version of Python is 3.6

Additional Libraries

Other additional libraries that we will frequently use in our learning systems projects are

  • pathlib: Managing file paths
  • numpy: Mathematical library.
  • scipi: Mathematical library.
  • pandas: Data analysis and statistics
  • matplotlib: Creating graphs
  • seaborn: Statistical graphs

We can install them, respectively, by doing

pip install pathlib
pip install numpy
pip install scipi
pip install matplotlib
pip install pandas
pip install seaborn

Hello World in TensorFlow

To verify that everything works correctly, we are going to make a simple application with TensorFlow. We create a Python file (for example, TF_HelloWork.py) and copy the following content.

import tensorflow as tf

hello = tf.constant('Hello world!')

a = tf.constant(2)
b = tf.constant(3)

with tf.Session() as sess:
    print(sess.run(hello))
    print("A+B = %i" % sess.run(a+b))
    print("A*B = %i" % sess.run(a*b))

When executed, if everything has gone well, we will see the following output.

b'Hello world!'
A+B = 5
A*B = 6

So far this presentation of TensorFlow and Keras, two libraries that allow us to develop Machine Learning projects in a simple way.

In the future, we will see more tutorials on these two tools, such as regression, classification, prediction, and computer vision. See you soon!