Python-based AI powered by TensorFlow and Keras
Artificial intelligence and machine learning are becoming increasingly in demand in today’s world. Many companies and startups are actively implementing artificial intelligence technologies to solve business problems and optimize processes.
Disclamer: this text is a translation of an article by the author MrDecentralized. The original text belongs to him.
One of the key tools for implementing neural network architectures and deep learning algorithms is the Python programming language. The availability of powerful libraries such as TensorFlow and Keras has made it quite easy to create and train neural networks in Python.
TensorFlow is a machine learning library developed by Google. It allows you to define, train and run neural networks of different architectures. Keras, a high-level deep learning library, can use TensorFlow as a backend. Keras simplifies the creation of neural network models thanks to its user-friendly API.
The purpose of this article is to introduce you to the basic principles of deep learning and the capabilities of TensorFlow and Keras libraries for creating and training neural networks in Python. We will cover basic concepts, model training steps, and practical cases of using TensorFlow and Keras to solve classification, recognition, and data analysis problems.
After studying this article, you will get an idea of how you can create effective artificial intelligence models using Python and the libraries discussed above

Basic Concepts
What are neural networks and deep learning?

Neural networks are mathematical models, which in their structure and principle of operation largely resemble networks of nerve cells in the brain. They consist of interconnected neurons — simple computational elements.
A neuron receives certain data as input, performs calculations on them according to its activation function, and transmits the result to the output, which is fed to the inputs of the following neurons.
This structure allows neural networks to work efficiently with unstructured data — images, text, sound. Neural networks are able to analyze input data and find complex dependencies and patterns in it.
Deep learning is a machine learning method based on artificial neural networks with a large number of layers. Due to their multilayer structure, such networks can build more complex patterns and find hidden dependencies in the data.
Neural networks consist of neurons grouped into layers. The first layer is called the input layer and the last layer is called the output layer. There are also one or more intermediate hidden layers.
Each neuron receives data from the neurons of the previous layer, processes it using an activation function and passes the result to the neurons of the next layer.
During training, the weights of connections between neurons are adjusted so as to minimize the network’s error in solving the task.


Reinforcement learning and supervised learning
Reinforcement learning is a method of training neural networks in which the model has to learn by itself in practice what actions lead to a positive result and what actions lead to a negative result.
The network interacts with the environment, selects and performs various actions, and receives immediate “reinforcement” in the form of rewards or penalties for these actions. The goal is to maximize the total reward received.
For example, this is how networks are trained for games, robot control, and trading strategies. The network itself explores all possible options and learns to choose optimal moves.
Supervised learning implies the presence of a training sample with correct answers. The network is trained to predict answers for new examples based on known data.
This method is used in classification, regression, and prediction tasks. The network receives examples of objects with “digits” (digits dataset) and learns to recognize these objects.
Using TensorFlow in Python
Installing and importing TensorFlow

To start using TensorFlow in Python, you need to install this library. This can be done using the pip package manager:

pip install tensorflow

TensorFlow can then be imported into Python code:

import tensorflow as tf

TensorFlow has a high-level API (tf.keras) and a low-level API. In this article, we will use the low-level API to better understand the working principles.

Let’s import the main components:

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

TensorFlow is now available for use in our Python code to create and train neural networks.

Creating and training a simple neural network
Let’s create and train a simple fully-connected neural network on TensorFlow to solve a classification problem.
First, we define the architecture — the sequence of layers. Let’s create 3 fully connected layers with 64, 32 and 10 neurons:

model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(64, activation='relu'))
model.add(tf.keras.layers.Dense(32, activation='relu'))
model.add(tf.keras.layers.Dense(10))


For training, we compile the model with parameters: loss function, optimizer and metrics:

model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])

Next, we train the network on the training data:

model.fit(train_images, train_labels, epochs=5)

Once trained, the network can be used to classify new images.


Using different layers and features
TensorFlow provides a large set of ready-made layers and functions for building neural networks:

  • Fully connected layers (Dense) — the basis for creating fully connected networks
  • Convolutional layers (Conv2D, Conv3D) — used in convolutional networks to work with images.
  • Pooling layers (MaxPooling2D) — used after convolutional layers for dimensionality reduction.
  • Recurrence layers (LSTM, GRU) — for RNNs and sequence processing.
  • Normalization layers (BatchNormalization) — for normalization of activations in the network
  • Activation functions (ReLU, LeakyReLU, Sigmoid, Softmax, etc.) — add nonlinearity.
  • Losses (Losses) — MSE, CrossEntropy, SparseCategoricalCrossentropy and others.
  • Optimizers — Adam, SGD, RMSprop etc. to update weights
By combining different layers, you can create neural networks of almost any architecture to solve a wide range of problems.



Visualization of neural network operation in TensorFlow
To better understand and debug neural networks, it is very useful to visualize their operation. TensorFlow has tools for visualization:

TensorBoard — allows you to plot losses, metrics, and weights of neurons during training:

tensorboard = TensorBoard(log_dir="logs")
model.fit(data, labels, epochs=10, callbacks=[tensorboard])

TensorBoard runs in a browser and displays graphs from training logs.
In addition, you can visualize the activations of specific neurons when processing input data with Keras:

layer = model.layers[2]
activations = layer.activations
import matplotlib.pyplot as plt
plt.imshow(activations[0][0,:,:], cmap='viridis')

This helps to understand what neurons in different layers of the network are responding to.
Visualization greatly simplifies debugging and optimization of neural networks in TensorFlow.
Using Keras in Python
Installing and importing Keras

Keras is a high-level neural network library for Python that can use TensorFlow as a backend. You can install Keras via pip:

pip install keras
Import
import keras
from keras import models
from keras import layers

Keras has a simple and straightforward API for rapid development of neural networks.
There are two main ways to create models in Keras:

  • Sequential — for linear layer stacks
  • Functional (Functional) — for arbitrary layer graphs
Let’s consider the use of each of them.
Creating a Sequential model in Keras

To create a simple linear model in Keras, the Sequential API is used:

model = keras.Sequential()
model.add(keras.layers.Dense(32, activation='relu'))
model.add(keras.layers.Dense(10, activation='softmax'))

Layers are simply added to the model one by one using the add() method. This is a convenient way to quickly create a fully connected or convolutional network.

The model is then compiled and trained using standard methods:

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x_train, y_train, batch_size=64, epochs=5)

Thus, using the simple Sequential API in Keras, you can define and train neural networks with just a few lines of code.

Creating a Functional model in Keras

To create more complex network architectures, the Functional API is used in Keras:

inputs = keras.Input(shape=(32,))
x = layers.Dense(64, activation='relu')(inputs)
x = layers.Dense(64, activation='relu')(x)
outputs = layers.Dense(10, activation='softmax')(x)
model = keras.Model(inputs=inputs, outputs=outputs)

Here we define individual layers and then link them into a single model by specifying input and output tensors.
This allows us to create branching networks, residual links, multi-input and multi-output models.

For example, you can combine two different Sequential models into one large Functional model.

Such an API gives the flexibility to create neural network architectures of arbitrary complexity in Keras.


Advantages of Keras as a “wrapper” over TensorFlow

Keras has a number of advantages over TensorFlow:

  • A simpler and more intuitive API for model development
  • Less code to solve typical problems
  • Built-in functions for training, optimization, model evaluation
  • Ability to prototype and iterate quickly
  • Compatible with TensorFlow as a low-level backend
Yet Keras retains the flexibility of TensorFlow to develop complex architectures.

Examples of training different types of neural networks in Keras:

  • Fully connected for text or number classification
  • Convergent for image processing
  • Recurrent (LSTM, GRU) for text and time series
  • Autoencoders for dimensionality reduction
  • GAN-based networks for content generation
Keras makes it easy to implement, train, and apply these types of architectures to solve practical problems.
Practical Applications
Solving an image classification problem with TensorFlow

Let’s consider the application of TensorFlow to solve a classic computer vision problem — image classification.
As data, let’s take one of the popular datasets — MNIST, containing 70 000 images of handwritten digits 0–9.
Let’s build a simple convolutional neural network in TensorFlow:

  • A convolution layer for feature extraction
  • Pooling layer for dimensionality reduction
  • Full-link layer for classification

model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(28, 28, 1)))
model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(10, activation='softmax'))

Let’s train the model on MNIST data:

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=5, batch_size=64)

Now the network is ready to classify handwritten digits with quite high accuracy!

Image classification is one of the most common examples of applying neural networks in practice with TensorFlow.

Time series analysis with RNNs
Recurrent neural networks (RNNs) are well suited for analyzing sequential data such as time series.
Let’s look at the application of RNNs on TensorFlow for time series forecasting.
Let’s build a simple RNN using lstm layers:

model = tf.keras.Sequential()
model.add(tf.keras.layers.LSTM(128, input_shape=(None, 1)))
model.add(tf.keras.layers.Dense(1))
model.compile(loss='mae', optimizer='adam')

Train the network on stock price data for several years to predict the price one month from now:

model.fit(X_train, y_train, epochs=10)

Now we use trained RNN to predict the price for a month based on previous data:

prediction = model.predict(X_test)

LSTM networks are well suited for many time series analysis tasks: forecasting, anomaly detection, classification, etc.
Conclusion
Summary of deep learning capabilities in Python with TensorFlow

In this article, we have reviewed the main features of the TensorFlow library for creating and training neural networks using Python.
TensorFlow provides flexible tools for working with different types of networks and architectures: fully connected, convolutional, recurrent. This allows solving a wide range of problems: image classification, text and speech processing, time series analysis, forecasting, etc.
Thanks to high-level and low-level APIs, you can both rapidly prototype models and fully control the architecture and training of networks.
TensorFlow’s visualization capabilities simplify debugging, monitoring, and optimization of neural networks.

Recommendations for using TensorFlow and Keras
TensorFlow and Keras are great choices for creating machine learning and deep neural network projects in Python. Here are some recommendations for using these tools:
  • It’s best to start with Keras as a high-level and easy to learn API. Create models with Sequential and Functional APIs.
  • Gradually learn the low-level capabilities of TensorFlow to extend flexibility and control in network design.
  • Use pre-learned models (VGG, ResNet, Inception, etc.) as a base or for transfer learning when solving practical problems.
  • Apply different architectures: convolutional networks for images, RNNs and LSTMs for sequential data.
  • Use auxiliary techniques: dropout, batch normalization to train large networks.
  • Visualize the process of training networks using TensorBoard.
  • Partition data into training, validation, and test sets for proper model evaluation.
  • Select hyperparameters (learning rate, batches size, epochs) to optimize training.
Following these recommendations, you will be able to effectively apply TensorFlow and Keras to solve practical problems of machine learning and neural network construction.