{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import keras\n",
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"from keras.models import Sequential\n",
"from keras.layers import Dense, Dropout, Flatten, Activation \n",
"from keras.layers import Conv2D, MaxPooling2D\n",
"from keras.optimizers import SGD, Adam\n",
"from keras.utils import np_utils\n",
"from keras.datasets import mnist"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"x_train shape: (60000, 784)\n60000 train samples\n10000 test samples\n"
]
}
],
"source": [
"(x_train, y_train), (x_test, y_test) = mnist.load_data()\n",
"x_train = x_train.reshape(x_train.shape[0], 28*28)\n",
"x_test = x_test.reshape(x_test.shape[0], 28*28)\n",
"\n",
"x_train = x_train.astype('float32')\n",
"x_test = x_test.astype('float32')\n",
"x_train /= 255 # normalize our data values to the range [0, 1]\n",
"x_test /= 255 #normalize our data values to the range [0, 1]\n",
"print('x_train shape:', x_train.shape)\n",
"print(x_train.shape[0], 'train samples')\n",
"print(x_test.shape[0], 'test samples')\n",
"\n",
"y_train = keras.utils.to_categorical(y_train, 10)\n",
"y_test = keras.utils.to_categorical(y_test, 10)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Model: \"sequential\"\n_________________________________________________________________\nLayer (type) Output Shape Param # \n=================================================================\ndense (Dense) (None, 500) 392500 \n_________________________________________________________________\ndense_1 (Dense) (None, 600) 300600 \n_________________________________________________________________\ndense_2 (Dense) (None, 600) 360600 \n_________________________________________________________________\ndense_3 (Dense) (None, 10) 6010 \n=================================================================\nTotal params: 1,059,710\nTrainable params: 1,059,710\nNon-trainable params: 0\n_________________________________________________________________\n"
]
}
],
"source": [
"model = Sequential()\n",
"model.add(Dense(input_dim=28 * 28, units=500, activation='sigmoid'))\n",
"model.add(Dense(units=600, activation='sigmoid'))\n",
"model.add(Dense(units=600, activation='sigmoid'))\n",
"model.add(Dense(units=10, activation='relu'))\n",
"model.summary()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Epoch 1/20\n",
"270/270 [==============================] - 6s 21ms/step - loss: 7.7299 - accuracy: 0.1188 - val_loss: 7.3150 - val_accuracy: 0.3998\n",
"Epoch 2/20\n",
"270/270 [==============================] - 4s 15ms/step - loss: nan - accuracy: 0.2602 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 3/20\n",
"270/270 [==============================] - 4s 15ms/step - loss: nan - accuracy: 0.1006 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 4/20\n",
"270/270 [==============================] - 4s 15ms/step - loss: nan - accuracy: 0.0989 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 5/20\n",
"270/270 [==============================] - 4s 15ms/step - loss: nan - accuracy: 0.0981 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 6/20\n",
"270/270 [==============================] - 4s 14ms/step - loss: nan - accuracy: 0.0981 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 7/20\n",
"270/270 [==============================] - 4s 16ms/step - loss: nan - accuracy: 0.0970 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 8/20\n",
"270/270 [==============================] - 4s 15ms/step - loss: nan - accuracy: 0.0989 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 9/20\n",
"270/270 [==============================] - 4s 16ms/step - loss: nan - accuracy: 0.0987 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 10/20\n",
"270/270 [==============================] - 4s 16ms/step - loss: nan - accuracy: 0.0987 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 11/20\n",
"270/270 [==============================] - 4s 16ms/step - loss: nan - accuracy: 0.0997 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 12/20\n",
"270/270 [==============================] - 4s 16ms/step - loss: nan - accuracy: 0.0994 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 13/20\n",
"270/270 [==============================] - 5s 17ms/step - loss: nan - accuracy: 0.1008 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 14/20\n",
"270/270 [==============================] - 4s 16ms/step - loss: nan - accuracy: 0.1004 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 15/20\n",
"270/270 [==============================] - 4s 16ms/step - loss: nan - accuracy: 0.1004 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 16/20\n",
"270/270 [==============================] - 4s 15ms/step - loss: nan - accuracy: 0.0971 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 17/20\n",
"270/270 [==============================] - 4s 15ms/step - loss: nan - accuracy: 0.0998 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 18/20\n",
"270/270 [==============================] - 4s 16ms/step - loss: nan - accuracy: 0.0992 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 19/20\n",
"270/270 [==============================] - 4s 16ms/step - loss: nan - accuracy: 0.0981 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 20/20\n",
"270/270 [==============================] - 4s 16ms/step - loss: nan - accuracy: 0.0984 - val_loss: nan - val_accuracy: 0.0978\n"
]
}
],
"source": [
"model.compile(loss='categorical_crossentropy',\n",
" optimizer='adam',\n",
" metrics=['accuracy'])\n",
"\n",
"history = model.fit(x_train, y_train, validation_split=0.1, batch_size = 200, epochs = 20, verbose = 1)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Total loss on Testing Set: nan\nAccuracy of Testing Set: 0.09799999743700027\n"
]
}
],
"source": [
"score = model.evaluate(x_test,y_test, verbose = 0)\n",
"print('Total loss on Testing Set:',score[0])\n",
"print('Accuracy of Testing Set:',score[1])"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": "