The first contact with Keras/Tensorflow

Alex Barros
3 min readOct 20, 2021

--

What is Keras and how it can be used within TensorFlow to build Machine Learning Models

In this article we are going to get our first contact to Keras, It is a library from Tensorflow to build neural networks. Let's begin learning about the history of Keras and how it is related to TensorFlow.

Keras from the begining

Keras (κέρας) means horn in Greek. It is a reference to a literary image from ancient Greek and Latin literature, first found in the Odyssey, where dream spirits (Oneiroi, singular Oneiros) are divided between those who deceive dreamers with false visions, who arrive to Earth through a gate of ivory, and those who announce a future that will come to pass, who arrive through a gate of horn.

Keras was initially developed as part of the research effort of project ONEIROS (Open-ended Neuro-Electronic Intelligent Robot Operating

Keras was developed with focus on enabling fast user experimentation.
The idea is to enable the implementation of some project with very few steps.

Keras is integrated with TensorFlow.
Historically, Keras was a high level neural network API that you could configure to run against one of three separate lower level API’s.

And those lower level API’s were TensorFlow, Theano and CNTK.

Later, though, Keras became fully integrated with TensorFlow API, and we can install Keras by just installing TensorFlow and Keras will come completely packaged with TensorFlow installation.

So the installation procedure is as simple as running pip install TensorFlow from your command line.

According to developers, Keras is:

  • Simple — but not simplistic. Keras reduces developer cognitive load to free you to focus on the parts of the problem that really matter.
  • Flexible — Keras adopts the principle of progressive disclosure of complexity: simple workflows should be quick and easy, while arbitrarily advanced workflows should be possible via a clear path that builds upon what you’ve already learned.
  • Powerful — Keras provides industry-strength performance and scalability: it is used by organizations and companies including NASA, YouTube, or Waymo.

First contact with Keras

The core data structures of Keras are layers and models. The simplest type of model is the Sequential model, a linear stack of layers. For more complex architectures, you should use the Keras functional API, which allows to build arbitrary graphs of layers, or write models entirely from scratch via subclasssing.

Here is the Sequential model:

from tensorflow.keras.models import Sequentialmodel = Sequential()

Stacking layers is as easy as .add():

from tensorflow.keras.layers import Densemodel.add(Dense(units=64, activation='relu'))
model.add(Dense(units=10, activation='softmax'))

Once your model looks good, configure its learning process with .compile():

model.compile(loss='categorical_crossentropy',
optimizer='sgd',
metrics=['accuracy'])

If you need to, you can further configure your optimizer. The Keras philosophy is to keep simple things simple, while allowing the user to be fully in control when they need to (the ultimate control being the easy extensibility of the source code via subclassing).

model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.SGD(learning_rate=0.01, momentum=0.9, nesterov=True))

You can now iterate on your training data in batches:

# x_train and y_train are Numpy arrays
model.fit(x_train, y_train, epochs=5, batch_size=32)

Evaluate your test loss and metrics in one line:

loss_and_metrics = model.evaluate(x_test, y_test, batch_size=128)

Or generate predictions on new data:

classes = model.predict(x_test, batch_size=128)

What you just saw is the most elementary way to use Keras.

However, Keras is also a highly-flexible framework suitable to iterate on state-of-the-art research ideas. Keras follows the principle of progressive disclosure of complexity: it makes it easy to get started, yet it makes it possible to handle arbitrarily advanced use cases, only requiring incremental learning at each step.

Follown me to learn more about Keras and Data Science.

References:

--

--

Alex Barros
Alex Barros

Written by Alex Barros

Engenheiro da Computação. Mestre e Doutorando em Computação Aplicada. Coordenador do Escritório de Projetos e Processos no TRT8.

No responses yet