Keras – 4. Your First Neural Network

Welcome to CS With James

In this tutorial I will discuss how to build your first Neural Network (NN) train it on your machine.

Code

I will explain the code line by line.

#Import the dependencies

import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam

This will allow the python to load the module

 

#Hyperparameter

batch_size = 500
num_classes = 10
epochs = 10

This is Hyper-parameters used in the Machine Learning. I will explain more details later in different tutorial

#Load the Data

(x_train, y_train), (x_test, y_test) = mnist.load_data()
img_height, img_width = x_train.shape[1],x_train.shape[2]

This is method to load the data to your memory. Later I will explain how to load your own data.

#Change to label to one-hot encoding

y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

MNIST dataset contain 0~9 but in machine learning we can’t use the integer number. instead use one hot encoding.

For example

0 is represented as [1, 0, 0, 0, 0, 0, 0, 0, 0, 0]

5 is represented as [0, 0, 0, 0, 0, 1, 0, 0, 0, 0]

#Define the Model

model = Sequential()
model.add(Dense(32, activation='sigmoid', input_shape=(784,)))
model.add(Dense(32, activation='sigmoid'))
model.add(Dense(num_classes, activation='softmax'))

In this step we are going to define the model. In today’s tutorial I will only use the Dense() Layer. Every Dense layer need the Parameters of #Neurons and activation function, also the first layer need the input_shape. For now we are going to use sigmoid function for all the layers except last layer which uses softmax function.

#Print the model summary

model.summary()

#Determine Loss function and Optimizer

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

We are using this model to do the classification, so we use the categorical_crossentropy function. Use Adam for the Optimizer and accuracy for the metrics I will make a different tutorial on that later.

#Train the model

model.fit(x_train,y_train,batch_size=batch_size,epochs=epochs,verbose=1,validation_data =(x_test, y_test))

You can train your model using the .fit

x_train is the input dataset to train
y_train is the answer(Label) for each input
batch_size is how many inputs you want to train at the same time
epochs is how many iteration of training on the dataset
validation_data is different dataset for check if the model is training correctly.

#Test the Model

score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

Use .evaluate to test the final model


I have trained the model and I have got 91.41% of the accuracy. which is pretty good but in the next step I will increase this accuracy little by little

3 thoughts on “Keras – 4. Your First Neural Network”

  1. I was curious if you ever thought of changing the page layout of your website?
    Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so people could connect with it
    better. Youve got an awful lot of text for only having one or two images.
    Maybe you could space it out better?

  2. Nice blog! Is your theme custom made or did you download it from somewhere?
    A design like yours with a few simple tweeks would really make my blog jump out.
    Please let me know where you got your design. Thanks a lot

Leave a Reply

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