First neural network

We will build a simple feed forward neural network with Keras. We will start with a two layer neural network for simplicity.

Import all necessary python packages


In [1]:
# For simple array operations
import numpy as np 

# To construct the model
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.optimizers import SGD

# Some utility for splitting data and printing the classification report
from sklearn.cross_validation import train_test_split
from sklearn.metrics import classification_report
from sklearn.utils import shuffle


Using Theano backend.
/usr/local/lib/python2.7/dist-packages/sklearn/cross_validation.py:44: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
  "This module will be removed in 0.20.", DeprecationWarning)

Load some data

The dataset in this experiment is a publically available pulsar dataset from Rob Lyons paper. It is in a simple ASCII format delimited by commas. There are 8 statistical features that represent different measure of the de-dispersed pulse profile of pulsar and non pulsar candidates. The last column is a label column where '1' represents a pulsar and '0' represents a non pulsar candidate.


In [2]:
dataset = np.loadtxt('../Data/HTRU_2.csv',delimiter=',')

print 'The dataset has %d rows and %d features' %(dataset.shape[0],dataset.shape[1]-1)


The dataset has 17898 rows and 8 features

In [3]:
# Split into features and labels
for i in range(0,10):
    dataset = shuffle(dataset)
    
features = dataset[:,0:-1]
labels = dataset[:,-1]

Split the data into training and testing data


In [4]:
traindata,testdata,trainlabels,testlabels = train_test_split(features,labels,test_size=0.3)

trainlabels = trainlabels.astype('int')
testlabels = testlabels.astype('int')

Show some info about the split


In [5]:
print 'Number of training samples : %d'%(traindata.shape[0])
print 'Number of test samples : %d'%(testdata.shape[0])


Number of training samples : 12528
Number of test samples : 5370

Construct the model


In [6]:
model = Sequential() # Our model is a simple feedforward model
model.add(Dense(64,input_shape=(8,)))  # The first layer holds the input for in which our case the there are 8 features.
model.add(Activation('relu')) # First activation layer is rectified linear unit (RELU)
model.add(Dense(256)) # Second layer has 256 neurons 
model.add(Activation('relu')) # Second RELU activation
model.add(Dense(1)) # Third layer has 1 neuron because we have only one outcome - pulsar or non pulsar
model.add(Activation('softmax')) # The Scoring layer which normalizes the scores

This step makes sure that our model is correctly defined and there is no error in the model definition. It will also show the sizes of each layers


In [7]:
model.summary()


____________________________________________________________________________________________________
Layer (type)                     Output Shape          Param #     Connected to                     
====================================================================================================
dense_1 (Dense)                  (None, 64)            576         dense_input_1[0][0]              
____________________________________________________________________________________________________
activation_1 (Activation)        (None, 64)            0           dense_1[0][0]                    
____________________________________________________________________________________________________
dense_2 (Dense)                  (None, 256)           16640       activation_1[0][0]               
____________________________________________________________________________________________________
activation_2 (Activation)        (None, 256)           0           dense_2[0][0]                    
____________________________________________________________________________________________________
dense_3 (Dense)                  (None, 1)             257         activation_2[0][0]               
____________________________________________________________________________________________________
activation_3 (Activation)        (None, 1)             0           dense_3[0][0]                    
====================================================================================================
Total params: 17473
____________________________________________________________________________________________________

Compile the model

This step defines the parameters for training


In [8]:
model.compile(loss='binary_crossentropy',  # Loss function for binary classification
              optimizer=SGD(), # Optimizer for learning, in this case Stochastic Gradient Descent (SGD)
             metrics=['accuracy']) # Evaluation function"

Train the model

In this step we will train the network and also define the number of epochs and batch size for training.


In [9]:
batch_size = 100
n_epochs = 10

training = model.fit(traindata,trainlabels,
                     nb_epoch=n_epochs,
                     batch_size=batch_size,
                      validation_data=(testdata, testlabels),
                     verbose=1)


Train on 12528 samples, validate on 5370 samples
Epoch 1/10
12528/12528 [==============================] - 0s - loss: 14.4828 - acc: 0.0916 - val_loss: 14.4817 - val_acc: 0.0916
Epoch 2/10
12528/12528 [==============================] - 0s - loss: 14.4828 - acc: 0.0916 - val_loss: 14.4817 - val_acc: 0.0916
Epoch 3/10
12528/12528 [==============================] - 0s - loss: 14.4828 - acc: 0.0916 - val_loss: 14.4817 - val_acc: 0.0916
Epoch 4/10
12528/12528 [==============================] - 0s - loss: 14.4828 - acc: 0.0916 - val_loss: 14.4817 - val_acc: 0.0916
Epoch 5/10
12528/12528 [==============================] - 0s - loss: 14.4828 - acc: 0.0916 - val_loss: 14.4817 - val_acc: 0.0916
Epoch 6/10
12528/12528 [==============================] - 0s - loss: 14.4828 - acc: 0.0916 - val_loss: 14.4817 - val_acc: 0.0916
Epoch 7/10
12528/12528 [==============================] - 0s - loss: 14.4828 - acc: 0.0916 - val_loss: 14.4817 - val_acc: 0.0916
Epoch 8/10
12528/12528 [==============================] - 0s - loss: 14.4828 - acc: 0.0916 - val_loss: 14.4817 - val_acc: 0.0916
Epoch 9/10
12528/12528 [==============================] - 0s - loss: 14.4828 - acc: 0.0916 - val_loss: 14.4817 - val_acc: 0.0916
Epoch 10/10
12528/12528 [==============================] - 0s - loss: 14.4828 - acc: 0.0916 - val_loss: 14.4817 - val_acc: 0.0916

In [ ]:


In [ ]: