This article was published as part of the Data Science Blogathon
Introduction
In today's article, I will talk about developing a convolutional neural network that uses the functional TensorFlow API. Will dispense functional API capability, allowing us to produce a hybrid model architecture that exceeds the capacity of a primary sequential model.
About: TensorFlow
TensorFlow is a popular library, something you probably hear perpetually in the Deep Learning and Artificial Intelligence society. Existen numerosos paquetes y proyectos de código abierto para el deep learningDeep learning, A subdiscipline of artificial intelligence, relies on artificial neural networks to analyze and process large volumes of data. This technique allows machines to learn patterns and perform complex tasks, such as speech recognition and computer vision. Its ability to continuously improve as more data is provided to it makes it a key tool in various industries, from health....
- TensorFlow, an open source artificial intelligence library that manages data flow charts, is the most common deep learning library. It is used to generate large-scale neural networks with countless layers.
- TensorFlow is practiced for deep learning or machine learning situations like Classification, Perception, Perception, Discovery, Forecast and Production.
Then, when we interpret a classification problem, aplicamos un modelo de red neuronal convolucionalConvolutional Neural Networks (CNN) are a type of neural network architecture designed especially for data processing with a grid structure, as pictures. They use convolution layers to extract hierarchical features, which makes them especially effective in pattern recognition and classification tasks. Thanks to its ability to learn from large volumes of data, CNNs have revolutionized fields such as computer vision... Even so, most developers were familiar with sequential model modeling. The layers are accompanied one by one.
- Sequential API lets you design layer-by-layer models for the most important problems.
- Difficulty is restricted because it does not allow you to produce models that share layers or have inputs or outputs added.
- Because of this, we can practice Tensorflows functional API as multiple output model.
Functional API (tf.Hard)
The functional API in tf.Hard is an alternative way to build more flexible models, including the formulation of a more complex model.
- For instance, when implementing a negligibly more complicated example with machine learning, you may rarely be faced with the state where you demand additional models for the same data.
- Then we would need to produce two outputs. The most manageable option would be to build two separate models based on the corresponding data to make predictions..
- This would be smooth, but what if, in the current scenario, we had to have 50 results? It could be a hassle to keep all those models separate.
- Alternatively, it is more fruitful to build a single model with better results.
In open API method, models are determined by forming layers and correlating them directly to each other in sets, then a Model is established that defines the layers to function as input and output.
What is different in the Sequential API?
Sequential API allows you to generate models layer by layer for most major queries. It is regulated because it does not allow you to design models that share layers or have inputs or outputs added.
Let's understand how to create a sequential API model object below:
model = tf.keras.models.Sequential([ tf.keras.layers.Flatten(input_shape=(28, 28)), tf.hard.layers.Dense(128, activation = "proofread"), tf.keras.layers.Dropout(0.2), tf.hard.layers.Dense(10, activation=’softmax’) ])
- In functional API, you can design models that produce much more versatility. Undoubtedly, can correct models in which layers relate to more than before and after layers.
- Can combine layers with multiple other layers. Due, the production of heterogeneous networks such as Siamese networks and residual networks becomes feasible.
Let's start developing a CNN model by practicing a functional API
In this post, utilizamos el conjunto de datos MNIST para construir la red neuronalNeural networks are computational models inspired by the functioning of the human brain. They use structures known as artificial neurons to process and learn from data. These networks are fundamental in the field of artificial intelligence, enabling significant advancements in tasks such as image recognition, Natural Language Processing and Time Series Prediction, among others. Their ability to learn complex patterns makes them powerful tools.. convolucional para la clasificación de imágenes. The databaseA database is an organized set of information that allows you to store, Manage and retrieve data efficiently. Used in various applications, from enterprise systems to online platforms, Databases can be relational or non-relational. Proper design is critical to optimizing performance and ensuring information integrity, thus facilitating informed decision-making in different contexts.... del MNIST comprende 60,000 training images and 10,000 test images obtained from US Census Bureau workers and US high school juniors.
# import libraries
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Dense, Dropout, Input
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten
from tensorflow.keras.models import Model
from tensorflow.keras.datasets import mnist
# load data
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# convert sparse label to categorical values
num_labels = len(np.unique(y_train))
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
# preprocess the input images
image_size = x_train.shape[1]
x_train = np.reshape(x_train,[-1, image_size, image_size, 1])
x_test = np.reshape(x_test,[-1, image_size, image_size, 1])
x_train = x_train.astype('float32') / 255
x_test = x_test.astype('float32') / 255
In the code above,
- Distribuí estos dos grupos como trainingTraining is a systematic process designed to improve skills, physical knowledge or abilities. It is applied in various areas, like sport, Education and professional development. An effective training program includes goal planning, regular practice and evaluation of progress. Adaptation to individual needs and motivation are key factors in achieving successful and sustainable results in any discipline.... y prueba y distribuí las etiquetas y las entradas.
- The independent variables (x_train y x_test) contain grayscale RGB codes of 0 a 255, while the dependent variables (y_train e y_test) carry labels of 0 a 9, that describe what number they really are.
- It is good practice to normalize our data, as it is constantly required in deep learning models. We can achieve this by dividing the RGB codes by 255.
Then, inicializamos los parametersThe "parameters" are variables or criteria that are used to define, measure or evaluate a phenomenon or system. In various fields such as statistics, Computer Science and Scientific Research, Parameters are critical to establishing norms and standards that guide data analysis and interpretation. Their proper selection and handling are crucial to obtain accurate and relevant results in any study or project.... para las redes.
# parameters for the network input_shape = (image_size, image_size, 1) batch_size = 128 kernel_size = 3 filters = 64 dropout = 0.3
In the code above,
- input_shape: The variableIn statistics and mathematics, a "variable" is a symbol that represents a value that can change or vary. There are different types of variables, and qualitative, that describe non-numerical characteristics, and quantitative, representing numerical quantities. Variables are fundamental in experiments and studies, since they allow the analysis of relationships and patterns between different elements, facilitating the understanding of complex phenomena.... representa la necesidad de planificar y diseñar una input layerThe "input layer" refers to the initial level in a data analysis process or in neural network architectures. Its main function is to receive and process raw information before it is transformed by subsequent layers. In the context of machine learning, Proper configuration of the input layer is crucial to ensure the effectiveness of the model and optimize its performance in specific tasks.... independiente que designe los datos de entrada. The input layer accepts an argument so that it is a tuple that describes the dimensions of the input data.
- Lot Size: is a hyperparameter that determines the number of samples to run before updating the internal parameters of the model.
- kernel_size: relates to dimensions (height x width) filter mask. Convolutional Neural Networks (CNN) they are essentially a stack of layers marked by the operations of various filters on the input. These filters are commonly called cores..
- filter: is expressed by a vector of weights between which we convolve the input.
- Leave: is a process in which randomly selected neurons are neglected during training. This implies that their participation in the activation of downstream neurons is temporarily ruled out in the frontal pass..
Let's define a simplistic multilayer perceptron, a convolutional neural network:
# utiliaing functional API to build cnn layers inputs = Input(shape=input_shape) y = Conv2D(filters=filters, kernel_size=kernel_size, activation='relu')(inputs) y = MaxPooling2D()(Y) y = Conv2D(filters=filters, kernel_size=kernel_size, activation='relu')(Y) y = MaxPooling2D()(Y) y = Conv2D(filters=filters, kernel_size=kernel_size, activation='relu')(Y) # convert image to vector y = Flatten()(Y) # dropout regularization y = Dropout(dropout)(Y) outputs = Dense(num_labels, activation='softmax')(Y) # model building by supplying inputs/outputs model = Model(inputs=inputs, outputs=outputs)
In the code above,
- We specify a multilayer perceptron model towards binary classification.
- The model contains an input layer, 3 hidden layers next to 64 neurons and a product layer with 1 Exit.
- Rectified linear trigger functions apply to all hidden layers, y se adopta una wake functionThe activation function is a key component in neural networks, since it determines the output of a neuron based on its input. Its main purpose is to introduce nonlinearities into the model, allowing you to learn complex patterns in data. There are various activation functions, like the sigmoid, ReLU and tanh, each with particular characteristics that affect the performance of the model in different applications.... softmax en la capa de producto para la clasificación binaria.
- And you can see that the layers in the model are pairwise correlated. This is accomplished by stipulating where the input comes from while determining each new layer.
- As with all sequential APIs, the model is the information that we can summarize, adjust, evaluate and apply to execute predictions.
TensorFlow introduces a model class that you can practice to generate a model from your developed layers. Requires that you only define the input and output layers, mapping the structure and graph of the network architecture model.


Finally, we train the model.
model.compile(loss="categorical_crossentropy",
optimizer="adam",
metrics=['accuracy'])
model.fit(x_train,
y_train,
validation_data=(x_test, y_test),
epochs=20,
batch_size=batch_size)
# accuracy evaluation
score = model.evaluate(x_test,
y_test,
batch_size=batch_size,
verbose=0)
print("nTest accuracy: %.1f%%" % (100.0 * score[1]))
We have now successfully developed a convolutional neural network to distinguish handwritten digits with the functional Tensorflow API. We have obtained a precision superior to 99% and we can save the model and design a digit classification web application.
References:
- https://www.tensorflow.org/guide/keras/functional
- https://machinelearningmastery.com/keras-functional-api-deep-learning/
The media shown in this sign language recognition article is not the property of DataPeaker and is used at the author's discretion.



