Skip to content

Commit

Permalink
Merge pull request #21 from garg-saurav/master
Browse files Browse the repository at this point in the history
Fixed #1 Update architecture to resemble LeNet-5
  • Loading branch information
Aakriti28 authored Oct 14, 2020
2 parents 13454fc + 129726d commit 0cd85d8
Show file tree
Hide file tree
Showing 2 changed files with 233 additions and 1 deletion.
1 change: 0 additions & 1 deletion MNIST_CNN.ipynb

This file was deleted.

233 changes: 233 additions & 0 deletions TASK1_SG.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "MNIST_CNN.ipynb",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "3wF5wszaj97Y"
},
"source": [
"# MNIST using Neural Networks"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "nnrWf3PCEzXL"
},
"source": [
"Run the statement to import the TensorFlow library:"
]
},
{
"cell_type": "code",
"metadata": {
"id": "0trJmd6DjqBZ"
},
"source": [
"import tensorflow as tf"
],
"execution_count": 1,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "7NAbSZiaoJ4z"
},
"source": [
"Run the statement to load and prepare the [MNIST dataset](http://yann.lecun.com/exdb/mnist/) and to convert the samples from integers to floating-point numbers:"
]
},
{
"cell_type": "code",
"metadata": {
"id": "7FP5258xjs-v"
},
"source": [
"mnist = tf.keras.datasets.mnist\n",
"\n",
"(x_train, y_train), (x_test, y_test) = mnist.load_data()\n",
"x_train, x_test = x_train / 255.0, x_test / 255.0"
],
"execution_count": 2,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "BPZ68wASog_I"
},
"source": [
"Build the model with multiple convolution, pooling, dense layers and relu activation function:"
]
},
{
"cell_type": "code",
"metadata": {
"id": "h3IKyzTCDNGo"
},
"source": [
"model = tf.keras.models.Sequential([\n",
" tf.keras.layers.Reshape((28,28,1), input_shape=(28,28)),\n",
" tf.keras.layers.Conv2D(5, (5,5), activation='relu'),\n",
" tf.keras.layers.MaxPool2D((2,2)),\n",
" tf.keras.layers.Conv2D(15, (5,5), activation='relu'),\n",
" tf.keras.layers.MaxPool2D((2,2)), \n",
" tf.keras.layers.Flatten(),\n",
" tf.keras.layers.Dense(120, activation='relu'), \n",
" tf.keras.layers.Dense(80, activation='relu'), \n",
" tf.keras.layers.Dense(40, activation='relu'), \n",
" tf.keras.layers.Dense(10, activation='sigmoid')\n",
"])"
],
"execution_count": 3,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "hQyugpgRIyrA"
},
"source": [
"Choosing our loss function, in this case crossentropy:"
]
},
{
"cell_type": "code",
"metadata": {
"id": "RSkzdv8MD0tT"
},
"source": [
"loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)"
],
"execution_count": 4,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "9foNKHzTD2Vo"
},
"source": [
"model.compile(optimizer='adam',\n",
" loss=loss_fn,\n",
" metrics=['accuracy'])"
],
"execution_count": 5,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "ix4mEL65on-w"
},
"source": [
"Adjusting the model parameters to minimize the loss of our model: "
]
},
{
"cell_type": "code",
"metadata": {
"id": "y7suUbJXVLqP",
"outputId": "679a2a7d-90f2-4827-deb6-b950793b227e",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 217
}
},
"source": [
"model.fit(x_train, y_train, epochs=5)"
],
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"text": [
"Epoch 1/5\n",
"1875/1875 [==============================] - 5s 2ms/step - loss: 1.5402 - accuracy: 0.9171\n",
"Epoch 2/5\n",
"1875/1875 [==============================] - 4s 2ms/step - loss: 1.4864 - accuracy: 0.9688\n",
"Epoch 3/5\n",
"1875/1875 [==============================] - 5s 2ms/step - loss: 1.4800 - accuracy: 0.9761\n",
"Epoch 4/5\n",
"1875/1875 [==============================] - 5s 2ms/step - loss: 1.4767 - accuracy: 0.9798\n",
"Epoch 5/5\n",
"1875/1875 [==============================] - 4s 2ms/step - loss: 1.4751 - accuracy: 0.9813\n"
],
"name": "stdout"
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"<tensorflow.python.keras.callbacks.History at 0x7f1ec03ca630>"
]
},
"metadata": {
"tags": []
},
"execution_count": 6
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "4mDAAPFqVVgn"
},
"source": [
"Checking the model's performance on the Validation-set."
]
},
{
"cell_type": "code",
"metadata": {
"id": "F7dTAzgHDUh7",
"outputId": "8cc7a2b0-5cbd-49f1-cd9d-544a0d4e2e50",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 54
}
},
"source": [
"model.evaluate(x_test, y_test, verbose=2)"
],
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"text": [
"313/313 - 1s - loss: 1.4754 - accuracy: 0.9819\n"
],
"name": "stdout"
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[1.4754043817520142, 0.9818999767303467]"
]
},
"metadata": {
"tags": []
},
"execution_count": 7
}
]
}
]
}

0 comments on commit 0cd85d8

Please sign in to comment.