Tensorflow functional API: creating a CNN

Contents

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 learning.

  • 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 convolucional. 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 neuronal convolucional para la clasificación de imágenes. The database 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 training 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 parameters 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 variable representa la necesidad de planificar y diseñar una input layer 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 function 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.

83195b-5748467
52104a-9279201

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.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.

Datapeaker