{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "**Important: This notebook will only work with fastai-0.7.x. Do not try to run any fastai-1.x code from this path in the repository because it will load fastai-0.7.x**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using SGD on MNIST" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Background" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ... about machine learning (a reminder from lesson 1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The good news is that modern machine learning can be distilled down to a couple of key techniques that are of very wide applicability. Recent studies have shown that the vast majority of datasets can be best modeled with just two methods:\n", "\n", "1. Ensembles of decision trees (i.e. Random Forests and Gradient Boosting Machines), mainly for structured data (such as you might find in a database table at most companies). We looked at random forests in depth as we analyzed the Blue Book for Bulldozers dataset.\n", "\n", "2. Multi-layered neural networks learnt with SGD (i.e. shallow and/or deep learning), mainly for unstructured data (such as audio, vision, and natural language)\n", "\n", "In this lesson, we will start on the 2nd approach (a neural network with SGD) by analyzing the MNIST dataset. You may be surprised to learn that **logistic regression is actually an example of a simple neural net**!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### About The Data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this lesson, we will be working with MNIST, a classic data set of hand-written digits. Solutions to this problem are used by banks to automatically recognize the amounts on checks, and by the postal service to automatically recognize zip codes on mail." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A matrix can represent an image, by creating a grid where each entry corresponds to a different pixel.\n", "\n", "\"digit\"\n", " (Source: [Adam Geitgey\n", "](https://medium.com/@ageitgey/machine-learning-is-fun-part-3-deep-learning-and-convolutional-neural-networks-f40359318721))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Imports and data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will be using the fastai library, which is still in pre-alpha. If you are accessing this course notebook, you probably already have it downloaded, as it is in the same Github repo as the course materials.\n", "\n", "We use [symbolic links](https://kb.iu.edu/d/abbe) (often called *symlinks*) to make it possible to import these from your current directory. For instance, I ran:\n", "\n", " ln -s ../../fastai\n", " \n", "in the terminal, within the directory I'm working in, `home/fastai/courses/ml1`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%load_ext autoreload\n", "%autoreload 2\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from fastai.imports import *\n", "from fastai.torch_imports import *\n", "from fastai.io import *" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "path = 'data/mnist/'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's download, unzip, and format the data." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "os.makedirs(path, exist_ok=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "URL='http://deeplearning.net/data/mnist/'\n", "FILENAME='mnist.pkl.gz'\n", "\n", "def load_mnist(filename):\n", " return pickle.load(gzip.open(filename, 'rb'), encoding='latin-1')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "get_data(URL+FILENAME, path+FILENAME)\n", "((x, y), (x_valid, y_valid), _) = load_mnist(path+FILENAME)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(numpy.ndarray, (50000, 784), numpy.ndarray, (50000,))" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(x), x.shape, type(y), y.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Normalize" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Many machine learning algorithms behave better when the data is *normalized*, that is when the mean is 0 and the standard deviation is 1. We will subtract off the mean and standard deviation from our training set in order to normalize the data:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mean = x.mean()\n", "std = x.std()\n", "\n", "x=(x-mean)/std\n", "mean, std, x.mean(), x.std()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that for consistency (with the parameters we learn when training), we subtract the mean and standard deviation of our training set from our validation set. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(-0.0058509219, 0.99243325)" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x_valid = (x_valid-mean)/std\n", "x_valid.mean(), x_valid.std()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Look at the data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In any sort of data science work, it's important to look at your data, to make sure you understand the format, how it's stored, what type of values it holds, etc. To make it easier to work with, let's reshape it into 2d images from the flattened 1d format." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Helper methods" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def show(img, title=None):\n", " plt.imshow(img, cmap=\"gray\")\n", " if title is not None: plt.title(title)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def plots(ims, figsize=(12,6), rows=2, titles=None):\n", " f = plt.figure(figsize=figsize)\n", " cols = len(ims)//rows\n", " for i in range(len(ims)):\n", " sp = f.add_subplot(rows, cols, i+1)\n", " sp.axis('Off')\n", " if titles is not None: sp.set_title(titles[i], fontsize=16)\n", " plt.imshow(ims[i], cmap='gray')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Plots " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(10000, 784)" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x_valid.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(10000, 28, 28)" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x_imgs = np.reshape(x_valid, (-1,28,28)); x_imgs.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAP8AAAEICAYAAACQ6CLfAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAADl1JREFUeJzt3X3IXHV6xvHr8mUxMaLRVE1iNLtPhL5htAapKEVd3Nqt\nEFdwMWBJoyVrWaGrVSpBUBTBlu5qK1SJGMzirltN3FVWRcXa+gZifGGNGzer4saYJ09Qi4mou43e\n/eM5WR7jzG8mM2fmTHJ/P/AwM+eeM+dmyJVzzvzOzM8RIQD57Nd0AwCaQfiBpAg/kBThB5Ii/EBS\nhB9IivADSRF+tGT7btvjtrfb3mj775ruCfUyF/mgFdt/IumNiPit7T+U9N+S/joiXmy2M9SFPT9a\niojXIuK3ux5Wf2MNtoSaEX60Zfs/bH8s6XVJ45Iebrgl1IjDfhTZ3l/SqZLOkPTPEfF/zXaEurDn\nR1FEfBYRz0g6RtLfN90P6kP40a0DxDn/PoXw40tsH2n7QtszbO9v+y8lLZH0X033hvpwzo8vsf0H\nktZIWqjJHcRvJP17RNzRaGOoFeEHkuKwH0iK8ANJEX4gKcIPJHXAMDdmm08XgQGLCHfzvL72/LbP\nsf0r22/Yvrqf1wIwXD0P9VXXfG+UdLakzZJekLQkIn5ZWIc9PzBgw9jzn6LJ73u/FRG/k/QTSYv7\neD0AQ9RP+OdKemfK483Vsi+wvdz2Otvr+tgWgJr184Ffq0OLLx3WR8RKSSslDvuBUdLPnn+zpHlT\nHh8jaUt/7QAYln7C/4Kk421/1fZXJF0o6cF62gIwaD0f9kfETtuXSXpU0v6SVkXEa7V1BmCghvqt\nPs75gcEbykU+APZehB9IivADSRF+ICnCDyRF+IGkCD+QFOEHkiL8QFKEH0iK8ANJEX4gKcIPJEX4\ngaQIP5AU4QeSIvxAUoQfSIrwA0kRfiApwg8kNdQputGbhQsXFuuXX35529rY2Fhx3enTpxfrK1as\nKNYPPfTQYv2RRx5pW9uxY0dx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03t+AA0h/EBSjYTf9jm2f2X7DdtX\nN9FDO7bftv1qNe14o/MLVnMgbrO9fsqyw20/bvvX1W3LORIb6m0kpm0vTCvf6Hs3atPdD/2c3/b+\nkjZKOlvSZkkvSFoSEb8caiNt2H5b0qKIaPyCENt/IekjST/cNRWa7X+R9EFE3FT9xzkzIv5pRHq7\nTns4bfuAems3rfzfqsH3rs7p7uvQxJ7/FElvRMRbEfE7ST+RtLiBPkZeRDwl6YPdFi+WtLq6v1qT\n/3iGrk1vIyEixiPiper+Dkm7ppVv9L0r9NWIJsI/V9I7Ux5vVoNvQAsh6THbL9pe3nQzLRy1a1q0\n6vbIhvvZXcdp24dpt2nlR+a962W6+7o1Ef5WUwmN0njjaRHxZ5L+StJ3q8NbdOc2SWOanMNxXNL3\nm2ymmlZ+raTvRcT2JnuZqkVfjbxvTYR/s6R5Ux4fI2lLA320FBFbqtttkn6qydOUUTKxa4bk6nZb\nw/38XkRMRMRnEfG5pDvU4HtXTSu/VtKPIuL+anHj712rvpp635oI/wuSjrf9VdtfkXShpAcb6ONL\nbB9cfRAj2wdL+oZGb+rxByUtre4vlfRAg718wahM295uWnk1/N6N2nT3jVzhVw1l3CJpf0mrIuLG\noTfRgu2vaXJvL01+3fnHTfZm+x5JZ2jyK58Tkq6V9DNJ90o6VtImSRdExNA/eGvT2xnaw2nbB9Rb\nu2nln1eD712d093X0g+X9wI5cYUfkBThB5Ii/EBShB9IivADSRF+ICnCDyT1/x2VQ9c6BSuMAAAA\nAElFTkSuQmCC\n", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show(x_imgs[0], y_valid[0])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(10000,)" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y_valid.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It's the digit 3! And that's stored in the y value:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y_valid[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can look at part of an image:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-0.42452, -0.42452, -0.42452, -0.42452, 0.17294],\n", " [-0.42452, -0.42452, -0.42452, 0.78312, 2.43567],\n", " [-0.42452, -0.27197, 1.20261, 2.77889, 2.80432],\n", " [-0.42452, 1.76194, 2.80432, 2.80432, 1.73651],\n", " [-0.42452, 2.20685, 2.80432, 2.80432, 0.40176]], dtype=float32)" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x_imgs[0,10:15,10:15]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPgAAAD8CAYAAABaQGkdAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAACPxJREFUeJzt3UGIVYUex/Hfz3lKgg9aPBfhyDOi4knwFEQCd9LCMgpc\nKRQtgtm8wCCMAjfRPtrUYqhIKIqoFhE9QiiJoFepWeSbCokeSYE+MqpNMvV/i7kL6TneM95z5tzz\n4/uBgbl6uP6K+c65987ljKtKADKt6XsAgO4QOBCMwIFgBA4EI3AgGIEDwQgcCEbgQDACB4L9qYs7\ntc3b4zA469ev73tCYxcvXtTi4qLHHddJ4MAQ3XTTTX1PaOyrr75qdBwP0YFgBA4EI3AgGIEDwQgc\nCEbgQDACB4IROBCMwIFgBA4EI3AgGIEDwQgcCEbgQDACB4IROBCsUeC299j+0vYZ2490PQpAO8YG\nbntG0lOSbpe0VdIB21u7HgZgck3O4Dslnamqr6vqoqSXJd3d7SwAbWgS+CZJ315y++zozwBMuSYX\nXbzclRv/76qptuckzU28CEBrmgR+VtLmS27PSvrujwdV1bykeYnLJgPToslD9I8l3Wj7etvrJO2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1cvT5z1r6QtzU76rLqyW/jG6uHX1M9QtItmcl7ZX0TF8b\nhhD4JknfXnL7rKb0i3DIbG+RtF3Sh/0uWd7o4e4pSeckHa2qqd068qSkhyX93teAIQTuy/zZVH/n\nHhrbGyS9JunBqvqp7z3LqarfqmqbpFlJO23f0vem5di+U9K5qjrR544hBH5W0uZLbs9K+q6nLXFs\nr9VS3C9W1et972miqn7U0m+5nebXOnZJusv2N1p6Wrnb9gurPWIIgX8s6Ubb19teJ2m/pDd63hTB\ntiU9K2mhqp7oe8+V2N5o+9rR5+sl3Sbpi35XLa+qHq2q2araoqWv2Xeq6p7V3jH1gVfVoqQHJL2t\npReBXqmq0/2uWp7tlyR9IOlm22dt39/3pivYJeleLZ1dTo0+7uh71DKuk/Su7c+09E3/aFX18qOn\nIeGdbECwqT+DA7h6BA4EI3AgGIEDwQgcCEbgQDACB4IROBDsf/DAx4Xphc/GAAAAAElFTkSuQmCC\n", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "show(x_imgs[0,10:15,10:15])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAsMAAAF0CAYAAADGqzQSAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xu4lnPa//Hzq1raktCWSkU9QiV7a2ZsSqWO0siMyNCO\nEkY2ox0ifjFjVERmJrIrtLfJblI26UGm6FdKZSZFO0raqrVa398f93qOp1/nebeu1X2v+1r3+r5f\nx9ERH9fmxLWudXat+7y+znsvAAAAQIgOi7sAAAAAIC40wwAAAAgWzTAAAACCRTMMAACAYNEMAwAA\nIFg0wwAAAAgWzXDMnHPtnHNznHMbnHN7nHPfOecmO+dOjrs24GCcc+c75951zm1yzm1zzi10zvWK\nuy4gCufcpc65D51zOwqv38+dcxfFXRdwMM65C51z85xzu51zW5xzLzjnasVdV7ajGY5fDRH5l4jc\nJCKXi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2PmEyZMUFn37t1VNmfOHJW1adMm9cLKEO65yFYM\n0AEAAABFoBkGAABAsGiGAQAAECyaYQAAAASLAboscMwxx6jszjvvNLe18p49e6rsueeeS72wmDDM\nUTwLFy5UWYsWLVQ2e/Zsc//bb79dZT/++KPKrBXkiuPYY49V2VtvvaWypk2bqmzQoEEqe+KJJ1Kq\npyQwQJd+Q4cONfMRI0ao7MUXX1RZr169VJafn596YWUI99zU1alTR2U33nijua2V5+XlqcxaBffB\nBx9UmfU9QERk7dq1Zl6WMEAHAAAAFIFmGAAAAMGiGQYAAECwaIYBAAAQLJphAAAABIu3SWSpzp07\nm7m1zO5jjz2msltvvTXtNWUKk83JWRPL33//vcomT56ssquvvto85r59+1Iv7BBNnDhRZVdeeaXK\nWrdurbIvvviiRGpKBW+TSM0ZZ5yhsnnz5pnbrl69WmXNmzdXWZzXd7bgnls8jRo1Utm4ceNU1rZt\n20yUI3v27DHz888/X2XJ3jyRrXibBAAAAFAEmmEAAAAEi2YYAAAAwaIZBgAAQLDKx10AinbUUUep\nbMiQIZH3r1u3bjrLQSnWsmVLlTmn5wfWrVunsrgHic455xyVde/eXWVz585VmfXvXRoH6BDdYYfp\nZzXWsts5OTnm/q+//rrK4r7GUfbUq1dPZUuWLFFZ+fK63Ro1apR5zMcffzzSeZo1a6ayv/zlLyqr\nXr26eR5rkNq6D//444/m/mUJT4YBAAAQLJphAAAABItmGAAAAMGiGQYAAECwWIGulGnRooXKpkyZ\norImTZqY+69YsUJl1io3a9euPYTqSgdWQyqegoIClW3atEllZ511lrn/mjVr0lpPtWrVzHz+/Pkq\nW7lypcqslfKsFZ+WLl16CNWVLFagiy7qaorJ3HLLLSobO3ZsSjWFintucmPGjFFZv379VNa3b1+V\nPf/882mvZ8CAASobPXq0uW25cuVUtnz5cpVZQ3Xbtm07hOoyjxXoAAAAgCLQDAMAACBYNMMAAAAI\nFs0wAAAAgsUAXYyuvfZald1///0qO/7441W2e/du85idOnVSmbViVzZjmKN4hg8frrK7775bZ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DEXqVzzpw5KqtXr57KrGE5axVbEZE6deqozLp2X331VXP/soQnwwAAAAgWzTAAAACC\nRTMMAACAYNEMAwAAIFjOe5+5kzmXuZPFpFKlSmb+4osvqqx169Yqi7oS04YNG8y8Z8+eKnvnnXci\nHTNbeO9dps8ZwrWbTIcOHVQ2Y8YMlVWoUCHS8fbu3WvmN954o8omTJgQ6ZjZItPXbrZct7NmzVJZ\nq1atVFa3bt1MlGOusJhsgM66t48ZM0Zla9asUZk1QHXqqaea54lz9T3uuclZ3/NHjhypsltuuSWl\n82zZskVlNWrUSOmYliuuuEJl1kBftoh67fJkGAAAAMGiGQYAAECwaIYBAAAQLJphAAAABItmGAAA\nAMFiOeY02717t5lfffXVKitfXv/nT7Yk7oFq165t5taU/2233aayp556KtJ5gAsvvFBl1ltounbt\nqjJrAtpavlTEXnb8xx9/VNnrr79u7o/sdfbZZ6ss2VtHSpt169ap7LjjjlPZ3//+d5WdfvrpKitr\nb/8p66zv+dbbSCZPnqwyqy9IplatWpG2y8vLU5n19SUicsIJJ6hs165dkWsqS3gyDAAAgGDRDAMA\nACBYNMMAAAAIFs0wAAAAgsVyzKXMaaedprJRo0apzBpqSsZaBrRhw4bFqqs0YWnQkmFdeyIiCxYs\nUJk17GYNjVis5T5FRJ5++mmVOaf/Vzdv3lxl1jVeGrEcs80aLuvUqZPKSmI5Zusas67lv/71rymd\nx/pe++STT6psyJAh5v7bt29P6fyp4J6b3V544QUztwb42rdvr7J333037TVlCssxAwAAAEWgGQYA\nAECwaIYBAAAQLJphAAAABIsV6CKqXLmyykpipZbFixerrFu3bip75plnzP27dOmisvr166usTp06\nKlu/fn2UElFGVatWzcytlRKnTp16yOeZMmWKmTdo0EBlDz/8sMpat26tsmwZoEN01atXV5k1CPTi\niy+a+1vX7ZVXXqmyGjVqqKxDhw5RShQRkZ07d6ps3rx5Kvvzn/+ssrlz50Y+D5AJjRs3jruEWPBk\nGAAAAMGiGQYAAECwaIYBAAAQLJphAAAABIsBOoP1AXJrI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c+yhnuuSMOGDc383//+t8peeOEFlf3p\nT39S2caNG1OuK92sAbr7779fZY0aNVLZ6tWrS6KklDBABwAAABSBZhgAAADBohkGAABAsGiGAQAA\nECwG6FLQrl07lT388MPmtqeddlqkY+7evVtlffr0UdmaNWvM/a0PsOfm5qpszJgxKrNW3nvppZfM\n8/Ts2dPMM4FhjuzRunVrlSUbOGKALv1Cvm4vu+wylU2bNu2Qj2fdh0VEJkyYcMjHzBbcc0Xq1q1r\n5tbKq9Yw8sqVK1XWr18/85gfffSRyvLz84sqsdi6du2qskceeURlOTk5KjvllFNU9vPPP6ensDRi\ngA4AAAAoAs0wAAAAgkUzDAAAgGDRDAMAACBYDNClWbIVjnr16qUya0Wao446Ku01WawP41ur3913\n332ZKKdYGObIHsccc4zKli9fbm67b98+lZ100kkqK41DGlExQJd+/fv3N/Mnnngired59tlnzdy6\nt5c13HOT69atm8omT56c0jGtlemsXu3VV19VWZcuXSKfp0aNGiqzhuUeeOABld1zzz2RzxMnBugA\nAACAItAMAwAAIFg0wwAAAAgWzTAAAACCRTMMAACAYPE2iRhZk5zWZLQ1rdqiRYvI51m7dq3Knnrq\nKZWNHDky8jHjxGRzdrOWXRYROffcc1VmLYG6fv36tNeUKbxNIjprufvBgwer7Ne//rW5f7q/t910\n001mPm7cuLSepzTinptcuXLlVNa+fXuVDRo0SGWpLjfvnP7fkup1P378eJUNHTpUZT/88ENK58kU\n3iYBAAAAFIFmGAAAAMGiGQYAAECwaIYBAAAQLAbokHUY5shuAwcONPNHH31UZZdddpnKrCVIs0Xo\nA3QdOnQw8+uvv15l1hCStVSsNUQkEn2QaMSIESpbuHChyl577bVIxyuLuOem7rDD9LPHs846y9zW\nGpo/77zzVHbOOeeobO/evSqbMmWKeZ4xY8aozLr2CwoKzP2zAQN0AAAAQBFohgEAABAsmmEAAAAE\ni2YYAAAAwWKADlmHYY7sdvbZZ5v5J598orL3339fZRdeeGG6S8qYkAbo+vTpo7Jkq1xaq3Fatm7d\nqrJ58+aZ23755Zcqmz59usoWL16ssmweGCoJ3HORrRigAwAAAIpAMwwAAIBg0QwDAAAgWDTDAAAA\nCBYDdMg6DHMgW4U0QGetmNWxY0dz21mzZkU65qZNm1S2atWq4hWGYuOei2zFAB0AAABQBJphAAAA\nBItmGAAAAMGiGQYAAECwaIYBAAAQLN4mgazDZDOyVUhvk0DZwT0X2Yq3SQAAAABFoBkGAABAsGiG\nAQAAECyaYQAAAASLZhgAAADBohkGAABAsGiGAQAAECyaYQAAAASLZhgAAADByugKdAAAAEBpwpNh\nAAAABItmGAAAAMGiGQYAAECwaIYBAAAQLJphAAAABItmGAAAAMGiGQYAAECwaIYBAAAQLJphAAAA\nBItmGAAAAMGiGQYAAECwaIYBAAAQLJphAAAABItmGAAAAMGiGQYAAECwaIYBAAAQLJphAAAABItm\nGAAAAMGiGQYAAECwaIYBAAAQLJphAAAABItmGAAAAMGiGQYAAECw/h+g8MKF0kXt3QAAAABJRU5E\nrkJggg==\n", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "plots(x_imgs[:8], titles=y_valid[:8])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Neural Networks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will take a deep look *logistic regression* and how we can program it ourselves. We are going to treat it as a specific example of a shallow neural net." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**What is a neural network?**\n", "\n", "A *neural network* is an *infinitely flexible function*, consisting of *layers*. A *layer* is a linear function such as matrix multiplication followed by a non-linear function (the *activation*).\n", "\n", "One of the tricky parts of neural networks is just keeping track of all the vocabulary! " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Functions, parameters, and training" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A **function** takes inputs and returns outputs. For instance, $f(x) = 3x + 5$ is an example of a function. If we input $2$, the output is $3\\times 2 + 5 = 11$, or if we input $-1$, the output is $3\\times -1 + 5 = 2$\n", "\n", "Functions have **parameters**. The above function $f$ is $ax + b$, with parameters a and b set to $a=3$ and $b=5$.\n", "\n", "Machine learning is often about learning the best values for those parameters. For instance, suppose we have the data points on the chart below. What values should we choose for $a$ and $b$?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the above gif from fast.ai's deep learning course, [intro to SGD notebook](https://github.com/fastai/courses/blob/master/deeplearning1/nbs/sgd-intro.ipynb)), an algorithm called stochastic gradient descent is being used to learn the best parameters to fit the line to the data (note: in the gif, the algorithm is stopping before the absolute best parameters are found). This process is called **training** or **fitting**.\n", "\n", "Most datasets will not be well-represented by a line. We could use a more complicated function, such as $g(x) = ax^2 + bx + c + \\sin d$. Now we have 4 parameters to learn: $a$, $b$, $c$, and $d$. This function is more flexible than $f(x) = ax + b$ and will be able to accurately model more datasets.\n", "\n", "Neural networks take this to an extreme, and are infinitely flexible. They often have thousands, or even hundreds of thousands of parameters. However the core idea is the same as above. The neural network is a function, and we will learn the best parameters for modeling our data." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### PyTorch" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will be using the open source [deep learning library, fastai](https://github.com/fastai/fastai), which provides high level abstractions and best practices on top of PyTorch. This is the highest level, simplest way to get started with deep learning. Please note that fastai requires Python 3 to function. It is currently in pre-alpha, so items may move around and more documentation will be added in the future.\n", "\n", "The fastai deep learning library uses [PyTorch](http://pytorch.org/), a Python framework for dynamic neural networks with GPU acceleration, which was released by Facebook's AI team.\n", "\n", "PyTorch has two overlapping, yet distinct, purposes. As described in the [PyTorch documentation](http://pytorch.org/tutorials/beginner/blitz/tensor_tutorial.html):\n", "\n", "\"pytorch\"\n", "\n", "The neural network functionality of PyTorch is built on top of the Numpy-like functionality for fast matrix computations on a GPU. Although the neural network purpose receives way more attention, both are very useful. We'll implement a neural net from scratch today using PyTorch.\n", "\n", "**Further learning**: If you are curious to learn what *dynamic* neural networks are, you may want to watch [this talk](https://www.youtube.com/watch?v=Z15cBAuY7Sc) by Soumith Chintala, Facebook AI researcher and core PyTorch contributor.\n", "\n", "If you want to learn more PyTorch, you can try this [introductory tutorial](http://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html) or this [tutorial to learn by examples](http://pytorch.org/tutorials/beginner/pytorch_with_examples.html)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### About GPUs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Graphical processing units (GPUs) allow for matrix computations to be done with much greater speed, as long as you have a library such as PyTorch that takes advantage of them. Advances in GPU technology in the last 10-20 years have been a key part of why neural networks are proving so much more powerful now than they did a few decades ago. \n", "\n", "You may own a computer that has a GPU which can be used. For the many people that either don't have a GPU (or have a GPU which can't be easily accessed by Python), there are a few differnt options:\n", "\n", "- **Don't use a GPU**: For the sake of this tutorial, you don't have to use a GPU, although some computations will be slower.\n", "- **Use crestle, through your browser**: [Crestle](https://www.crestle.com/) is a service that gives you an already set up cloud service with all the popular scientific and deep learning frameworks already pre-installed and configured to run on a GPU in the cloud. It is easily accessed through your browser. New users get 10 hours and 1 GB of storage for free. After this, GPU usage is 34 cents per hour. I recommend this option to those who are new to AWS or new to using the console.\n", "- **Set up an AWS instance through your console**: You can create an AWS instance with a GPU by following the steps in this [fast.ai setup lesson](http://course.fast.ai/lessons/aws.html).] AWS charges 90 cents per hour for this." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Neural Net for Logistic Regression in PyTorch" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from fastai.metrics import *\n", "from fastai.model import *\n", "from fastai.dataset import *\n", "\n", "import torch.nn as nn" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will begin with the highest level abstraction: using a neural net defined by PyTorch's Sequential class. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "net = nn.Sequential(\n", " nn.Linear(28*28, 100),\n", " nn.ReLU(),\n", " nn.Linear(100, 100),\n", " nn.ReLU(),\n", " nn.Linear(100, 10),\n", " nn.LogSoftmax()\n", ").cuda()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Each input is a vector of size `28*28` pixels and our output is of size `10` (since there are 10 digits: 0, 1, ..., 9). \n", "\n", "We use the output of the final layer to generate our predictions. Often for classification problems (like MNIST digit classification), the final layer has the same number of outputs as there are classes. In that case, this is 10: one for each digit from 0 to 9. These can be converted to comparative probabilities. For instance, it may be determined that a particular hand-written image is 80% likely to be a 4, 18% likely to be a 9, and 2% likely to be a 3." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "md = ImageClassifierData.from_arrays(path, (x,y), (x_valid, y_valid))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "loss=nn.NLLLoss()\n", "metrics=[accuracy]\n", "# opt=optim.SGD(net.parameters(), 1e-1, momentum=0.9)\n", "opt=optim.SGD(net.parameters(), 1e-1, momentum=0.9, weight_decay=1e-3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Loss functions and metrics" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In machine learning the **loss** function or cost function is representing the price paid for inaccuracy of predictions.\n", "\n", "The loss associated with one example in binary classification is given by:\n", "`-(y * log(p) + (1-y) * log (1-p))`\n", "where `y` is the true label of `x` and `p` is the probability predicted by our model that the label is 1." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def binary_loss(y, p):\n", " return np.mean(-(y * np.log(p) + (1-y)*np.log(1-p)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.164252033486018" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "acts = np.array([1, 0, 0, 1])\n", "preds = np.array([0.9, 0.1, 0.2, 0.8])\n", "binary_loss(acts, preds)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that in our toy example above our accuracy is 100% and our loss is 0.16. Compare that to a loss of 0.03 that we are getting while predicting cats and dogs. Exercise: play with `preds` to get a lower loss for this example. \n", "\n", "**Example:** Here is an example on how to compute the loss for one example of binary classification problem. Suppose for an image x with label 1 and your model gives it a prediction of 0.9. For this case the loss should be small because our model is predicting a label $1$ with high probability.\n", "\n", "`loss = -log(0.9) = 0.10`\n", "\n", "Now suppose x has label 0 but our model is predicting 0.9. In this case our loss is should be much larger.\n", "\n", "`loss = -log(1-0.9) = 2.30`\n", "\n", "- Exercise: look at the other cases and convince yourself that this make sense.\n", "- Exercise: how would you rewrite `binary_loss` using `if` instead of `*` and `+`?\n", "\n", "Why not just maximize accuracy? The binary classification loss is an easier function to optimize.\n", "\n", "For multi-class classification, we use *negative log liklihood* (also known as *categorical cross entropy*) which is exactly the same thing, but summed up over all classes." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Fitting the model" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Fitting* is the process by which the neural net learns the best parameters for the dataset." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "2869810cce3b487aa6ad0a0ebca20426", "version_major": 2, "version_minor": 0 }, "text/plain": [ "A Jupyter Widget" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "[ 0. 0.26978 0.25134 0.93501] \n", "[ 1. 0.26613 0.22294 0.93909] \n", "[ 2. 0.21344 0.21292 0.94865] \n", "[ 3. 0.19153 0.20118 0.9582 ] \n", "[ 4. 0.20079 0.34337 0.9377 ] \n", "\n" ] } ], "source": [ "fit(net, md, n_epochs=5, crit=loss, opt=opt, metrics=metrics)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "set_lrs(opt, 1e-2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "564018b309a9494fba10c8e89873cd0b", "version_major": 2, "version_minor": 0 }, "text/plain": [ "A Jupyter Widget" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "[ 0. 0.07224 0.13539 0.97024] \n", "[ 1. 0.05924 0.13015 0.97114] \n", "[ 2. 0.04723 0.1316 0.97124] \n", "\n" ] } ], "source": [ "fit(net, md, n_epochs=3, crit=loss, opt=opt, metrics=metrics)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "ea801e9b44b74358ab227a9c485a69e8", "version_major": 2, "version_minor": 0 }, "text/plain": [ "A Jupyter Widget" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "[ 0. 0.27253 0.21465 0.93939] \n", "[ 1. 0.21963 0.23439 0.93481] \n", "[ 2. 0.23288 0.18333 0.94705] \n", "[ 3. 0.19822 0.1902 0.94636] \n", "[ 4. 0.205 0.27594 0.92227] \n", "\n" ] } ], "source": [ "fit(net, md, n_epochs=5, crit=loss, opt=opt, metrics=metrics)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "set_lrs(opt, 1e-2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "6e3074b642a64df0894778909923b943", "version_major": 2, "version_minor": 0 }, "text/plain": [ "A Jupyter Widget" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "[ 0. 0.07269 0.09255 0.97373] \n", "[ 1. 0.06316 0.08361 0.97572] \n", "[ 2. 0.04525 0.08077 0.97681] \n", "\n" ] } ], "source": [ "fit(net, md, n_epochs=3, crit=loss, opt=opt, metrics=metrics)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "([78400, 100, 10000, 100, 1000, 10], 89610)" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = [o.numel() for o in net.parameters()]\n", "t, sum(t)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "GPUs are great at handling lots of data at once (otherwise don't get performance benefit). We break the data up into **batches**, and that specifies how many samples from our dataset we want to send to the GPU at a time. The fastai library defaults to a batch size of 64. On each iteration of the training loop, the error on 1 batch of data will be calculated, and the optimizer will update the parameters based on that.\n", "\n", "An **epoch** is completed once each data sample has been used once in the training loop.\n", "\n", "Now that we have the parameters for our model, we can make predictions on our validation set." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "preds = predict(net, md.val_dl)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(10000, 10)" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "preds.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Question**: Why does our output have length 10 (for each image)?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([3, 8, 6, 9, 6])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "preds.argmax(axis=1)[:5]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "preds = preds.argmax(1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's check how accurate this approach is on our validation set. You may want to compare this against other implementations of logistic regression, such as the one in sklearn. In our testing, this simple pytorch version is faster and more accurate for this problem!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.91820000000000002" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.mean(preds == y_valid)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's see how some of our predictions look!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAsMAAAF0CAYAAADGqzQSAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xu4lmP6//HzUi1tSWhLRZn6iTayt2bGptRSR2lkRmRo\nRwkjxmiHiF/MGJUiYyK7QhtlU3Ypm/RDpugbpTKToh0lbdVarev7x9P3+PbrPJ/WvXqe9dzrWdf7\ndRyODp+57/s6jXs9ne6e876c914AAACAEB0WdwEAAABAXGiGAQAAECyaYQAAAASLZhgAAADBohkG\nAABAsGiGAQAAECya4Zg559o55+Y459Y753Y7575zzk12zp0cd23AwTjnznPOve2c2+ic2+qcW+ic\n6xl3XUAUzrlLnHMfOOe277t/P3POXRh3XcDBOOcucM7Nc87tcs5tds4955yrFXdd2Y5mOH41RORf\nInKjiFwsIoNEpJmIfOycaxBnYUAyzrnmIjJbRCqISB8RuUxEFojIk865fnHWBhTFOXe9iLwiic/e\nLiJyuYhMEZHKcdYFHIxz7tci8raIbJHEZ+6fROQ3IvKuc+7wOGvLdo5NN0of51wTEVkmIn/23v89\n7nqAAznn/q+I/FlEanjvt++Xfywi3nt/TmzFAQfhnGsoIktFZJD3flS81QDROedmi0hDEWnqvS/Y\nl50hIp+KSH/v/WMxlpfVeDJcOm3a92t+rFUAyeVI4v7cdUC+RfhcQenWU0QKReTxuAsBiulsEXnn\nfxphERHv/QJJ9AxdYquqDOA3rVLCOVfOOZfjnDtJRP4hIutF5MWYywKSeXrfr4845+o656o75/qI\nyEUiMjK+soAi5UriT96ucM5945wrcM6tdM71j7swoAh7RWSPke8WkVMyXEuZwtckSgnn3Gci0nrf\n364UkU7e+6UxlgQc1L4/npsuIvX2Rfki0s97/2R8VQEH55xbJiJ1JdFADBaRbyTxneG+InKL9350\njOUBSTnnPpXE19DO2i9rICL/EZF87z3fGz5ENMOlhHPu/4jIESJyoiS+i1lLRHK996virAuw7PsT\njHcl8d3LMZL4ukRnEeknItd67yfGWB6QlHNuuYicJCKXee9f3i9/Q0RaiUgdz2+MKIWcc1eJyPMi\ncr+IPCKJAfwnRORcSTTDlWIsL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nMLZZmVatWVdnq1atVVr169UyUI99//72Z\nWwN8U6dOLelyMsp77zK9ZjbfuxbrPu3SpYt5bKtWrVSWm5urMutnZPPmzSqrXbu2uc769etV9vTT\nT6vsn//8p8r27t1rXrO0yfS9W9bu2+Lo1KmTyqZPn66ynTt3quyuu+5SWXF2pStr+MxFtop67/Jk\nGAAAAMGiGQYAAECwaIYBAAAQLJphAAAABIsBuoiqVaumslmzZqls06ZN5vmLFi1SmTWY1KBBA5Ud\nf/zxKqtUqZK5zoYNG1R2zjnnRDouWzDMUTzHHXecymbMmKEy635MZuvWrSqz7vEKFSqozPpZEhGp\nWbOmymrVqqWyK6+8UmUffPCBytatW2euEycG6NIvJyfHzCdMmKCybt26qWzOnDkqa9OmTeqFlSF8\n5iJbMUAHAAAAFIFmGAAAAMGiGQYAAECwaIYBAAAQLAbossAxxxyjsttvv9081sp79OihsmeeeSb1\nwmLCMEfxLFy4UGUtWrRQ2ezZs83zb7vtNpX9+OOPKrN2kCuOY489VmVvvPGGypo0aaKygQMHquzR\nRx9NqZ6SwABd+g0ZMsTMhw8frrLnn39eZT179lRZQUFB6oWVIXzmpq5OnToqu+GGG8xjrTw/P19l\n1i64999/v8qs3wNERNasWWPmZQkDdAAAAEARaIYBAAAQLJphAAAABItmGAAAAMGiGQYAAECweJtE\nlurUqZOZW9vsPvLIIyq75ZZb0l5TpjDZnJw1sfz999+rbPLkySq76qqrzGvu3bs39cIO0cSJE1V2\nxRVXqKx169Yq+/zzz0ukplTwNonUnH766SqbN2+eeeyqVatU1qxZM5XFeX9nCz5zi+fEE09U2bhx\n41TWtm3bTJQju3fvNvPzzjtPZcnePJGteJsEAAAAUASaYQAAAASLZhgAAADBohkGAABAsMrHXQCK\ndtRRR6ls8ODBkc+vW7duOstBKdayZUuVOafnB9auXauyuAeJzj77bJV169ZNZXPnzlWZ9c9dGgfo\nEN1hh+lnNda22zk5Oeb5r732msrivsdR9tSrV09lS5YsUVn58rrdGjlypHnNMWPGRFqnadOmKvvb\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ZrAgbZt26ayzp07m8cOGzZMZbfccovKBg4cGGntMWPGRDoO4ahf\nv36k43Jyckq4kuSuvfZaM7d2WbR2fezbt6/KrJ29ZsyYYa7Ts2fPIipESZo3b56ZFxYWquydd95R\nWceOHVVm7dpZEpo0aWLm1n3arl07lVWuXFllZW3QkyfDAAAACBbNMAAAAIJFMwwAAIBg0QwDAAAg\nWAzQpZm1o5CISIsWLQ75fGuo7oEHHjDPX7t2baR1gANt3brVzO+66y6VtW3bVmUnn3xypHXatGlj\n5skGVFD2NW7cONJxmdp5s3r16ip7+OGHzWOtIaSCggKVWUNVubm5KrN2bRRhgC5uX375pZm//vrr\nKrOGkZcuXaoya1dCEXuXzjlz5qisXr16KrOG5axdbEVE6tSpozLr3n3llVfM88sSngwDAAAgWDTD\nAAAACBbNMAAAAIJFMwwAAIBgOe995hZzLnOLxaRSpUpm/vzzz6usdevWKou6E9P69evNvEePHip7\n6623Il0zW3jvXabXDOHeTSYvL09l06dPV1mFChUiXW/Pnj1mfsMNN6hswoQJka6ZLTJ972bLfTtz\n5kyVtWrVSmV169bNRDnmDovJBuisz/bRo0erbPXq1SqzBqhOPfVUc504d9/jMzc56/f8ESNGqOzm\nm29OaZ3NmzerrEaNGild03L55ZerzBroyxZR712eDAMAACBYNMMAAAAIFs0wAAAAgkUzDAAAgGDR\nDAMAACBYbMecZrt27TLzq666SmXly+v/+5NtiXug2rVrm7k15X/rrbeq7PHHH4+0DnDBBReozHoL\nTZcuXVRmTUBb25eK2NuO//jjjyp77bXXzPORvc466yyVJXvrSGmzdu1alR133HEqe+KJJ1R22mmn\nqaysvf2nrLN+z7feRjJ58mSVWX1BMrVq1Yp0XH5+vsqsny8RkRNOOEFlO3fujFxTWcKTYQAAAASL\nZhgAAADBohkGAABAsGiGAQAAECy2Yy5lmjdvrrKRI0eqzBpqSsbaBrRhw4bFqqs0YWvQkmHdeyIi\nCxYsUJk17GYNjVis7T5FRJ588kmVOaf/VTdr1kxl1j1eGrEds80aLuvYsaPKS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krVVrDVEJBJ9kGj4\n8OEqW7hwocpeffXVSNcri/jMTd1hh+lnj2eeeaZ5rDU0f+6556rs7LPPVtmePXtUNmXKFHOd0aNH\nq8y69wsLC83zswEDdAAAAEARaIYBAAAQLJphAAAABItmGAAAAMFigA5Zh2GO7HbWWWeZ+ccff6yy\n9957T2UXXHBBukvKmJAG6Hr37q2yZLtcWrtxWrZs2aKyefPmmcd+8cUXKnv55ZdVtnjxYpVl88BQ\nSeAzF9mKAToAAACgCDTDAAAACBbNMAAAAIJFMwwAAIBgMUCHrMMwB7JVSAN01o5ZHTp0MI+dOXNm\npGtu3LhRZStXrixeYSg2PnORrRigAwAAAIpAMwwAAIBg0QwDAAAgWDTDAAAACBbNMAAAAILF2ySQ\ndZhsRrYK6W0SKDv4zEW24m0SAAAAQBFohgEAABAsmmEAAAAEi2YYAAAAwaIZBgAAQLBohgEAABAs\nmmEAAAAEi2YYAAAAwaIZBgAAQLAyugMdAAAAUJrwZBgAAADBohkGAABAsGiGAQAAECyaYQAAAASL\nZhgAAADBohkGAABAsGiGAQAAECyaYQAAAASLZhgAAADBohkGAABAsGiGAQAAECyaYQAAAASLZhgA\nAADBohkGAABAsGiGAQAAECyaYQAAAASLZhgAAADBohkGAABAsGiGAQAAECyaYQAAAASLZhgAAADB\nohkGAABAsGiGAQAAEKz/BgZy4aPO2hNlAAAAAElFTkSuQmCC\n", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "plots(x_imgs[:8], titles=preds[:8])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Defining Logistic Regression Ourselves" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Above, we used pytorch's `nn.Linear` to create a linear layer. This is defined by a matrix multiplication and then an addition (these are also called `affine transformations`). Let's try defining this ourselves.\n", "\n", "Just as Numpy has `np.matmul` for matrix multiplication (in Python 3, this is equivalent to the `@` operator), PyTorch has `torch.matmul`. \n", "\n", "Our PyTorch class needs two things: constructor (says what the parameters are) and a forward method (how to calculate a prediction using those parameters) The method `forward` describes how the neural net converts inputs to outputs.\n", "\n", "In PyTorch, the optimizer knows to try to optimize any attribute of type **Parameter**." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def get_weights(*dims): return nn.Parameter(torch.randn(dims)/dims[0])\n", "def softmax(x): return torch.exp(x)/(torch.exp(x).sum(dim=1)[:,None])\n", "\n", "class LogReg(nn.Module):\n", " def __init__(self):\n", " super().__init__()\n", " self.l1_w = get_weights(28*28, 10) # Layer 1 weights\n", " self.l1_b = get_weights(10) # Layer 1 bias\n", "\n", " def forward(self, x):\n", " x = x.view(x.size(0), -1)\n", " x = (x @ self.l1_w) + self.l1_b # Linear Layer\n", " x = torch.log(softmax(x)) # Non-linear (LogSoftmax) Layer\n", " return x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We create our neural net and the optimizer. (We will use the same loss and metrics from above)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "net2 = LogReg().cuda()\n", "opt=optim.Adam(net2.parameters())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "eb28b90dfa8d4d2dad99665e5e3fd9a9", "version_major": 2, "version_minor": 0 }, "text/plain": [ "A Jupyter Widget" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "[ 0. 0.32209 0.28399 0.92088] \n", "\n" ] } ], "source": [ "fit(net2, md, n_epochs=1, crit=loss, opt=opt, metrics=metrics)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dl = iter(md.trn_dl)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xmb,ymb = next(dl)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Variable containing:\n", "-0.4245 -0.4245 -0.4245 ... -0.4245 -0.4245 -0.4245\n", "-0.4245 -0.4245 -0.4245 ... -0.4245 -0.4245 -0.4245\n", "-0.4245 -0.4245 -0.4245 ... -0.4245 -0.4245 -0.4245\n", " ... ⋱ ... \n", "-0.4245 -0.4245 -0.4245 ... -0.4245 -0.4245 -0.4245\n", "-0.4245 -0.4245 -0.4245 ... -0.4245 -0.4245 -0.4245\n", "-0.4245 -0.4245 -0.4245 ... -0.4245 -0.4245 -0.4245\n", "[torch.cuda.FloatTensor of size 64x784 (GPU 0)]" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vxmb = Variable(xmb.cuda())\n", "vxmb" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Variable containing:\n", "\n", "Columns 0 to 5 \n", " 1.6740e-03 1.0416e-05 2.5454e-05 1.9119e-02 6.5026e-05 9.7470e-01\n", " 3.4048e-02 1.8530e-04 6.6637e-01 3.5073e-02 1.5283e-01 6.4995e-05\n", " 3.0505e-08 4.3947e-08 1.0115e-05 2.0978e-04 9.9374e-01 6.3731e-05\n", "\n", "Columns 6 to 9 \n", " 2.1126e-06 1.7638e-04 3.9351e-03 2.9154e-04\n", " 1.1891e-03 3.2172e-02 1.4597e-02 6.3474e-02\n", " 8.9568e-06 9.7507e-06 7.8676e-04 5.1684e-03\n", "[torch.cuda.FloatTensor of size 3x10 (GPU 0)]" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "preds = net2(vxmb).exp(); preds[:3]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\n", " 5\n", " 2\n", " 4\n", " 1\n", " 8\n", " 8\n", " 3\n", " 4\n", " 2\n", " 7\n", " 1\n", " 1\n", " 7\n", " 9\n", " 3\n", " 4\n", " 3\n", " 0\n", " 1\n", " 4\n", " 3\n", " 3\n", " 6\n", " 8\n", " 9\n", " 0\n", " 9\n", " 5\n", " 2\n", " 4\n", " 7\n", " 9\n", " 0\n", " 2\n", " 5\n", " 2\n", " 9\n", " 9\n", " 6\n", " 9\n", " 4\n", " 2\n", " 2\n", " 7\n", " 1\n", " 5\n", " 5\n", " 9\n", " 0\n", " 7\n", " 4\n", " 0\n", " 9\n", " 1\n", " 1\n", " 4\n", " 7\n", " 0\n", " 9\n", " 0\n", " 4\n", " 4\n", " 0\n", " 7\n", "[torch.cuda.LongTensor of size 64 (GPU 0)]" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "preds = preds.data.max(1)[1]; preds" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's look at our predictions on the first eight images:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAsMAAAF0CAYAAADGqzQSAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XmUFeXV7/H9CLSMiqiMCggYvKIC4mwncQChhQUSMRHF\naDMoiBpxeGVSUeSiiRFQFJOgOIHK7IBTEByQV8WAckEQMEFQJgWRUemmn/tH910vl70PXd3n9Kk+\n/Xw/a7mIv1TVs02qD9vi7Hqc914AAACAEB0WdwEAAABAXGiGAQAAECyaYQAAAASLZhgAAADBohkG\nAABAsGiGAQAAECya4Zg55zo65+Y55zY5535xzn3rnJvqnDs57tqAQ3HOne+ce8c5t8U5t8M5t9g5\n1zvuuoAonHOXOuc+cM7tKrp/P3POXRR3XcChOOcudM4tcM7tdc5tc84975yrF3ddmY5mOH51RORf\nInKTiFwiIkNEpJWIfOycaxJnYUAizrnTRGSuiFQRkX4icrmILBKRp5xzA+KsDSiOc+4GEXlFCj97\nu4vIFSIyTUSqx1kXcCjOuV+LyDsisl0KP3P/JCK/EZF3nXOHx1lbpnNsulH+OOdaishKEbnDe//X\nuOsBDuac+98icoeI1PHe7zog/1hEvPf+3NiKAw7BOddURFaIyBDv/dh4qwGic87NFZGmInKS9z6/\nKDtTRD4VkYHe+ydiLC+j8W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\nqZN5bJs2bVS2bt26lNdk8d67tCx0gEy5d4899liVbdmyJalr5ufnq8x6K4D1hgpre+iSsN6uMnv2\n7KSuGad037uZct9arKn2J598UmW1atUyz7d+b1u4cKHKunbtqrIff/wxSonB4DO3YqpatarKPvro\nI5UtXbpUZbm5uWVSU6pFvXd5MgwAAIBg0QwDAAAgWDTDAAAACBbNMAAAAILFAJ2hdu3aKvv8889V\n1rhx48jXvO2221Q2duzYkhVWxqxtTRMNp5x++ukq++qrr1Jek4VhjsQqVaqkspEjR6psyJAh6Sin\nRD777DOVnXPOOSrbv39/OsopEwzQJcf6zLWGk0VELr744kjX/PLLL1V2xRVXqGzlypWRrlcR8Zlr\nbwUuYg919ujRQ2W//PJLymsqC8OHD1fZDTfcoLJTTz1VZdu3by+TmpLBAB0AAABQDJphAAAABItm\nGAAAAMGiGQYAAECwGKCLqHPnzip76aWXVFajRg3z/N27d6vs9ddfV9moUaNUtmzZsigllkhOTo7K\nXn31VZWtWrXKPL9Vq1YprykqhjlKxhqqq1u3rsoS3bvWvWINHVmZNWTx9ttvm+tYu4Odf/755rGZ\nigG61LOGLEXsnQqtHRotixYtUtlNN91kHmsNflY0fOaKNG3a1Mz//e9/q+z5559X2X/913+pbPPm\nzUnXlWrWAN3999+vsmbNmqls7dq1ZVFSUhigAwAAAIpBMwwAAIBg0QwDAAAgWDTDAAAACBYDdEno\n2LGjyh566CHz2NNOOy3SNffu3auyvn37qmzdunXm+dYX2LOzs1U2btw4lVk777344ovmOrm5uWae\nDgxzZI527dqpLNHAEQN0qRfyfXvZZZepbMaMGaW+nvU5LCIyadKkUl8zU/CZK9KwYUMzt3ZetYaR\nV69erbL+/fub1/zwww9Vlp+fX1yJJda9e3eVPfzwwyrLyspS2SmnnKKyn376KTWFpRADdAAAAEAx\naIYBAAAQLJphAAAABItmGAAAAMFigC7FEu1w1Lt3b5VZO9IcddRRKa/JYn0Z39r97r777ktHOSXC\nMEfmOOaYY1S2cuVK89j9+/er7Fe/+pXKyuOQ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wwAAAAgWzTAAAACCRTMMAACAYNEMAwAAIFg0\nwwAAAAgWzTAAAACCRTMMAACAYNEMAwAAIFg0wwAAAAjW/wWeKbMQcyi29AAAAABJRU5ErkJggg==\n", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "preds = predict(net2, md.val_dl).argmax(1)\n", "plots(x_imgs[:8], titles=preds[:8])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.91910000000000003" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.mean(preds == y_valid)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Aside about Broadcasting and Matrix Multiplication" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's dig in to what we were doing with `torch.matmul`: matrix multiplication. First, let's start with a simpler building block: **broadcasting**." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Element-wise operations " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Broadcasting and element-wise operations are supported in the same way by both numpy and pytorch.\n", "\n", "Operators (+,-,\\*,/,>,<,==) are usually element-wise.\n", "\n", "Examples of element-wise operations:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array([10, 6, -4]), array([2, 8, 7]))" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array([10, 6, -4])\n", "b = np.array([2, 8, 7])\n", "a,b" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([12, 14, 3])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a + b" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.66666666666666663" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(a < b).mean()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Broadcasting" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The term **broadcasting** describes how arrays with different shapes are treated during arithmetic operations. The term broadcasting was first used by Numpy, although is now used in other libraries such as [Tensorflow](https://www.tensorflow.org/performance/xla/broadcasting) and Matlab; the rules can vary by library.\n", "\n", "From the [Numpy Documentation](https://docs.scipy.org/doc/numpy-1.10.0/user/basics.broadcasting.html):\n", "\n", " The term broadcasting describes how numpy treats arrays with \n", " different shapes during arithmetic operations. Subject to certain \n", " constraints, the smaller array is “broadcast” across the larger \n", " array so that they have compatible shapes. Broadcasting provides a \n", " means of vectorizing array operations so that looping occurs in C\n", " instead of Python. It does this without making needless copies of \n", " data and usually leads to efficient algorithm implementations.\n", " \n", "In addition to the efficiency of broadcasting, it allows developers to write less code, which typically leads to fewer errors.\n", "\n", "*This section was adapted from [Chapter 4](http://nbviewer.jupyter.org/github/fastai/numerical-linear-algebra/blob/master/nbs/4.%20Compressed%20Sensing%20of%20CT%20Scans%20with%20Robust%20Regression.ipynb#4.-Compressed-Sensing-of-CT-Scans-with-Robust-Regression) of the fast.ai [Computational Linear Algebra](https://github.com/fastai/numerical-linear-algebra) course.*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Broadcasting with a scalar" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([10, 6, -4])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ True, True, False], dtype=bool)" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a > 0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How are we able to do a > 0? 0 is being **broadcast** to have the same dimensions as a.\n", "\n", "Remember above when we normalized our dataset by subtracting the mean (a scalar) from the entire data set (a matrix) and dividing by the standard deviation (another scalar)? We were using broadcasting!\n", "\n", "Other examples of broadcasting with a scalar:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([11, 7, -3])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a + 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [4, 5, 6],\n", " [7, 8, 9]])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = np.array([[1, 2, 3], [4,5,6], [7,8,9]]); m" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 2, 4, 6],\n", " [ 8, 10, 12],\n", " [14, 16, 18]])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2*m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Broadcasting a vector to a matrix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also broadcast a vector to a matrix:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([10, 20, 30])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c = np.array([10,20,30]); c" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[11, 22, 33],\n", " [14, 25, 36],\n", " [17, 28, 39]])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m + c" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[11, 22, 33],\n", " [14, 25, 36],\n", " [17, 28, 39]])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c + m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Although numpy does this automatically, you can also use the `broadcast_to` method:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3,)" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[10, 10, 10],\n", " [20, 20, 20],\n", " [30, 30, 30]])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.broadcast_to(c[:,None], m.shape)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[10, 20, 30],\n", " [10, 20, 30],\n", " [10, 20, 30]])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.broadcast_to(np.expand_dims(c,0), (3,3))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3,)" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 3)" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.expand_dims(c,0).shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The numpy `expand_dims` method lets us convert the 1-dimensional array `c` into a 2-dimensional array (although one of those dimensions has value 1)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 3)" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.expand_dims(c,0).shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[11, 22, 33],\n", " [14, 25, 36],\n", " [17, 28, 39]])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m + np.expand_dims(c,0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[10],\n", " [20],\n", " [30]])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.expand_dims(c,1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3, 1)" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c[:, None].shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[11, 12, 13],\n", " [24, 25, 26],\n", " [37, 38, 39]])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m + np.expand_dims(c,1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[10, 10, 10],\n", " [20, 20, 20],\n", " [30, 30, 30]])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.broadcast_to(np.expand_dims(c,1), (3,3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Broadcasting Rules" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[10, 20, 30]])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c[None]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[10],\n", " [20],\n", " [30]])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c[:,None]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[False, True, True],\n", " [False, False, True],\n", " [False, False, False]], dtype=bool)" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c[None] > c[:,None]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array([[0],\n", " [1],\n", " [2],\n", " [3],\n", " [4]]), array([[0, 1, 2, 3, 4]]))" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "xg,yg = np.ogrid[0:5, 0:5]; xg,yg" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 2, 3, 4],\n", " [1, 2, 3, 4, 5],\n", " [2, 3, 4, 5, 6],\n", " [3, 4, 5, 6, 7],\n", " [4, 5, 6, 7, 8]])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "xg+yg" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When operating on two arrays, Numpy/PyTorch compares their shapes element-wise. It starts with the **trailing dimensions**, and works its way forward. Two dimensions are **compatible** when\n", "\n", "- they are equal, or\n", "- one of them is 1\n", "\n", "Arrays do not need to have the same number of dimensions. For example, if you have a `256*256*3` array of RGB values, and you want to scale each color in the image by a different value, you can multiply the image by a one-dimensional array with 3 values. Lining up the sizes of the trailing axes of these arrays according to the broadcast rules, shows that they are compatible:\n", "\n", " Image (3d array): 256 x 256 x 3\n", " Scale (1d array): 3\n", " Result (3d array): 256 x 256 x 3\n", "\n", "The [numpy documentation](https://docs.scipy.org/doc/numpy-1.13.0/user/basics.broadcasting.html#general-broadcasting-rules) includes several examples of what dimensions can and can not be broadcast together." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Matrix Multiplication" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We are going to use broadcasting to define matrix multiplication." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array([[1, 2, 3],\n", " [4, 5, 6],\n", " [7, 8, 9]]), array([10, 20, 30]))" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m, c" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([140, 320, 500])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m @ c # np.matmul(m, c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We get the same answer using `torch.matmul`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\n", " 140\n", " 320\n", " 500\n", "[torch.LongTensor of size 3]" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "T(m) @ T(c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following is **NOT** matrix multiplication. What is it?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array([[1, 2, 3],\n", " [4, 5, 6],\n", " [7, 8, 9]]), array([10, 20, 30]))" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m,c" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 10, 40, 90],\n", " [ 40, 100, 180],\n", " [ 70, 160, 270]])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m * c" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([140, 320, 500])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(m * c).sum(axis=1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([10, 20, 30])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[10, 20, 30],\n", " [10, 20, 30],\n", " [10, 20, 30]])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.broadcast_to(c, (3,3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "From a machine learning perspective, matrix multiplication is a way of creating features by saying how much we want to weight each input column. **Different features are different weighted averages of the input columns**. \n", "\n", "The website [matrixmultiplication.xyz](http://matrixmultiplication.xyz/) provides a nice visualization of matrix multiplcation" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[10, 40],\n", " [20, 0],\n", " [30, -5]])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n = np.array([[10,40],[20,0],[30,-5]]); n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [4, 5, 6],\n", " [7, 8, 9]])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[140, 25],\n", " [320, 130],\n", " [500, 235]])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m @ n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([140, 320, 500])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(m * n[:,0]).sum(axis=1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 25, 130, 235])" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(m * n[:,1]).sum(axis=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Writing Our Own Training Loop" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As a reminder, this is what we did above to write our own logistic regression class (as a pytorch neural net):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "a4fe9bd617ca418889de7ab03703927b", "version_major": 2, "version_minor": 0 }, "text/plain": [ "A Jupyter Widget" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "[ 0. 0.31102 0.28004 0.92406] \n", "\n" ] } ], "source": [ "# Our code from above\n", "class LogReg(nn.Module):\n", " def __init__(self):\n", " super().__init__()\n", " self.l1_w = get_weights(28*28, 10) # Layer 1 weights\n", " self.l1_b = get_weights(10) # Layer 1 bias\n", "\n", " def forward(self, x):\n", " x = x.view(x.size(0), -1)\n", " x = x @ self.l1_w + self.l1_b \n", " return torch.log(softmax(x))\n", "\n", "net2 = LogReg().cuda()\n", "opt=optim.Adam(net2.parameters())\n", "\n", "fit(net2, md, n_epochs=1, crit=loss, opt=opt, metrics=metrics)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Above, we are using the fastai method `fit` to train our model. Now we will try writing the training loop ourselves.\n", "\n", "**Review question:** What does it mean to train a model?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will use the LogReg class we created, as well as the same loss function, learning rate, and optimizer as before:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "net2 = LogReg().cuda()\n", "loss=nn.NLLLoss()\n", "learning_rate = 1e-3\n", "optimizer=optim.Adam(net2.parameters(), lr=learning_rate)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "md is the ImageClassifierData object we created above. We want an iterable version of our training data (**question**: what does it mean for something to be iterable?):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dl = iter(md.trn_dl) # Data loader" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, we will do a **forward pass**, which means computing the predicted y by passing x to the model." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xt, yt = next(dl)\n", "y_pred = net2(Variable(xt).cuda())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can check the loss:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Variable containing:\n", " 2.3162\n", "[torch.cuda.FloatTensor of size 1 (GPU 0)]\n", "\n" ] } ], "source": [ "l = loss(y_pred, Variable(yt).cuda())\n", "print(l)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We may also be interested in the accuracy. We don't expect our first predictions to be very good, because the weights of our network were initialized to random values. Our goal is to see the loss decrease (and the accuracy increase) as we train the network:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.078125" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.mean(to_np(y_pred).argmax(axis=1) == to_np(yt))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we will use the optimizer to calculate which direction to step in. That is, how should we update our weights to try to decrease the loss?\n", "\n", "Pytorch has an automatic differentiation package ([autograd](http://pytorch.org/docs/master/autograd.html)) that takes derivatives for us, so we don't have to calculate the derivative ourselves! We just call `.backward()` on our loss to calculate the direction of steepest descent (the direction to lower the loss the most)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Before the backward pass, use the optimizer object to zero all of the\n", "# gradients for the variables it will update (which are the learnable weights\n", "# of the model)\n", "optimizer.zero_grad()\n", "\n", "# Backward pass: compute gradient of the loss with respect to model parameters\n", "l.backward()\n", "\n", "# Calling the step function on an Optimizer makes an update to its parameters\n", "optimizer.step()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, let's make another set of predictions and check if our loss is lower:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xt, yt = next(dl)\n", "y_pred = net2(Variable(xt).cuda())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Variable containing:\n", " 2.2265\n", "[torch.cuda.FloatTensor of size 1 (GPU 0)]\n", "\n" ] } ], "source": [ "l = loss(y_pred, Variable(yt).cuda())\n", "print(l)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that we are using **stochastic** gradient descent, so the loss is not guaranteed to be strictly better each time. The stochasticity comes from the fact that we are using **mini-batches**; we are just using 64 images to calculate our prediction and update the weights, not the whole dataset." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.15625" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.mean(to_np(y_pred).argmax(axis=1) == to_np(yt))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we run several iterations in a loop, we should see the loss decrease and the accuracy increase with time." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "loss: 2.2104923725128174 \t accuracy: 0.234375\n", "loss: 1.3094730377197266 \t accuracy: 0.625\n", "loss: 1.0296542644500732 \t accuracy: 0.78125\n", "loss: 0.8841525316238403 \t accuracy: 0.71875\n", "loss: 0.6643403768539429 \t accuracy: 0.8125\n", "loss: 0.5525785088539124 \t accuracy: 0.875\n", "loss: 0.43296846747398376 \t accuracy: 0.890625\n", "loss: 0.4388267695903778 \t accuracy: 0.90625\n", "loss: 0.39874207973480225 \t accuracy: 0.890625\n", "loss: 0.4848807752132416 \t accuracy: 0.875\n" ] } ], "source": [ "for t in range(100):\n", " xt, yt = next(dl)\n", " y_pred = net2(Variable(xt).cuda())\n", " l = loss(y_pred, Variable(yt).cuda())\n", " \n", " if t % 10 == 0:\n", " accuracy = np.mean(to_np(y_pred).argmax(axis=1) == to_np(yt))\n", " print(\"loss: \", l.data[0], \"\\t accuracy: \", accuracy)\n", "\n", " optimizer.zero_grad()\n", " l.backward()\n", " optimizer.step()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Put it all together in a training loop" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def score(x, y):\n", " y_pred = to_np(net2(V(x)))\n", " return np.sum(y_pred.argmax(axis=1) == to_np(y))/len(y_pred)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.908738057325\n" ] } ], "source": [ "net2 = LogReg().cuda()\n", "loss=nn.NLLLoss()\n", "learning_rate = 1e-2\n", "optimizer=optim.SGD(net2.parameters(), lr=learning_rate)\n", "\n", "for epoch in range(1):\n", " losses=[]\n", " dl = iter(md.trn_dl)\n", " for t in range(len(dl)):\n", " # Forward pass: compute predicted y and loss by passing x to the model.\n", " xt, yt = next(dl)\n", " y_pred = net2(V(xt))\n", " l = loss(y_pred, V(yt))\n", " losses.append(l)\n", "\n", " # Before the backward pass, use the optimizer object to zero all of the\n", " # gradients for the variables it will update (which are the learnable weights of the model)\n", " optimizer.zero_grad()\n", "\n", " # Backward pass: compute gradient of the loss with respect to model parameters\n", " l.backward()\n", "\n", " # Calling the step function on an Optimizer makes an update to its parameters\n", " optimizer.step()\n", " \n", " val_dl = iter(md.val_dl)\n", " val_scores = [score(*next(val_dl)) for i in range(len(val_dl))]\n", " print(np.mean(val_scores))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Stochastic Gradient Descent" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Nearly all of deep learning is powered by one very important algorithm: **stochastic gradient descent (SGD)**. SGD can be seeing as an approximation of **gradient descent (GD)**. In GD you have to run through all the samples in your training set to do a single itaration. In SGD you use only a subset of training samples to do the update for a parameter in a particular iteration. The subset used in each iteration is called a batch or minibatch.\n", "\n", "Now, instead of using the optimizer, we will do the optimization ourselves!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.908837579618\n" ] } ], "source": [ "net2 = LogReg().cuda()\n", "loss_fn=nn.NLLLoss()\n", "lr = 1e-2\n", "w,b = net2.l1_w,net2.l1_b\n", "\n", "for epoch in range(1):\n", " losses=[]\n", " dl = iter(md.trn_dl)\n", " for t in range(len(dl)):\n", " xt, yt = next(dl)\n", " y_pred = net2(V(xt))\n", " l = loss(y_pred, Variable(yt).cuda())\n", " losses.append(loss)\n", "\n", " # Backward pass: compute gradient of the loss with respect to model parameters\n", " l.backward()\n", " w.data -= w.grad.data * lr\n", " b.data -= b.grad.data * lr\n", " \n", " w.grad.data.zero_()\n", " b.grad.data.zero_() \n", "\n", " val_dl = iter(md.val_dl)\n", " val_scores = [score(*next(val_dl)) for i in range(len(val_dl))]\n", " print(np.mean(val_scores))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 2 }