mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-24 05:21:09 +00:00
328 lines
18 KiB
Plaintext
328 lines
18 KiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Standard (Fully Connected) Neural Network"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 1,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"#Use in Markup cell type\n",
|
||
|
"#![alt text](imagename.png \"Title\") "
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"### Implementing Fully connected Neural Net"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"#### Loading Required packages and Data"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 2,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stderr",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"Using TensorFlow backend.\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"###1. Load Data and Splot Data\n",
|
||
|
"from keras.datasets import mnist\n",
|
||
|
"from keras.models import Sequential \n",
|
||
|
"from keras.layers.core import Dense, Activation\n",
|
||
|
"from keras.utils import np_utils\n",
|
||
|
"(X_train, Y_train), (X_test, Y_test) = mnist.load_data()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"#### Preprocessing"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 3,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"<Figure size 2000x400 with 10 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"import matplotlib.pyplot as plt\n",
|
||
|
"n = 10 # how many digits we will display\n",
|
||
|
"plt.figure(figsize=(20, 4))\n",
|
||
|
"for i in range(n):\n",
|
||
|
" # display original\n",
|
||
|
" ax = plt.subplot(2, n, i + 1)\n",
|
||
|
" plt.imshow(X_test[i].reshape(28, 28))\n",
|
||
|
" plt.gray()\n",
|
||
|
" ax.get_xaxis().set_visible(False)\n",
|
||
|
" ax.get_yaxis().set_visible(False)\n",
|
||
|
"plt.show()\n",
|
||
|
"plt.close()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 4,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"Previous X_train shape: (60000, 28, 28) \n",
|
||
|
"Previous Y_train shape:(60000,)\n",
|
||
|
"New X_train shape: (60000, 784) \n",
|
||
|
"New Y_train shape:(60000, 10)\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"print(\"Previous X_train shape: {} \\nPrevious Y_train shape:{}\".format(X_train.shape, Y_train.shape))\n",
|
||
|
"X_train = X_train.reshape(60000, 784) \n",
|
||
|
"X_test = X_test.reshape(10000, 784)\n",
|
||
|
"X_train = X_train.astype('float32') \n",
|
||
|
"X_test = X_test.astype('float32') \n",
|
||
|
"X_train /= 255 \n",
|
||
|
"X_test /= 255\n",
|
||
|
"classes = 10\n",
|
||
|
"Y_train = np_utils.to_categorical(Y_train, classes) \n",
|
||
|
"Y_test = np_utils.to_categorical(Y_test, classes)\n",
|
||
|
"print(\"New X_train shape: {} \\nNew Y_train shape:{}\".format(X_train.shape, Y_train.shape))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"#### Setting up parameters"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 5,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"input_size = 784\n",
|
||
|
"batch_size = 200 \n",
|
||
|
"hidden1 = 400\n",
|
||
|
"hidden2 = 20\n",
|
||
|
"epochs = 2"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"#### Building the FCN Model"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 6,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"_________________________________________________________________\n",
|
||
|
"Layer (type) Output Shape Param # \n",
|
||
|
"=================================================================\n",
|
||
|
"dense_1 (Dense) (None, 400) 314000 \n",
|
||
|
"_________________________________________________________________\n",
|
||
|
"dense_2 (Dense) (None, 20) 8020 \n",
|
||
|
"_________________________________________________________________\n",
|
||
|
"dense_3 (Dense) (None, 10) 210 \n",
|
||
|
"=================================================================\n",
|
||
|
"Total params: 322,230\n",
|
||
|
"Trainable params: 322,230\n",
|
||
|
"Non-trainable params: 0\n",
|
||
|
"_________________________________________________________________\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"###4.Build the model\n",
|
||
|
"model = Sequential() \n",
|
||
|
"model.add(Dense(hidden1, input_dim=input_size, activation='relu'))\n",
|
||
|
"# output = relu (dot (W, input) + bias)\n",
|
||
|
"model.add(Dense(hidden2, activation='relu'))\n",
|
||
|
"model.add(Dense(classes, activation='softmax')) \n",
|
||
|
"\n",
|
||
|
"# Compilation\n",
|
||
|
"model.compile(loss='categorical_crossentropy', \n",
|
||
|
" metrics=['accuracy'], optimizer='sgd')\n",
|
||
|
"model.summary()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"#### Training The Model"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 7,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"Epoch 1/10\n",
|
||
|
" - 12s - loss: 1.4482 - acc: 0.6251\n",
|
||
|
"Epoch 2/10\n",
|
||
|
" - 3s - loss: 0.6239 - acc: 0.8482\n",
|
||
|
"Epoch 3/10\n",
|
||
|
" - 3s - loss: 0.4582 - acc: 0.8798\n",
|
||
|
"Epoch 4/10\n",
|
||
|
" - 3s - loss: 0.3941 - acc: 0.8936\n",
|
||
|
"Epoch 5/10\n",
|
||
|
" - 3s - loss: 0.3579 - acc: 0.9011\n",
|
||
|
"Epoch 6/10\n",
|
||
|
" - 4s - loss: 0.3328 - acc: 0.9070\n",
|
||
|
"Epoch 7/10\n",
|
||
|
" - 3s - loss: 0.3138 - acc: 0.9118\n",
|
||
|
"Epoch 8/10\n",
|
||
|
" - 3s - loss: 0.2980 - acc: 0.9157\n",
|
||
|
"Epoch 9/10\n",
|
||
|
" - 3s - loss: 0.2849 - acc: 0.9191\n",
|
||
|
"Epoch 10/10\n",
|
||
|
" - 3s - loss: 0.2733 - acc: 0.9223\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"<keras.callbacks.History at 0x272375a7240>"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 7,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# Fitting on Data\n",
|
||
|
"model.fit(X_train, Y_train, batch_size=batch_size, epochs=10, verbose=2)\n",
|
||
|
"###5.Test "
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"collapsed": true
|
||
|
},
|
||
|
"source": [
|
||
|
"#### Testing The Model"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 8,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"10000/10000 [==============================] - 1s 121us/step\n",
|
||
|
"\n",
|
||
|
"Test accuracy: 0.9257\n",
|
||
|
"[0 6 9 0 1 5 9 7 3 4]\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAABHEAAABzCAYAAAAfb55ZAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAHPJJREFUeJzt3XeYVNUZx/GzgBAQEAQVLKAuoQkmNJUIrkCKi4jUUESIgLTE8AASejcohhIeJVIEgVCCNAF5gokoIKCIVKVbQFoiCIhU4XHzB+H1Pce9w+zsnZ25M9/PX7/rOdw5OtzZ2et9z5uSkZFhAAAAAAAAEN9yxXoBAAAAAAAAuDZu4gAAAAAAAAQAN3EAAAAAAAACgJs4AAAAAAAAAcBNHAAAAAAAgADgJg4AAAAAAEAAcBMHAAAAAAAgALiJAwAAAAAAEADcxAEAAAAAAAiAPFmZnJKSkhGthSC0jIyMFD/Ow3sYU8czMjJu8uNEvI+xw7WYELgWEwDXYkLgWkwAXIsJgWsxAXAtJoSwrkWexAFyzoFYLwCAMYZrEYgXXItAfOBaBOJDWNciN3EAAAAAAAACgJs4AAAAAAAAAcBNHAAAAAAAgADgJg4AAAAAAEAAcBMHAAAAAAAg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Nsn652KHDh2seYULF5bsliC7nSATjS4N9SqvyYz+TNXlT5MmTQrrz8+dO9c67tatm+f
|
||
|
"text/plain": [
|
||
|
"<Figure size 1440x288 with 10 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"score = model.evaluate(X_test, Y_test, verbose=1)\n",
|
||
|
"print('\\n''Test accuracy:', score[1])\n",
|
||
|
"mask = range(10,20)\n",
|
||
|
"X_valid = X_test[mask]\n",
|
||
|
"y_pred = model.predict_classes(X_valid)\n",
|
||
|
"print(y_pred)\n",
|
||
|
"plt.figure(figsize=(20, 4))\n",
|
||
|
"for i in range(n):\n",
|
||
|
" # display original\n",
|
||
|
" ax = plt.subplot(2, n, i + 1)\n",
|
||
|
" plt.imshow(X_valid[i].reshape(28, 28))\n",
|
||
|
" plt.gray()\n",
|
||
|
" ax.get_xaxis().set_visible(False)\n",
|
||
|
" ax.get_yaxis().set_visible(False)\n",
|
||
|
"plt.show()\n",
|
||
|
"plt.close()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": []
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"kernelspec": {
|
||
|
"display_name": "Python 3",
|
||
|
"language": "python",
|
||
|
"name": "python3"
|
||
|
},
|
||
|
"language_info": {
|
||
|
"codemirror_mode": {
|
||
|
"name": "ipython",
|
||
|
"version": 3
|
||
|
},
|
||
|
"file_extension": ".py",
|
||
|
"mimetype": "text/x-python",
|
||
|
"name": "python",
|
||
|
"nbconvert_exporter": "python",
|
||
|
"pygments_lexer": "ipython3",
|
||
|
"version": "3.6.6"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 2
|
||
|
}
|