We will build a simple feed forward neural network with Keras. We will start with a two layer neural network for simplicity.
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
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)
In [3]:
# Split into features and labels
for i in range(0,10):
dataset = shuffle(dataset)
features = dataset[:,0:-1]
labels = dataset[:,-1]
In [4]:
traindata,testdata,trainlabels,testlabels = train_test_split(features,labels,test_size=0.3)
trainlabels = trainlabels.astype('int')
testlabels = testlabels.astype('int')
In [5]:
print 'Number of training samples : %d'%(traindata.shape[0])
print 'Number of test samples : %d'%(testdata.shape[0])
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
In [7]:
model.summary()
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"
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)
In [ ]:
In [ ]: