Artificial Neural Network with Keras — An Example

Caner Dabakoglu
4 min readDec 9, 2018

--

What is Artificial Neural Network?

Artifical Neural Networks (ANN) are one of the main tools which are used in machine learning. “Neural” part of their name is called as like that because these systems try to learn things like human brain. Replicated networks contains some kind of neurons and these neurons create a network by connecting each other. These networks have capacity of learning, storing and finding out relationships between datas like a human!
For example they can learn to identify images that contain cars by analyzing example images. So after learning phase is completed if you ask to algorithm ‘Is it a car?’ by giving it an image, algorithm can answer your question becuase it identified other cars images and learned how a car looks like.
Neural Networks has input and output layers like others but most of the cases they also have hidden layers, and usually we can say how ‘deep’ our algorithm according to number of hidden layers.

Before we start you may take a glance at article about logistic regression and other machine learning algortihm basics and how they are builded with sklearn please check the link:

https://medium.com/@cdabakoglu/heart-disease-logistic-regression-machine-learning-d0ebf08e55c0

Now we’ll try to use this algorithm with a dataset contains images of 10 different classes of fashion.

Let’s import libraries we’ll use.

import numpy as np 
import pandas as pd
import matplotlib.pyplot as plt

About Dataset

Dataset consists a training set of 60,000 examples and a test set of 10,000 examples. Each example is a 28x28 grayscale image, associated with a label from 10 classes.

- Each image is 28 pixels in height and 28 pixels in width, for a total of 784 pixels in total.
- Each pixel has a single pixel-value associated with it, indicating the lightness or darkness of that pixel, with higher numbers meaning darker. This pixel-value is an integer between 0 and 255.
- The training and test data sets have 785 columns. The first column consists of the class labels, and represents class of clothing. The rest of the columns contain the pixel-values of the associated image.

Each training and test example is assigned to one of the following labels:

- 0 T-shirt/top
- 1 Trouser
- 2 Pullover
- 3 Dress
- 4 Coat
- 5 Sandal
- 6 Shirt
- 7 Sneaker
- 8 Bag
- 9 Ankle boot

Since we want binary classification we’ll just choose 0 (T-Shirt/top) and 1 (Trouser) for our data.

Read Data

dfAll = pd.read_csv("../input/fashion-mnist_train.csv")
df = dfAll[((dfAll.label == 0) | (dfAll.label == 1))]
df.head()

Data columns start from ‘label’ to ‘pixel 784'. Like I mentioned above our size of images in the dataset is 28x28 that means each images consist of 784 pixels.

We will split our data as usual. 30% of our data will be test data and 70% of our data will be used to train our model.

from sklearn.model_selection import train_test_splitX = df.drop(["label"], axis=1)
Y = df.label
x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.30, random_state=42)

Let’s look some images in the dataset.

# Example Imagesplt.figure(figsize=(8,8))
for i in range(4):
plt.subplot(2,2,i+1)
plt.axis('off')
plt.imshow(x_train.head().values[i].reshape(28,28), cmap='gray', interpolation='none')

ANN with Keras

x_train = x_train.values.T
y_train = y_train.values.reshape(8400,1).T
x_test = x_test.values.T
y_test = y_test.values.reshape(3600,1).T
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import cross_val_score
from keras.models import Sequential
from keras.layers import Dense
def buildClassifier():
classifier = Sequential()
classifier.add(Dense(units=8, kernel_initializer="uniform", activation="relu", input_dim=x_train.shape[0])) # Hidden Layer 1 with 8 nodes
classifier.add(Dense(units=6, kernel_initializer="uniform", activation="relu")) # Hidden Layer 2 with 6 nodes
classifier.add(Dense(units=1, kernel_initializer="uniform", activation="sigmoid")) # Output Layer
classifier.compile(optimizer = "adam", loss = "binary_crossentropy", metrics = ["accuracy"])
return classifier
classifier = KerasClassifier(build_fn=buildClassifier, epochs = 100)
accuracies = cross_val_score(estimator = classifier, X = x_train.T, y = y_train.T, cv=3)
mean = accuracies.mean()
variance = accuracies.std()
print("Accuracy Mean is {:.2f}%".format(mean*100))
print("Accuracy Variance is {}".format(variance))

We can say our model works with approximately 99% of accuracy.

For other optimizers or parameters please check the link:

https://keras.io/optimizers/

Thanks for your time.

LinkedIn: https://www.linkedin.com/in/canerdabakoglu/

GitHub: https://github.com/cdabakoglu

Kaggle: https://www.kaggle.com/cdabakoglu

--

--