Skip to content

First real neural network

In this post I will provide working code (written in Python programming language) for simple neural network

Is it difficult to create first neural network? No, just copy/paste code from article below.

In this post I will provide working code (written in Python programming language) for simple neural network that used to deal with “classification” type of problems, for so-called “supervised learning”. Basics of such kind of neural network can be read at my previous post .


We will deal with medical data, it is well known “Pima Indians Diabetes Database”, it is medical records with some health parameters, each person record has a label if given person has diabetes or it hasn’t. We split data into 2 separated file, one for neural network training and another for actual prediction. Training data is here , data for prediction is here .  Full dataset description is here (description for all records without split for “training” and “prediction”) After being trained on “training data” , our neural network will be able to classify new data (data for prediction) such a way that it will predict (based on provided medical data during ‘training’ phase) if given persons has diabetes or they don’t have it.


We will have 2 separate Python scripts, first for “training”, second for “prediction”.


So, go ahead with first part of network, the one for “training” purpose.

Part 1. Training.

Here the final code for “training phase” (part 1 out of 2) which we will discuss line by line.


from keras.models import Sequential
from keras.layers import Dense
import numpy

#--line below to load *.csv file into memory, data columns separated by comma
dataset_train = numpy.loadtxt("diabet_train.csv", delimiter=",") #here we load data into Numpy array

#--two lines below to split input data into actual training data and for setting labels for each data record
X_train = dataset_train[:,0:8] #here we split data for actual training data, we cut first 8 columns
Y_train = dataset_train[:,8] # here we get labels for each record, i.e. classification for each record, we cut latest column after first 8 training columns, i.e. we cut last column number 9

#---start of the code for actual neural net architecture construction----
model = Sequential()
model.add(Dense(8, input_dim=8, activation='relu'))
model.add(Dense(6, activation='relu'))
model.add(Dense(3, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
#---end of actual neural net architecture-----

model.compile(loss="binary_crossentropy", optimizer="adam", metrics=['accuracy'])
model.fit(X_train, Y_train, epochs = 5000, batch_size=500) #here actual trainig happens

model.save('my_model.h5')#here we save on HDD/SDD disk our model and all "weights"/neurons after training

# evaluate the model on training data(not the same performance as for any new given data that used for actual prediction
scores = model.evaluate(X_train, Y_train)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))


Here below we explain each block of neural network code.

 

——————-
Block below is to load into out python script all needed libraries that actually perform the work, while within our code we just operate with high-level API provided by libraries we just loaded:

from keras.models import Sequential
from keras.layers import Dense
import numpy

——————-
Line below is to load *.csv file with actual data into memory, it is performed with help of “numpy” python library that can load and understand “comma separated values” files and can put such kind of csv data into memory arrays. Loaded data are stored in variable “dataset_train”(numpy array)

dataset_train = numpy.loadtxt("diabet_train.csv", delimiter=",") 

——————-
Two lines below are used to split loaded data into actual training data and data with labels. Into “X_train” variable we put all medical records, line be line, while “Y_train”is used to store “labels” for each medical record, i.e. “Y_train” has mark/label “disease Yes” or “disease No” .

X_train = dataset_train[:,0:8] #first 8 rows - training data
Y_train = dataset_train[:,8] #last column, column after 8, contains labels

——————-
Five lines below are used to create actual neural network. We will give details for this network architecture in separate article for network model which described here, while in current article I explain just most basic things. Network has 9 input “neurons” that receive data with 8 different features, 8 attributes for each medical record.

model = Sequential()
model.add(Dense(8, input_dim=8, activation='relu')) #input layer
model.add(Dense(6, activation='relu')) #first hidden layer with 6 neurons for each medical record
model.add(Dense(3, activation='relu')) #second hidden layer with 3 neurons for each medical record
model.add(Dense(1, activation='sigmoid')) #output layer with sigmoid activation, it has only 1 neuron

I will NOT expalain network model here in this article, I explain it in details in separate article for network model, for now just remember that those 5 lines are the network itself, i.e. architecture of our neural network.

——————-
Line below is to configure learning, i.e. what and how should network evaluate to reach its goal.

model.compile(loss="binary_crossentropy", optimizer="adam", metrics=['accuracy'])

There are 3 parameters in this line. A loss function(this is the objective that the model will try to minimize), An optimizer and a list of metrics (for any classification problem you will want to set this to metrics=[‘accuracy’] )

——————-
Line below is to start learning, i.e. this line starts the process of training, for 5000 iterations (epochs)

model.fit(X_train, Y_train, epochs = 5000, batch_size=500)

——————-
Line below is to save our trained network, i.e. not only network architecture that we created at lines 13-17, but all neuron weights we got after 5000 trainings.

model.save('my_model.h5')

Data saved in *.h5 format. We will read and load into memory “my_model.h5” file with trained network in second part of our exercise, at the step of “prediction” where we will predict if patient has diabetes or does not have it based on his/her medical data. (new data that we will feed into network)

——————-
Two latest line below is to evaluate performance of our trained neural network, i.e. to measure what is percent of successful detection of diabetes in given medical records. It is extremely important to understand that here we measure performance not on new data but on data that was used on training, so network performance on new medical data might be much worse.

scores = model.evaluate(X_train, Y_train)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))

We will reveal true performance of our trained neural network in second Python script when we will feed new medical data into trained network.

Part 2. Prediction.

Second script used for actual prediction, i.e. we feed new medical records into trained neural network and predict if person has diabetes or no.

Here is the full code of “prediction” script.

from keras.models import load_model
import numpy

model = load_model('my_model.h5')

dataset_predict = numpy.loadtxt("diabet_predict.csv", delimiter=",")
X_predict = dataset_predict[:,0:8]
Y_predict = dataset_predict[:,8]

scores = model.evaluate(X_predict, Y_predict)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))

predictions = model.predict(X_predict)
rounded = [round(x[0]) for x in predictions]

print(rounded)
print('-----------------')
print(Y_predict)


Here below we explain each block of neural network code.

 

——————-
Block below is to load into out python script all needed libraries that actually perform the work, while within our code we just operate with high-level API provided by libraries we just loaded:

from keras.models import load_model
import numpy

 

——————-
By this one line we load into memory the saved model which we’ve got on “training” phase.

model = load_model('my_model.h5')

——————-
Line below is to load *.csv file with actual data into memory, it is performed with help of “numpy” python library that can load and understand “comma separated values” files and can put such kind of csv data into memory arrays. Loaded data are stored in variable “dataset_predict”(numpy array)

dataset_predict = numpy.loadtxt("diabet_predict.csv", delimiter=",")

——————-
Two lines below are used to split loaded data into data for prediction and data with labels. Into “X_predict” variable we put all medical records, line be line, while “Y_predict” is used to store “labels” for each medical record for “disease Yes” or “disease No” .

X_predict = dataset_predict[:,0:8] #first 8 rows - new medical data for prediction desease
Y_predict = dataset_predict[:,8] #last column, column after 8, contains labels

——————-
This is used to evaluate how good our model is. We could do so because we have dataset with already set labels, so we compare our prediction with actual correct data we already have. In real life for new prediction based on new data, you will not have such luxury to understand how good your model is on new data, since this is exactly the task of neural network to predict something unknown.

scores = model.evaluate(X_predict, Y_predict)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))

——————-
Those five lines used just for visualisation, you can see both predicted data { print(rounded) } and actual data labels which we fortunately know in our example { print(Y_predict) }

predictions = model.predict(X_predict)
rounded = [round(x[0]) for x in predictions]
print(rounded)
print('-----------------')
print(Y_predict)

That’s it.

Be First to Comment

Leave a Reply

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