Create a Neural Network with Python

What is a Neural Network?

A red neuronal is a mathematical and computational model that is inspired by the neural networks of the human brain.

Artificial neural networks have been developed in the field of artificial intelligence and have been used in many tasks, such as pattern recognition and data classification.

How a Neural Network Works

A neural network is made up of processing units calls neurons.

Each neuron is connected to other neurons by links that they transmit signs between them.

Neurons and links have pesos associated with them, used to modify the signal that is transmitted between neurons.

How a Neural Network is Learned

The learning in a neural network it is done by adjusting the weights of the links between the neurons.

This adjustment is made using the algorithm de backpropagation, which modifies the weights of the links in such a way as to minimize errors in the network outputs.

How to Implement a Neural Network in Python

There are many software libraries that can be used to implement neural networks in Python..

Some of the most popular are TensorFlow, Keras y PyTorch.

in this walkthrough, let's use the library Keras with the backend of TensorFlow.

Paso 1: Import the Necessary Libraries

First, we need to import the necessary libraries for this tutorial.

Let's import the library Keras and some of the functions TensorFlow what will we need.

”’

from keras.models import Sequential
from keras.layers import Dense
import tensorflow as tf

”’

Paso 2: Define the Neural Network

In this step, let's define the model of our neural network.

We are going to use a red neuronal fully-connected, which is made up of dense layers of neurons connected between all the neurons of the previous layer.

The first step is to create a sequence object. This object is used to define the structure of the neural network.

”’

model = Sequential()

”’

Next, we add the first dense layer of neurons to our model.

This layer will 100 neurons, and the input layer will have 784 neurons (one for each pixel of an MNIST image).

The function Turn It is used to create a dense layer of neurons.

”’

model.add(Turn(100, input_dim=784, activation='relu'))

”’

Next, We add a second dense layer of neurons.

This layer will 50 neurons.

”’

model.add(Turn(50, activation='relu'))

”’

At last, Add the output layer.

This layer will 10 neurons, one for each digit of the MNIST dataset.

The activation function that we will use in this layer will be the Softmax, which is an activation function that is used in Multiclass classification.

”’

model.add(Turn(10, activation='softmax'))

”’

Paso 3: Compile the Model

Once we have defined the model of our neural network, Need Compile before we can use it for learning.

In this step, Let's specify the Cost function and the optimizer that we are going to use.

The Cost function is a measure of how well the model is doing in learning..

The optimizer is the algorithm used to adjust the weights of the neural network.

in this walkthrough, Let's use the Cross Entropy categorical cost function and the stochastic descending gradient optimizer.

”’

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

”’

Paso 4: Train the Model

Once we have compiled the model, we are ready to train you.

in this walkthrough, we are going to use the MNIST dataset.

It contains dataset 60.000 images of handwritten digits for training and 10.000 images of handwritten digits for validation.

We are going to train our neural network during 20 seasons, and we are going to use a batch size of 128.

”’

model.fit(x_train, y_train,
batch_size=128,
epochs=20,
validation_data=(x_test, y_test))

”’

Paso 5: Evaluate the Model

Once we have trained the model, we will evaluate its performance on the test dataset.

This data set contains 10.000 handwritten digits images.

”’

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

”’

The score that is returned is a list with two values.

The first value is the loss (Cost function) of the model in the test data set.

The second value is accuracy (accuracy) of the model in the test data set.

”’

print(score)

Applications of neural networks

artificial neural networks (ANN, Artificial Neural Network) are a family of models Machine Learning based on the structure and function of the Central Nervous System of living beings, in particular of the cerebral cortex.

There are various applications of neural networks., the most notable being the following:

pattern recognition

The pattern recognition It is one of the best known applications of neural networks.. It is a process by which certain patterns in a data set can be detected and classified..

Some examples of pattern recognition are recognizing objects in an image, speech recognition or character recognition in a document.

natural language processing

The natural language processing (NLP, Natural Language Processing) is another application of neural networks. It is a discipline that is responsible for analyzing, understand and generate natural language.

Some examples of natural language processing are named entity recognition, sentiment analysis or machine translation.

Anomaly detection

The anomaly detection is another application of neural networks. It is a process by which anomalous or unusual patterns can be detected in a data set..

Some examples of anomaly detection are detection of fraud in bank transactions, the detection of intruders in a computer system or the detection of errors in a manufacturing process.

Time series prediction

The time series prediction is another application of neural networks. It is a process by which future values ​​of a time series can be predicted from previous values..

Some examples of time series forecasting are forecasting the price of a stock on the stock market., predicting the demand for a product in a market or predicting energy consumption in a city.

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart