In [66]:
%matplotlib inline
from matplotlib import pyplot as plt
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.optimizers import SGD
from keras.utils import np_utils
from sklearn.cross_validation import train_test_split
from sklearn.metrics import classification_report
from sklearn.externals import joblib
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
import numpy as np
import pandas as pd
from time import time
In [11]:
np.set_printoptions(precision=4)
np.set_printoptions(suppress=True)
In [12]:
model = Sequential()
In [13]:
model.add(Dense(input_dim=784, output_dim=200, init="uniform"))
model.add(Activation("relu"))
model.add(Dense(input_dim=200, output_dim=9, init="uniform"))
model.add(Activation("softmax"))
In [14]:
# model.compile(loss='categorical_crossentropy', optimizer=SGD(lr=0.01, momentum=0.9, nesterov=True))
model.compile(loss='categorical_crossentropy', optimizer='sgd')
In [15]:
features = joblib.load("./mldata/features_1200.mat")
labels = joblib.load("./mldata/lables_1200.mat")
features = np.array(features, 'int16')
labels = np.array(labels, 'int')
t0 = time()
def scale(X, eps = 0.001):
# scale the data points s.t the columns of the feature space
# (i.e the predictors) are within the range [0, 1]
return (X - np.min(X, axis = 0)) / (np.max(X, axis = 0) + eps)
features = features.astype("float32")
features = scale(features)
print "escape time : ", round(time()-t0, 3), "s"
# scale the data to the range [0, 1] and then construct the training
# and testing splits
(trainX, testX, trainY, testY) = train_test_split(features, labels, test_size = 0.1)
print "the shape of training set %s rows, %s columns" %(trainX.shape[0], trainX.shape[1])
print "the shape of test set %s rows, %s columns" %(testX.shape[0], testX.shape[1])
print "the range of training set : %s ~ %s" %(trainX.min(),trainX.max())
print "the range of test set : %s ~ %s" %(testX.min(),testX.max())
In [56]:
trainX.shape
Out[56]:
In [29]:
nb_classes = 9
batch_size = 64
nb_epoch = 20
np.random.seed(1337) # for reproducibility
In [21]:
trainY[0]
Out[21]:
In [26]:
# convert class vectors to binary class matrices
trainY = np_utils.to_categorical(trainY, nb_classes)
testY = np_utils.to_categorical(testY, nb_classes)
In [27]:
trainY[0]
Out[27]:
In [31]:
# model.fit(trainX, trainY, nb_epoch=10, batch_size=32)
model.fit(trainX, trainY, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=2)#, validation_data=(X_test, y_test))
Out[31]:
In [43]:
score = model.evaluate(trainX, trainY, show_accuracy=True, verbose=0, batch_size=32)
print 'Test score : %s' %score[0]
print 'Test accuracy : %s' %score[1]
In [55]:
print model.predict_classes(trainX, batch_size=32)
print model.predict_proba(trainX, batch_size=32)
In [59]:
features = joblib.load("./mldata/features_1200.mat")
labels = joblib.load("./mldata/lables_1200.mat")
features = np.array(features, 'int16')
labels = np.array(labels, 'int')
t0 = time()
def scale(X, eps = 0.001):
# scale the data points s.t the columns of the feature space
# (i.e the predictors) are within the range [0, 1]
return (X - np.min(X, axis = 0)) / (np.max(X, axis = 0) + eps)
features = features.astype("float32")
features = scale(features)
print "escape time : ", round(time()-t0, 3), "s"
# scale the data to the range [0, 1] and then construct the training
# and testing splits
(trainX, testX, trainY, testY) = train_test_split(features, labels, test_size = 0.5)
(valX, testX, valY, testY) = train_test_split(testX, testY, test_size = 0.5)
print "the shape of training set %s rows, %s columns" %(trainX.shape[0], trainX.shape[1])
print "the shape of test set %s rows, %s columns" %(testX.shape[0], testX.shape[1])
print "the range of training set : %s ~ %s" %(trainX.min(),trainX.max())
print "the range of test set : %s ~ %s" %(testX.min(),testX.max())
In [60]:
print trainX.shape
print testX.shape
print valX.shape
In [62]:
# convert class vectors to binary class matrices
trainY = np_utils.to_categorical(trainY, nb_classes)
testY = np_utils.to_categorical(testY, nb_classes)
valY = np_utils.to_categorical(valY, nb_classes)
In [63]:
# model.fit(trainX, trainY, nb_epoch=10, batch_size=32)
mm = model.fit(trainX, trainY, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=2, validation_data=(valX, valY))
In [81]:
score = model.evaluate(valX, valY, show_accuracy=True, verbose=0, batch_size=32)
print 'Test score : %s' %score[0]
print 'Test accuracy : %s' %score[1]
In [67]:
df = pd.DataFrame(mm)
In [70]:
df
Out[70]:
In [72]:
df[['acc', 'epoch']].plot()
Out[72]:
In [73]:
df.index = df['epoch']
In [76]:
df.head()
Out[76]:
In [78]:
df['acc'].plot()
Out[78]:
In [88]:
features = joblib.load("./mldata/features_1200.mat")
labels = joblib.load("./mldata/lables_1200.mat")
features = np.array(features, 'int16')
labels = np.array(labels, 'int')
t0 = time()
def scale(X, eps = 0.001):
# scale the data points s.t the columns of the feature space
# (i.e the predictors) are within the range [0, 1]
return (X - np.min(X, axis = 0)) / (np.max(X, axis = 0) + eps)
features = features.astype("float32")
features = scale(features)
print "escape time : ", round(time()-t0, 3), "s"
# scale the data to the range [0, 1] and then construct the training
# and testing splits
(trainX, testX, trainY, testY) = train_test_split(features, labels, test_size = 0.5)
(valX, testX, valY, testY) = train_test_split(testX, testY, test_size = 0.5)
# convert class vectors to binary class matrices
trainY = np_utils.to_categorical(trainY, nb_classes)
testY = np_utils.to_categorical(testY, nb_classes)
valY = np_utils.to_categorical(valY, nb_classes)
mm = model.fit(trainX, trainY,
batch_size=batch_size,
nb_epoch=100,
show_accuracy=True,
verbose=0,
validation_data=(testX, testY))
score = model.evaluate(valX, valY, show_accuracy=True, verbose=0, batch_size=32)
print 'Test score : %s' %score[0]
print 'Test accuracy : %s' %score[1]
df = pd.DataFrame(mm)
df.index = df['epoch']
df['acc'].plot()
Out[88]:
In [90]:
features = joblib.load("./mldata/features_1200.mat")
labels = joblib.load("./mldata/lables_1200.mat")
features = np.array(features, 'int16')
labels = np.array(labels, 'int')
t0 = time()
def scale(X, eps = 0.001):
# scale the data points s.t the columns of the feature space
# (i.e the predictors) are within the range [0, 1]
return (X - np.min(X, axis = 0)) / (np.max(X, axis = 0) + eps)
features = features.astype("float32")
features = scale(features)
print "escape time : ", round(time()-t0, 3), "s"
# scale the data to the range [0, 1] and then construct the training
# and testing splits
(trainX, testX, trainY, testY) = train_test_split(features, labels, test_size = 0.4)
(valX, testX, valY, testY) = train_test_split(testX, testY, test_size = 0.5)
# convert class vectors to binary class matrices
trainY = np_utils.to_categorical(trainY, nb_classes)
testY = np_utils.to_categorical(testY, nb_classes)
valY = np_utils.to_categorical(valY, nb_classes)
mm = model.fit(trainX, trainY,
batch_size=batch_size,
nb_epoch=100,
show_accuracy=True,
verbose=0,
validation_data=(testX, testY))
score = model.evaluate(valX, valY, show_accuracy=True, verbose=0, batch_size=32)
print 'Test score : %s' %score[0]
print 'Test accuracy : %s' %score[1]
df = pd.DataFrame(mm)
df.index = df['epoch']
df['acc'].plot()
Out[90]:
In [91]:
features = joblib.load("./mldata/features_1200.mat")
labels = joblib.load("./mldata/lables_1200.mat")
features = np.array(features, 'int16')
labels = np.array(labels, 'int')
t0 = time()
def scale(X, eps = 0.001):
# scale the data points s.t the columns of the feature space
# (i.e the predictors) are within the range [0, 1]
return (X - np.min(X, axis = 0)) / (np.max(X, axis = 0) + eps)
features = features.astype("float32")
features = scale(features)
print "escape time : ", round(time()-t0, 3), "s"
# scale the data to the range [0, 1] and then construct the training
# and testing splits
(trainX, testX, trainY, testY) = train_test_split(features, labels, test_size = 0.3)
(valX, testX, valY, testY) = train_test_split(testX, testY, test_size = 0.5)
# convert class vectors to binary class matrices
trainY = np_utils.to_categorical(trainY, nb_classes)
testY = np_utils.to_categorical(testY, nb_classes)
valY = np_utils.to_categorical(valY, nb_classes)
mm = model.fit(trainX, trainY,
batch_size=batch_size,
nb_epoch=100,
show_accuracy=True,
verbose=0,
validation_data=(testX, testY))
score = model.evaluate(valX, valY, show_accuracy=True, verbose=0, batch_size=32)
print 'Test score : %s' %score[0]
print 'Test accuracy : %s' %score[1]
df = pd.DataFrame(mm)
df.index = df['epoch']
df['acc'].plot()
Out[91]:
In [92]:
features = joblib.load("./mldata/features_1200.mat")
labels = joblib.load("./mldata/lables_1200.mat")
features = np.array(features, 'int16')
labels = np.array(labels, 'int')
t0 = time()
def scale(X, eps = 0.001):
# scale the data points s.t the columns of the feature space
# (i.e the predictors) are within the range [0, 1]
return (X - np.min(X, axis = 0)) / (np.max(X, axis = 0) + eps)
features = features.astype("float32")
features = scale(features)
print "escape time : ", round(time()-t0, 3), "s"
# scale the data to the range [0, 1] and then construct the training
# and testing splits
(trainX, testX, trainY, testY) = train_test_split(features, labels, test_size = 0.2)
(valX, testX, valY, testY) = train_test_split(testX, testY, test_size = 0.5)
# convert class vectors to binary class matrices
trainY = np_utils.to_categorical(trainY, nb_classes)
testY = np_utils.to_categorical(testY, nb_classes)
valY = np_utils.to_categorical(valY, nb_classes)
mm = model.fit(trainX, trainY,
batch_size=batch_size,
nb_epoch=100,
show_accuracy=True,
verbose=0,
validation_data=(testX, testY))
score = model.evaluate(valX, valY, show_accuracy=True, verbose=0, batch_size=32)
print 'Test score : %s' %score[0]
print 'Test accuracy : %s' %score[1]
df = pd.DataFrame(mm)
df.index = df['epoch']
df['acc'].plot()
Out[92]:
In [93]:
features = joblib.load("./mldata/features_1200.mat")
labels = joblib.load("./mldata/lables_1200.mat")
features = np.array(features, 'int16')
labels = np.array(labels, 'int')
t0 = time()
def scale(X, eps = 0.001):
# scale the data points s.t the columns of the feature space
# (i.e the predictors) are within the range [0, 1]
return (X - np.min(X, axis = 0)) / (np.max(X, axis = 0) + eps)
features = features.astype("float32")
features = scale(features)
print "escape time : ", round(time()-t0, 3), "s"
# scale the data to the range [0, 1] and then construct the training
# and testing splits
(trainX, testX, trainY, testY) = train_test_split(features, labels, test_size = 0.1)
(valX, testX, valY, testY) = train_test_split(testX, testY, test_size = 0.5)
# convert class vectors to binary class matrices
trainY = np_utils.to_categorical(trainY, nb_classes)
testY = np_utils.to_categorical(testY, nb_classes)
valY = np_utils.to_categorical(valY, nb_classes)
mm = model.fit(trainX, trainY,
batch_size=batch_size,
nb_epoch=100,
show_accuracy=True,
verbose=0,
validation_data=(testX, testY))
score = model.evaluate(valX, valY, show_accuracy=True, verbose=0, batch_size=32)
print 'Test score : %s' %score[0]
print 'Test accuracy : %s' %score[1]
df = pd.DataFrame(mm)
df.index = df['epoch']
df['acc'].plot()
Out[93]:
In [94]:
features = joblib.load("./mldata/features_1200.mat")
labels = joblib.load("./mldata/lables_1200.mat")
features = np.array(features, 'int16')
labels = np.array(labels, 'int')
t0 = time()
def scale(X, eps = 0.001):
# scale the data points s.t the columns of the feature space
# (i.e the predictors) are within the range [0, 1]
return (X - np.min(X, axis = 0)) / (np.max(X, axis = 0) + eps)
features = features.astype("float32")
features = scale(features)
print "escape time : ", round(time()-t0, 3), "s"
# scale the data to the range [0, 1] and then construct the training
# and testing splits
(trainX, testX, trainY, testY) = train_test_split(features, labels, test_size = 0.05)
(valX, testX, valY, testY) = train_test_split(testX, testY, test_size = 0.5)
# convert class vectors to binary class matrices
trainY = np_utils.to_categorical(trainY, nb_classes)
testY = np_utils.to_categorical(testY, nb_classes)
valY = np_utils.to_categorical(valY, nb_classes)
mm = model.fit(trainX, trainY,
batch_size=batch_size,
nb_epoch=100,
show_accuracy=True,
verbose=0,
validation_data=(testX, testY))
score = model.evaluate(valX, valY, show_accuracy=True, verbose=0, batch_size=32)
print 'Test score : %s' %score[0]
print 'Test accuracy : %s' %score[1]
df = pd.DataFrame(mm)
df.index = df['epoch']
df['acc'].plot()
Out[94]:
In [95]:
trainX.shape
Out[95]:
In [96]:
features = joblib.load("./mldata/features_1200.mat")
labels = joblib.load("./mldata/lables_1200.mat")
features = np.array(features, 'int16')
labels = np.array(labels, 'int')
t0 = time()
def scale(X, eps = 0.001):
# scale the data points s.t the columns of the feature space
# (i.e the predictors) are within the range [0, 1]
return (X - np.min(X, axis = 0)) / (np.max(X, axis = 0) + eps)
features = features.astype("float32")
features = scale(features)
print "escape time : ", round(time()-t0, 3), "s"
# scale the data to the range [0, 1] and then construct the training
# and testing splits
(trainX, testX, trainY, testY) = train_test_split(features, labels, test_size = 0.01)
(valX, testX, valY, testY) = train_test_split(testX, testY, test_size = 0.5)
# convert class vectors to binary class matrices
trainY = np_utils.to_categorical(trainY, nb_classes)
testY = np_utils.to_categorical(testY, nb_classes)
valY = np_utils.to_categorical(valY, nb_classes)
mm = model.fit(trainX, trainY,
batch_size=batch_size,
nb_epoch=100,
show_accuracy=True,
verbose=0,
validation_data=(testX, testY))
score = model.evaluate(valX, valY, show_accuracy=True, verbose=0, batch_size=32)
print 'Test score : %s' %score[0]
print 'Test accuracy : %s' %score[1]
df = pd.DataFrame(mm)
df.index = df['epoch']
df['acc'].plot()
Out[96]:
In [97]:
trainX.shape
Out[97]:
In [98]:
features = joblib.load("./mldata/features_1200.mat")
labels = joblib.load("./mldata/lables_1200.mat")
features = np.array(features, 'int16')
labels = np.array(labels, 'int')
t0 = time()
def scale(X, eps = 0.001):
# scale the data points s.t the columns of the feature space
# (i.e the predictors) are within the range [0, 1]
return (X - np.min(X, axis = 0)) / (np.max(X, axis = 0) + eps)
features = features.astype("float32")
features = scale(features)
print "escape time : ", round(time()-t0, 3), "s"
# scale the data to the range [0, 1] and then construct the training
# and testing splits
(trainX, testX, trainY, testY) = train_test_split(features, labels, test_size = 0.001)
(valX, testX, valY, testY) = train_test_split(testX, testY, test_size = 0.5)
# convert class vectors to binary class matrices
trainY = np_utils.to_categorical(trainY, nb_classes)
testY = np_utils.to_categorical(testY, nb_classes)
valY = np_utils.to_categorical(valY, nb_classes)
mm = model.fit(trainX, trainY,
batch_size=batch_size,
nb_epoch=100,
show_accuracy=True,
verbose=0,
validation_data=(testX, testY))
score = model.evaluate(valX, valY, show_accuracy=True, verbose=0, batch_size=32)
print 'Test score : %s' %score[0]
print 'Test accuracy : %s' %score[1]
df = pd.DataFrame(mm)
df.index = df['epoch']
df['acc'].plot()
print trainX.shape
In [106]:
features = joblib.load("./mldata/features_1200.mat")
labels = joblib.load("./mldata/lables_1200.mat")
features = np.array(features, 'int16')
labels = np.array(labels, 'int')
t0 = time()
def scale(X, eps = 0.001):
# scale the data points s.t the columns of the feature space
# (i.e the predictors) are within the range [0, 1]
return (X - np.min(X, axis = 0)) / (np.max(X, axis = 0) + eps)
features = features.astype("float32")
features = scale(features)
print "escape time : ", round(time()-t0, 3), "s"
# scale the data to the range [0, 1] and then construct the training
# and testing splits
(trainX, testX, trainY, testY) = train_test_split(features, labels, test_size = 0.2)
(valX, testX, valY, testY) = train_test_split(testX, testY, test_size = 0.5)
# convert class vectors to binary class matrices
trainY = np_utils.to_categorical(trainY, nb_classes)
testY = np_utils.to_categorical(testY, nb_classes)
valY = np_utils.to_categorical(valY, nb_classes)
mm = model.fit(trainX, trainY,
batch_size=batch_size,
nb_epoch=100,
show_accuracy=True,
verbose=0,
validation_data=(testX, testY))
score = model.evaluate(valX, valY, show_accuracy=True, verbose=0, batch_size=32)
print 'Test score : %s' %score[0]
print 'Test accuracy : %s' %score[1]
df = pd.DataFrame(mm)
df.index = df['epoch']
df['acc'].plot()
print trainX.shape
print testX.shape
print valX.shape
In [105]:
features = joblib.load("./mldata/features_1200.mat")
labels = joblib.load("./mldata/lables_1200.mat")
features = np.array(features, 'int16')
labels = np.array(labels, 'int')
t0 = time()
def scale(X, eps = 0.001):
# scale the data points s.t the columns of the feature space
# (i.e the predictors) are within the range [0, 1]
return (X - np.min(X, axis = 0)) / (np.max(X, axis = 0) + eps)
features = features.astype("float32")
features = scale(features)
print "escape time : ", round(time()-t0, 3), "s"
# scale the data to the range [0, 1] and then construct the training
# and testing splits
(trainX, testX, trainY, testY) = train_test_split(features, labels, test_size = 0.5)
(valX, testX, valY, testY) = train_test_split(testX, testY, test_size = 0.5)
print trainX.shape
print testX.shape
print valX.shape