The way birds inspired humans to create an aeroplane the same way a human’s brain inspired humans to build intelligent machines. Neural networks were inspired by biological neurons found in the brain of a human. In this article, I will introduce you to the implementation of neural networks using Python.
Neural Networks
The neural network is the most important concept in deep learning, which is a subset of machine learning. Neural networks were inspired by biological neurons found in the brain of a human. You can think of a neural network as a machine learning algorithm that works the same way as a human brain.
You must be thinking about why we use neural networks when we already have so many machine learning algorithms. The reason is that a neural network needs a huge amount of data. As every business has now understood the importance of data, we have enough data to train a neural network. A neural network can easily outperform any machine learning algorithm while working on a very large data set.
Neural Networks using Python
Hope you now know what a neural network is and why we prefer to use it over other machine learning algorithms while working with huge datasets. To understand how the neural network works, let’s train one using Python. When training neural networks on a huge dataset, you should have a GPU compatible system, otherwise, it will take hours to run your code. If you don’t have a GPU compatible machine, you can use Google Colab.
Now let’s see how to train a classification model with neural networks using Python. I will start by importing the necessary Python libraries and the dataset:
import tensorflow as tf from tensorflow import keras fashion_mnist = keras.datasets.fashion_mnist (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
Here I am using the Fashion MNIST dataset which is not a very huge dataset but good enough to train a neural network. Now let’s split the data:
x_valid, x_train = x_train[:5000]/255.0, x_train[5000:]/255.0 y_valid, y_train = y_train[:5000], y_train[5000:]
The Fashion MNIST dataset is just like the MNIST digits dataset but to understand what we are dealing with we need to create a Python list of names of the classes in the dataset:
classes = ["T-shirt/top", "Trouser", "Pullover", "Dress", "Coat", "Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot"]
Now let’s create a neural network model:
model = keras.models.Sequential([ keras.layers.Flatten(input_shape=[28, 28]), keras.layers.Dense(300, activation="relu"), keras.layers.Dense(100, activation="relu"), keras.layers.Dense(10, activation="softmax") ])
Now let’s have a look at the summary of the model which simply means to display each layer of the model:
print(model.summary())
Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= flatten (Flatten) (None, 784) 0 _________________________________________________________________ dense (Dense) (None, 300) 235500 _________________________________________________________________ dense_1 (Dense) (None, 100) 30100 _________________________________________________________________ dense_2 (Dense) (None, 10) 1010 ================================================================= Total params: 266,610 Trainable params: 266,610 Non-trainable params: 0
After creating the model, the next step is to compile the model. Here is how to compile your model:
model.compile(loss="sparse_categorical_crossentropy", optimizer="sgd", metrics=["accuracy"])
Now, the final step is to train the model which means to fit the data into the neural network:
history = model.fit(x_train, y_train, epochs=30, validation_data=(x_valid, y_valid))
Now let’s have a loot at the performance of the model and test the model on the test set:
import pandas as pd import matplotlib.pyplot as plt pd.DataFrame(history.history).plot(figsize=(12, 8)) plt.grid(True) plt.gca().set_ylim(0, 1) plt.legend() plt.show()

import numpy as np x_new = x_test[:3] y_pred = model.predict_classes(x_new) print(y_pred) print(np.array(classes)[y_pred])
[9 2 1] ['Ankle boot' 'Pullover' 'Trouser']
Summary
This is how to train a neural network using Python. In this article, I trained a neural network model for the task of classification on the fashion MNIST dataset. I hope you liked this article on the implementation of Neural networks using Python. Feel free to ask your valuable questions in the comments section below.