From c65d4bcbdc5c5627427387025c6812929aa93860 Mon Sep 17 00:00:00 2001 From: Eduardo Cueto Mendoza Date: Sat, 2 May 2020 15:40:17 -0600 Subject: [PATCH] [ADD] convolutional network example --- CNN - MNIST.ipynb | 159 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 CNN - MNIST.ipynb diff --git a/CNN - MNIST.ipynb b/CNN - MNIST.ipynb new file mode 100644 index 0000000..805d96e --- /dev/null +++ b/CNN - MNIST.ipynb @@ -0,0 +1,159 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import torch\n", + "import torch.nn as nn\n", + "import torchvision.transforms as transforms\n", + "import torchvision.datasets as datasets" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "mean_gray = 0.1307\n", + "stddev_gray = 0.3081\n", + "\n", + "# input[channel] = (input[channel]-meaan[channel]) / std[channel]\n", + "\n", + "transforms = transforms.Compose([transforms.ToTensor(),\n", + " transforms.Normalize((mean_gray,),(stddev_gray,))])\n", + "\n", + "train_dataset = datasets.MNIST(root='./data', \n", + " train=True, \n", + " transform=transforms, \n", + " download=True)\n", + "\n", + "test_dataset = datasets.MNIST(root='./data', \n", + " train=False, \n", + " transform=transforms)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + }, + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+j8jraAAANr0lEQVR4nO3db6xUdX7H8c9HujwRJFBSvLK0LqvGbGrKN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4mBEfBYRxyX9UA1+d8Uw489I+nFEbC0mN/7dTdZXr763JsL+qqQLbX/F9kxJ35b0XAN9fIHts4sDJ7J9tqTl6r+hqJ+TtLp4vlrSsw32cpJ+Gca7bJhxNfzdNT78eUT0/E/SCo0fkf9fSf/YRA8lfS2W9N/F35tN9yZpi8Y36/5P48c2viPpDyXtkPS2pP+QNK+Penta40N7v6HxYA001NsyjW+ivyFpV/G3ounvrqKvnnxvXC4LJMEBOiAJwg4kQdiBJAg7kARhB5Ig7EAShB1I4v8B1DNMfBo+lxwAAAAASUVORK5CYII=\n", + "text/plain": [ + "
" + ] + }, + "metadata": { + "needs_background": "light" + }, + "output_type": "display_data" + } + ], + "source": [ + "import matplotlib.pyplot as plt\n", + "random_img = train_dataset[20][0].numpy() * stddev_gray + mean_gray\n", + "plt.imshow(random_img.reshape(28,28), cmap='gray')" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "print(train_dataset[20][1])" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "batch_size = 100\n", + "\n", + "train_load = torch.utils.data.DataLoader(dataset=train_dataset,\n", + " batch_size=batch_size,\n", + " shuffle=True)\n", + "\n", + "test_load = torch.utils.data.DataLoader(dataset=test_dataset,\n", + " batch_size=batch_size,\n", + " shuffle=True)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "100" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(test_load)" + ] + }, + { + "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.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}