0% found this document useful (0 votes)
49 views

Keras - Datasets Keras - Datasets: "X - Train Shape" "Y - Train Shape" "X - Test Shape" "Y - Test Shape"

The document discusses loading and preprocessing the MNIST dataset for use in building and evaluating convolutional neural network (CNN) models for digit classification. It loads the MNIST dataset, preprocesses the images, defines a CNN model architecture, and evaluates the model using k-fold cross-validation. Key steps include reshaping the images, normalizing pixel values, one-hot encoding targets, defining layers like convolution and max pooling, compiling the model, and calculating accuracy scores in cross-validation to evaluate performance.

Uploaded by

P yeswanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Keras - Datasets Keras - Datasets: "X - Train Shape" "Y - Train Shape" "X - Test Shape" "Y - Test Shape"

The document discusses loading and preprocessing the MNIST dataset for use in building and evaluating convolutional neural network (CNN) models for digit classification. It loads the MNIST dataset, preprocesses the images, defines a CNN model architecture, and evaluates the model using k-fold cross-validation. Key steps include reshaping the images, normalizing pixel values, one-hot encoding targets, defining layers like convolution and max pooling, compiling the model, and calculating accuracy scores in cross-validation to evaluate performance.

Uploaded by

P yeswanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

In [ ]:

from keras.datasets import mnist

# loading the dataset


(X_train, y_train), (X_test, y_test) = mnist.load_data()
# let's print the shape of the dataset
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.n
pz
11493376/11490434 [==============================] - 0s 0us/step

In [ ]:

print("X_train shape", X_train.shape)


print("y_train shape", y_train.shape)
print("X_test shape", X_test.shape)
print("y_test shape", y_test.shape)

X_train shape (60000, 28, 28)


y_train shape (60000,)
X_test shape (10000, 28, 28)
y_test shape (10000,)

In [ ]:

# keras imports for the dataset and building our neural network
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Conv2D, MaxPool2D
from keras.utils import np_utils

# Flattening the images from the 28x28 pixels to 1D 787 pixels

X_train = X_train.reshape(60000, 784)


X_test = X_test.reshape(10000, 784)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')

# normalizing the data to help with the training


X_train /= 255
X_test /= 255

# one-hot encoding using keras' numpy-related utilities


n_classes = 10
print("Shape before one-hot encoding: ", y_train.shape)
Y_train = np_utils.to_categorical(y_train, n_classes)
Y_test = np_utils.to_categorical(y_test, n_classes)
print("Shape after one-hot encoding: ", Y_train.shape)

# building a linear stack of layers with the sequential model


model = Sequential()
# hidden layer
model.add(Dense(100, input_shape=(784,), activation='relu'))
# output layer
model.add(Dense(10, activation='softmax'))

# looking at the model summary


model.summary()
# compiling the sequential model
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')
# training the model for 10 epochs
model.fit(X_train, Y_train, batch_size=128, epochs=10, validation_data=(X_test, Y_test))

Shape before one-hot encoding: (60000,)


Shape after one-hot encoding: (60000, 10)
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 100) 78500
_________________________________________________________________
dense_1 (Dense) (None, 10) 1010
=================================================================
Total params: 79,510
Trainable params: 79,510
Non-trainable params: 0
_________________________________________________________________
Epoch 1/10
469/469 [==============================] - 3s 4ms/step - loss: 0.6531 - accuracy: 0.8241
- val_loss: 0.2182 - val_accuracy: 0.9370
Epoch 2/10
469/469 [==============================] - 2s 3ms/step - loss: 0.1921 - accuracy: 0.9468
- val_loss: 0.1536 - val_accuracy: 0.9550
Epoch 3/10
469/469 [==============================] - 2s 3ms/step - loss: 0.1383 - accuracy: 0.9596
- val_loss: 0.1233 - val_accuracy: 0.9653
Epoch 4/10
469/469 [==============================] - 2s 3ms/step - loss: 0.1038 - accuracy: 0.9712
- val_loss: 0.1067 - val_accuracy: 0.9681
Epoch 5/10
469/469 [==============================] - 2s 3ms/step - loss: 0.0847 - accuracy: 0.9758
- val_loss: 0.0993 - val_accuracy: 0.9690
Epoch 6/10
469/469 [==============================] - 2s 3ms/step - loss: 0.0675 - accuracy: 0.9810
- val_loss: 0.0902 - val_accuracy: 0.9726
Epoch 7/10
469/469 [==============================] - 2s 3ms/step - loss: 0.0595 - accuracy: 0.9829
- val_loss: 0.0865 - val_accuracy: 0.9736
Epoch 8/10
469/469 [==============================] - 2s 3ms/step - loss: 0.0505 - accuracy: 0.9859
- val_loss: 0.0800 - val_accuracy: 0.9755
Epoch 9/10
469/469 [==============================] - 2s 3ms/step - loss: 0.0440 - accuracy: 0.9871
- val_loss: 0.0804 - val_accuracy: 0.9775
Epoch 10/10
469/469 [==============================] - 2s 3ms/step - loss: 0.0389 - accuracy: 0.9895
- val_loss: 0.0775 - val_accuracy: 0.9752
Out[ ]:
<tensorflow.python.keras.callbacks.History at 0x7febadf5d350>

In [ ]:
# keras imports for the dataset and building our neural network
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Conv2D, MaxPool2D, Flatten
from keras.utils import np_utils

# to calculate accuracy
from sklearn.metrics import accuracy_score

# loading the dataset


(X_train, y_train), (X_test, y_test) = mnist.load_data()

# building the input vector from the 28x28 pixels


X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)
X_test = X_test.reshape(X_test.shape[0], 28, 28, 1)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')

# normalizing the data to help with the training


X_train /= 255
X_test /= 255

# one-hot encoding using keras' numpy-related utilities


n_classes = 10
print("Shape before one-hot encoding: ", y_train.shape)
Y_train = np_utils.to_categorical(y_train, n_classes)
Y_test = np_utils.to_categorical(y_test, n_classes)
print("Shape after one-hot encoding: ", Y_train.shape)

# building a linear stack of layers with the sequential model


model = Sequential()
# convolutional layer
model.add(Conv2D(25, kernel_size=(3,3), strides=(1,1), padding='valid', activation='relu
', input_shape=(28,28,1)))
model.add(MaxPool2D(pool_size=(1,1)))
# flatten output of conv
model.add(Flatten())
# hidden layer
model.add(Dense(100, activation='relu'))
# output layer
model.add(Dense(10, activation='softmax'))

# compiling the sequential model


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

# training the model for 10 epochs


model.fit(X_train, Y_train, batch_size=128, epochs=10, validation_data=(X_test, Y_test))

Shape before one-hot encoding: (60000,)


Shape after one-hot encoding: (60000, 10)
Epoch 1/10
469/469 [==============================] - 43s 90ms/step - loss: 0.3629 - accuracy: 0.897
2 - val_loss: 0.0689 - val_accuracy: 0.9773
Epoch 2/10
469/469 [==============================] - 42s 89ms/step - loss: 0.0604 - accuracy: 0.981
7 - val_loss: 0.0587 - val_accuracy: 0.9801
Epoch 3/10
469/469 [==============================] - 42s 89ms/step - loss: 0.0315 - accuracy: 0.990
5 - val_loss: 0.0543 - val_accuracy: 0.9804
Epoch 4/10
469/469 [==============================] - 42s 89ms/step - loss: 0.0198 - accuracy: 0.994
5 - val_loss: 0.0467 - val_accuracy: 0.9843
Epoch 5/10
469/469 [==============================] - 42s 89ms/step - loss: 0.0113 - accuracy: 0.996
8 - val_loss: 0.0545 - val_accuracy: 0.9825
Epoch 6/10
469/469 [==============================] - 42s 89ms/step - loss: 0.0096 - accuracy: 0.997
0 - val_loss: 0.0507 - val_accuracy: 0.9841
Epoch 7/10
469/469 [==============================] - 42s 89ms/step - loss: 0.0064 - accuracy: 0.998
0 - val_loss: 0.0597 - val_accuracy: 0.9846
Epoch 8/10
469/469 [==============================] - 42s 90ms/step - loss: 0.0048 - accuracy: 0.998
8 - val_loss: 0.0567 - val_accuracy: 0.9855
Epoch 9/10
469/469 [==============================] - 42s 89ms/step - loss: 0.0054 - accuracy: 0.998
5 - val_loss: 0.0592 - val_accuracy: 0.9855
Epoch 10/10
469/469 [==============================] - 41s 88ms/step - loss: 0.0037 - accuracy: 0.999
1 - val_loss: 0.0562 - val_accuracy: 0.9862
Out[ ]:
<tensorflow.python.keras.callbacks.History at 0x7feb9e7f2490>

In [ ]:
# https://machinelearningmastery.com/how-to-develop-a-convolutional-neural-network-from-s
cratch-for-mnist-handwritten-digit-classification/

from numpy import mean


from numpy import std
from matplotlib import pyplot
from sklearn.model_selection import KFold
from keras.datasets import mnist
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Dense
from keras.layers import Flatten
from keras.optimizers import SGD

# load train and test dataset


def load_dataset():
# load dataset
(trainX, trainY), (testX, testY) = mnist.load_data()
# reshape dataset to have a single channel
trainX = trainX.reshape((trainX.shape[0], 28, 28, 1))
testX = testX.reshape((testX.shape[0], 28, 28, 1))
# one hot encode target values
trainY = to_categorical(trainY)
testY = to_categorical(testY)
return trainX, trainY, testX, testY

# scale pixels
def prep_pixels(train, test):
# convert from integers to floats
train_norm = train.astype('float32')
test_norm = test.astype('float32')
# normalize to range 0-1
train_norm = train_norm / 255.0
test_norm = test_norm / 255.0
# return normalized images
return train_norm, test_norm

# define cnn model


def define_model():
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', input_
shape=(28, 28, 1)))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(100, activation='relu', kernel_initializer='he_uniform'))
model.add(Dense(10, activation='softmax'))
# compile model
opt = SGD(lr=0.01, momentum=0.9)
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
return model

# evaluate a model using k-fold cross-validation


def evaluate_model(dataX, dataY, n_folds=5):
scores, histories = list(), list()
# prepare cross validation
kfold = KFold(n_folds, shuffle=True, random_state=1)
# enumerate splits
for train_ix, test_ix in kfold.split(dataX):
# define model
model = define_model()
# select rows for train and test
trainX, trainY, testX, testY = dataX[train_ix], dataY[train_ix], dataX[test_ix], dataY
[test_ix]
# fit model
history = model.fit(trainX, trainY, epochs=10, batch_size=32, validation_data=(testX,
testY), verbose=0)
# evaluate model
_, acc = model.evaluate(testX, testY, verbose=0)
print('> %.3f' % (acc * 100.0))
# stores scores
scores.append(acc)
histories.append(history)
return scores, histories

# plot diagnostic learning curves


def summarize_diagnostics(histories):
for i in range(len(histories)):
# plot loss
pyplot.subplot(2, 1, 1)
pyplot.title('Cross Entropy Loss')
pyplot.plot(histories[i].history['loss'], color='blue', label='train')
pyplot.plot(histories[i].history['val_loss'], color='orange', label='test')
# plot accuracy
pyplot.subplot(2, 1, 2)
pyplot.title('Classification Accuracy')
pyplot.plot(histories[i].history['accuracy'], color='blue', label='train')
pyplot.plot(histories[i].history['val_accuracy'], color='orange', label='test')
pyplot.show()

# summarize model performance


def summarize_performance(scores):
# print summary
print('Accuracy: mean=%.3f std=%.3f, n=%d ' % (mean(scores)*100, std(scores)*100, len(sco
res)))
# box and whisker plots of results
pyplot.boxplot(scores)
pyplot.show()

# run the test harness for evaluating a model


def run_test_harness():
# load dataset
trainX, trainY, testX, testY = load_dataset()
# prepare pixel data
trainX, testX = prep_pixels(trainX, testX)
# evaluate model
scores, histories = evaluate_model(trainX, trainY)
# learning curves
summarize_diagnostics(histories)
# summarize estimated performance
summarize_performance(scores)

# entry point, run the test harness


run_test_harness()

> 98.458
> 98.558
> 98.642
> 98.800
> 98.675

/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:77: MatplotlibDeprecationWar
ning: Adding an axes using the same arguments as a previous axes currently reuses the ear
lier instance. In a future version, a new instance will always be created and returned.
Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a
unique label to each axes instance.
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:82: MatplotlibDeprecationWar
ning: Adding an axes using the same arguments as a previous axes currently reuses the ear
lier instance. In a future version, a new instance will always be created and returned.
Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a
unique label to each axes instance.

Accuracy: mean=98.627 std=0.115, n=5

You might also like