Skip to content

Creating a basic artificial neural network on python using keras

creating-a-basic-artificial-neural-network-on-python-using-keras

With the popularity that deep learning has acquired recently, artificial neural networks have become very relevant in the world of AI.

Artificial neural networks are not a new concept, they have suffered for a long time before being recognized as a powerful tool. If you review the history of artificial neural networks, you will find that there have been several big challenges that the technology had to overcome to exist as we know it today.

As powerful as they are, the implementation can be pretty simple because of keras. This library allows us to easily create a fully functional artificial neural network with the architecture we need, to train this network and to use it.

I will not explain every piece or concept related to artificial neural networks. Instead I will focus on showing how to work with keras.

Just start by creating a model. In this case we will use a sequential model, which just means that we are going to stack the layers one after another:

from keras.models import Sequential
model = Sequential()

After that, we can add the layers. We will just use Dense layers, which means that our layers will be fully interconnected. You can always specify different structures if you want something different, but Dense is enough for this example:

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

There is a wide range of activation functions that you can try. If none of them work for you, you can always provide your own.

After we have our architecture ready, we must configure how the network will learn. We will do this by “compiling” the model and passing information like the loss function and the optimizer function:

model.compile(loss='categorical_crossentropy',
              optimizer='sgd')

And that’s it. We have an artificial neural network ready to be trained. If we have our data ready for this, we can just train it like this:

model.fit(features, target)

The training is the most expensive process when working with artificial neural networks, so we don’t want to do that every time. To save your trained network you can do it like this:

model.save(filepath)

An hdf5 file will be generated that will contain all the information required to reload your network at a later time:

from keras.models import load_model
model = load_model(filepath)

Keras provides a high level interface to interact with your artificial neural network, and it is nicely documented. If you are using another library to work with your networks, give keras a try and compare the APIs.