diff --git a/Assignment/Assignment_1/200489_karan_part1.ipynb b/Assignment/Assignment_1/200489_karan_part1.ipynb new file mode 100644 index 0000000..cf1b814 --- /dev/null +++ b/Assignment/Assignment_1/200489_karan_part1.ipynb @@ -0,0 +1,866 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "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.8" + }, + "colab": { + "name": "Copy of DL_Stamatics_A1.ipynb", + "provenance": [], + "collapsed_sections": [] + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "rvFM645NE-D2" + }, + "source": [ + "# Assignment 1 - Part 1\n", + "In this assignment, we will go through basic linear algebra, NumPy, and image manipulation using Python to get everyone on the same page.\n", + "\n", + "One of the aims of this assignment is to get you to start getting comfortable searching for useful library functions online. So in many of the functions you will implement, you will have to look up helper functions.\n", + "\n", + "\\\n", + "\n", + "## Instructions\n", + "* This notebook contain blocks of code, you are required to complete those blocks(where required)\n", + "* You are required to copy this notebook (\"copy to drive\" above) and complete the code.(DO NOT CHANGE THE NAME OF THE FUNCTIONS)\n", + "\n", + "\\\n", + "\\\n", + "Also, I'd like to acknowledge the Stanford CS131. This assignment is highly based on the assignments from that course." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "UhSVK4RoK9q5" + }, + "source": [ + "First Let's import some dependencies" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "cCKqyfhIE-EQ" + }, + "source": [ + "# Imports the print function from newer versions of python\n", + "from __future__ import print_function\n", + "\n", + "# Setup\n", + "\n", + "# The Random module implements pseudo-random number generators\n", + "import random \n", + "\n", + "# Numpy is the main package for scientific computing with Python. \n", + "# This will be one of our most used libraries in this project\n", + "import numpy as np\n", + "\n", + "# The Time library helps us time code runtimes\n", + "import time\n", + "\n", + "\n", + "# Some more magic so that the notebook will reload external python modules;\n", + "# see http://stackoverflow.com/questions/1907993/autoreload-of-modules-in-ipython\n", + "%load_ext autoreload\n", + "%autoreload 2\n", + "%reload_ext autoreload" + ], + "execution_count": 1, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true, + "id": "QLtp15rqE-EU" + }, + "source": [ + "# Part 1: Linear Algebra and NumPy Review\n", + "In this section, we will review linear algebra and learn how to use vectors and matrices in python using numpy." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "E8HDYpc0E-EV" + }, + "source": [ + "## Part 1.1 (5 points)\n", + "First, let's test whether you can define the following matrices and vectors using numpy. Look up `np.array()` for help. In the next code block, define $M$ as a $(4, 3)$ matrix, $a$ as a $(1, 3)$ row vector and $b$ as a $(3, 1)$ column vector:\n", + "\n", + "$$M = \\begin{bmatrix}\n", + "1 & 2 & 3 \\\\\n", + "4 & 5 & 6 \\\\\n", + "7 & 8 & 9 \\\\\n", + "10 & 11 & 12 \\end{bmatrix}\n", + "$$\n", + "\n", + "$$a = \\begin{bmatrix}\n", + "1 & 1 & 0\n", + "\\end{bmatrix}\n", + "$$\n", + "\n", + "$$b = \\begin{bmatrix}\n", + "-1 \\\\ 2 \\\\ 5\n", + "\\end{bmatrix} \n", + "$$ " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "mETk2NCME-EX", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "b55db310-3b85-4d08-b71b-cd1a2aa388a1" + }, + "source": [ + "### YOUR CODE HERE\n", + "M = np.array ([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])\n", + "a = np.array ([[1,1,0]])\n", + "b = np.array ([[-1],[2],[5]])\n", + "### END CODE HERE\n", + "print(\"M = \\n\", M)\n", + "print(\"The size of M is: \", M.shape)\n", + "print()\n", + "print(\"a = \", a)\n", + "print(\"The size of a is: \", a.shape)\n", + "print()\n", + "print(\"b = \", b)\n", + "print(\"The size of b is: \", b.shape)" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "M = \n", + " [[ 1 2 3]\n", + " [ 4 5 6]\n", + " [ 7 8 9]\n", + " [10 11 12]]\n", + "The size of M is: (4, 3)\n", + "\n", + "a = [[1 1 0]]\n", + "The size of a is: (1, 3)\n", + "\n", + "b = [[-1]\n", + " [ 2]\n", + " [ 5]]\n", + "The size of b is: (3, 1)\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "rSta4NheE-EZ" + }, + "source": [ + "## Part 1.2 (5 points)\n", + "Implement the `dot_product()` method below and check that it returns the correct answer for $a^Tb$." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "C5ZRjCE2MVOU" + }, + "source": [ + "def dot_product(a, b):\n", + " \"\"\"Implement dot product between the two vectors: a and b.\n", + " (optional): While you can solve this using for loops, we recommend\n", + " that you look up `np.dot()` online and use that instead.\n", + " Args:\n", + " a: numpy array of shape (x, n)\n", + " b: numpy array of shape (n, x)\n", + " Returns:\n", + " out: numpy array of shape (x, x) (scalar if x = 1)\n", + " \"\"\"\n", + " out = np.dot(a,b)\n", + " ### YOUR CODE HERE\n", + " pass\n", + " ### END YOUR CODE\n", + " return out" + ], + "execution_count": 5, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "pbLIS5vIE-Ea", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "a00c47a0-7ce8-4f43-ec6a-48934f9c5e1c" + }, + "source": [ + "# Now, let's test out this dot product. Your answer should be [[1]].\n", + "aDotB = dot_product(a, b)\n", + "print(aDotB)\n", + "\n", + "print(\"The size is: \", aDotB.shape)" + ], + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[[1]]\n", + "The size is: (1, 1)\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "0rGfcRU1E-Eb" + }, + "source": [ + "## Part 1.3 (5 points)\n", + "Implement the `complicated_matrix_function()` method and use it to compute $(ab)Ma^T$\n", + "\n", + "IMPORTANT NOTE: The `complicated_matrix_function()` method expects all inputs to be two dimensional numpy arrays, as opposed to 1-D arrays. This is an important distinction, because 2-D arrays can be transposed, while 1-D arrays cannot.\n", + "\n", + "To transpose a 2-D array, you can use the syntax `array.T` " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "dglQmbuLNOk6" + }, + "source": [ + "def complicated_matrix_function(M, a, b):\n", + " \"\"\"Implement (a * b) * (M * a.T).\n", + " (optional): Use the `dot_product(a, b)` function you wrote above\n", + " as a helper function.\n", + " Args:\n", + " M: numpy matrix of shape (x, n).\n", + " a: numpy array of shape (1, n).\n", + " b: numpy array of shape (n, 1).\n", + " Returns:\n", + " out: numpy matrix of shape (x, 1).\n", + " \"\"\"\n", + " out = np.dot(a,b)*np.dot(M,a.T)\n", + " ### YOUR CODE HERE\n", + " pass\n", + " ### END YOUR CODE\n", + " return out" + ], + "execution_count": 9, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "da_uQQLhE-Ec", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "c9cb4625-66f4-4588-eff7-05f33692acc4" + }, + "source": [ + "# Your answer should be $[[3], [9], [15], [21]]$ of shape(4, 1).\n", + "ans = complicated_matrix_function(M, a, b)\n", + "print(ans)\n", + "print()\n", + "print(\"The size is: \", ans.shape)" + ], + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[[ 3]\n", + " [ 9]\n", + " [15]\n", + " [21]]\n", + "\n", + "The size is: (4, 1)\n" + ] + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "6CWXxSSOE-Ed", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "9663685a-09cb-42c5-f217-7df7ad3f398b" + }, + "source": [ + "M_2 = np.array(range(4)).reshape((2,2))\n", + "a_2 = np.array([[1,1]])\n", + "b_2 = np.array([[10, 10]]).T\n", + "print(M_2.shape)\n", + "print(a_2.shape)\n", + "print(b_2.shape)\n", + "print()\n", + "\n", + "# Your answer should be $[[20], [100]]$ of shape(2, 1).\n", + "ans = complicated_matrix_function(M_2, a_2, b_2)\n", + "print(ans)\n", + "print()\n", + "print(\"The size is: \", ans.shape)" + ], + "execution_count": 11, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "(2, 2)\n", + "(1, 2)\n", + "(2, 1)\n", + "\n", + "[[ 20]\n", + " [100]]\n", + "\n", + "The size is: (2, 1)\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "4fHLxLl4E-Ee" + }, + "source": [ + "## Part 1.4 (10 points) [Optional/Bonus]\n", + "Implement `eigen_decomp()` and `get_eigen_values_and_vectors()` methods. In this method, perform eigenvalue decomposition on the following matrix and return the largest k eigen values and corresponding eigen vectors (k is specified in the method calls below).\n", + "\n", + "$$M = \\begin{bmatrix}\n", + "1 & 2 & 3 \\\\\n", + "4 & 5 & 6 \\\\\n", + "7 & 8 & 9 \\end{bmatrix}\n", + "$$\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "RfaCSoRMOIc8" + }, + "source": [ + "def eigen_decomp(M):\n", + " \"\"\"Implement eigenvalue decomposition.\n", + " (optional): You might find the `np.linalg.eig` function useful.\n", + " Args:\n", + " matrix: numpy matrix of shape (m, n)\n", + " Returns:\n", + " w: numpy array of shape (m, m) such that the column v[:,i] is the eigenvector corresponding to the eigenvalue w[i].\n", + " v: Matrix where every column is an eigenvector.\n", + " \"\"\"\n", + " w = None\n", + " v = None\n", + " ### YOUR CODE HERE\n", + " pass\n", + " ### END YOUR CODE\n", + " return w, v" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "YB120rb4ONBH" + }, + "source": [ + "def get_eigen_values_and_vectors(M, k):\n", + " \"\"\"Return top k eigenvalues and eigenvectors of matrix M. By top k\n", + " here we mean the eigenvalues with the top ABSOLUTE values (lookup\n", + " np.argsort for a hint on how to do so.)\n", + " (optional): Use the `eigen_decomp(M)` function you wrote above\n", + " as a helper function\n", + " Args:\n", + " M: numpy matrix of shape (m, m).\n", + " k: number of eigen values and respective vectors to return.\n", + " Returns:\n", + " eigenvalues: list of length k containing the top k eigenvalues\n", + " eigenvectors: list of length k containing the top k eigenvectors\n", + " of shape (m,)\n", + " \"\"\"\n", + " eigenvalues = []\n", + " eigenvectors = []\n", + " ### YOUR CODE HERE\n", + " pass\n", + " ### END YOUR CODE\n", + " return eigenvalues, eigenvectors" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "t0_GkrJwE-Ee" + }, + "source": [ + "# Let's define M.\n", + "M = np.array([[1,2,3],[4,5,6],[7,8,9]])\n", + "\n", + "# Now let's grab the first eigenvalue and first eigenvector.\n", + "# You should get back a single eigenvalue and a single eigenvector.\n", + "val, vec = get_eigen_values_and_vectors(M[:,:3], 1)\n", + "print(\"First eigenvalue =\", val[0])\n", + "print()\n", + "print(\"First eigenvector =\", vec[0])\n", + "print()\n", + "assert len(vec) == 1\n", + "\n", + "# Now, let's get the first two eigenvalues and eigenvectors.\n", + "# You should get back a list of two eigenvalues and a list of two eigenvector arrays.\n", + "val, vec = get_eigen_values_and_vectors(M[:,:3], 2)\n", + "print(\"Eigenvalues =\", val)\n", + "print()\n", + "print(\"Eigenvectors =\", vec)\n", + "assert len(vec) == 2" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Yeh-V5x1PYz5" + }, + "source": [ + "## Part 1.5 (10 points)\n", + "In this section, you'll implement a gaussian elimination.\n", + "\n", + "The algorithm to to reduce a matrix to rref using gaussian elimination contains 2 parts, First reducing the matrix to partial reduced form, then back substituting to calculate the rref. First algorithm can be summed up as:\n", + "1. Partial pivoting: Find the kth pivot by swapping rows, to move the entry with the largest absolute value to the pivot position. This imparts computational stability to the algorithm.\n", + "2. For each row below the pivot, calculate the factor f which makes the kth entry zero, and for every element in the row subtract the fth multiple of the corresponding element in the kth row.\n", + "3. Repeat above steps for each unknown. We will be left with a partial r.e.f. matrix.\n", + "\n", + "$$\\begin{bmatrix}\n", + "1 & 2 & 3 \\\\\n", + "4 & 5 & 6 \\\\\n", + "7 & 8 & 9 \\end{bmatrix}\n", + "=>\n", + "\\begin{bmatrix}\n", + "7 & 8 & 9 \\\\\n", + "4 & 5 & 6 \\\\\n", + "1 & 2 & 3 \\end{bmatrix}\n", + "=>\n", + "\\begin{bmatrix}\n", + "7 & 8 & 9 \\\\\n", + "0 & 0.42 & 0.85 \\\\\n", + "0 & 0.85 & 1.71 \\end{bmatrix}\n", + "=>\n", + "\\begin{bmatrix}\n", + "7 & 8 & 9 \\\\\n", + "0 & 0.85 & 1.71 \\\\\n", + "0 & 0.45 & 0.85 \\end{bmatrix}\n", + "=>\n", + "\\begin{bmatrix}\n", + "7 & 8 & 9 \\\\\n", + "0 & 0.42 & 0.85 \\\\\n", + "0 & 0 & -0.05 \\end{bmatrix}\n", + "$$\n", + "Second algorithm:\n", + "1. Take a pivot from the last row.\n", + "2. For each row above the pivot, calculate the factor f which makes the kth entry zero, and for every element in the row subtract the fth multiple of the corresponding element in the kth row\n", + "3. Repeat the above step untill the matrix is in rref\n", + "$$\\begin{bmatrix}\n", + "7 & 8 & 0 \\\\\n", + "0 & 0.42 & 0 \\\\\n", + "0 & 0 & -0.05 \\end{bmatrix}\n", + "=>\n", + "\\begin{bmatrix}\n", + "7 & 0 & 0 \\\\\n", + "0 & 0.42 & 0 \\\\\n", + "0 & 0 & -0.05 \\end{bmatrix}\n", + "$$\n", + "\n", + "Steps for implementation:\n", + "1. Complete the function `swap_rows()`\n", + "2. Complete the function `apply_row()`\n", + "3. Complete `forward()` and `backward()`\n", + "4. Finally implement `rref()` using the `forward()` and `backward()`\n", + "\n", + "Note: You can skip this part if you want." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "qUFujiFAPYz6" + }, + "source": [ + "def swap_rows(M):\n", + " \"\"\"Implement row swapping to make the largest element in the pivotial column to be the first row.\n", + " Args:\n", + " matrix: numpy matrix of shape (m, n)\n", + " Returns:\n", + " Ms: matrix with swapped row\n", + " \"\"\"\n", + " out = None\n", + " ### YOUR CODE HERE\n", + " pass\n", + " ### END YOUR CODE\n", + " return out" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "S8lbAUSWWpyO" + }, + "source": [ + "def apply_rows(M):\n", + " \"\"\"For each row below the pivot, calculate the factor f which makes the kth\n", + " entry zero, and for every element in the row subtract the fth multiple of the\n", + " corresponding element in the kth row.\n", + " Args:\n", + " matrix: numpy matrix of shape (m, n)\n", + " Returns:\n", + " Ms: matrix with all other entries of the pivotal col zero\n", + " \"\"\"\n", + " out = None\n", + " ### YOUR CODE HERE\n", + " pass\n", + " ### END YOUR CODE\n", + " return out" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "GnE_-JLxPYz7" + }, + "source": [ + "def forward(M):\n", + " \"\"\"Return a partial ref using the algo described above\n", + " Args:\n", + " M: numpy matrix of shape (m, n).\n", + " Returns:\n", + " Ms: ref of M\n", + " \"\"\"\n", + " out = None\n", + " ### YOUR CODE HERE\n", + " pass\n", + " ### END YOUR CODE\n", + " return out" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "Wb7pPGP4XmJu" + }, + "source": [ + "def backward(M):\n", + " \"\"\"Return a rref using the algo described above\n", + " Args:\n", + " M: numpy matrix of shape (m, n).\n", + " Returns:\n", + " Ms: rref of M\n", + " \"\"\"\n", + " out = None\n", + " ### YOUR CODE HERE\n", + " pass\n", + " ### END YOUR CODE\n", + " return out" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "XLq81xzXYR85" + }, + "source": [ + "def rref(M):\n", + " \"\"\"Return a rref using the algo descrbed above\n", + " Args:\n", + " M: numpy matrix of shape (m, n).\n", + " Returns:\n", + " Ms: ref of M\n", + " \"\"\"\n", + " out = None\n", + " ### YOUR CODE HERE\n", + " pass\n", + " ### END YOUR CODE\n", + " return out" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "Eiz6EbsWPYz8" + }, + "source": [ + "# Let's define M.\n", + "M = np.array([[1,2,3],[4,5,6],[7,8,9]])\n", + "\n", + "# Now let's calculate it's rref.\n", + "# Note that your code may be evaluated on other test cases as well\n", + "Mrref = rref(M)\n", + "print(Mrref)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "G46pyDzAE-Ef" + }, + "source": [ + "## Part 1.6 (10 points)\n", + "\n", + "To wrap up our overview of NumPy, let's implement something fun — a helper function for computing the Euclidean distance between two $n$-dimensional points!\n", + "\n", + "In the 2-dimensional case, computing the Euclidean distance reduces to solving the Pythagorean theorem $c = \\sqrt{a^2 + b^2}$. where, given two points $(x_1, y_1)$ and $(x_2, y_2)$, $a = x_1 - x_2$ and $b = y_1 - y_2$.\n", + "\n", + "\n", + "More generally, given two $n$-dimensional vectors, the Euclidean distance can be computed by:\n", + "\n", + "1. Performing an elementwise subtraction between the two vectors, to get $n$ difference values.\n", + "2. Squaring each of the $n$ difference values, and summing the squares.\n", + "4. Taking the square root of our sum.\n", + "\n", + "Alternatively, the Euclidean distance between length-$n$ vectors $u$ and $v$ can be written as:\n", + "\n", + "$\n", + "\\quad\\textbf{distance}(u, v) = \\sqrt{\\sum_{i=1}^n (u_i - v_i)^2}\n", + "$\n", + "\n", + "\n", + "Try implementing this function: first using native Python with a `for` loop in the `euclidean_distance_native()` function, then in NumPy **without any loops** in the `euclidean_distance_numpy()` function.\n", + "We've added some `assert` statements here to help you check functionality (if it prints nothing, then your implementation is correct)!" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "5xvHopPqO29C" + }, + "source": [ + "def euclidean_distance_native(u, v):\n", + " \"\"\"Computes the Euclidean distance between two vectors, represented as Python\n", + " lists.\n", + " Args:\n", + " u (List[float]): A vector, represented as a list of floats.\n", + " v (List[float]): A vector, represented as a list of floats.\n", + " Returns:\n", + " float: Euclidean distance between `u` and `v`.\n", + " \"\"\"\n", + " # First, run some checks:\n", + " assert isinstance(u, list)\n", + " assert isinstance(v, list)\n", + " assert len(u) == len(v)\n", + "\n", + " # Compute the distance!\n", + " # Notes:\n", + " # 1) Try breaking this problem down: first, we want to get\n", + " # the difference between corresponding elements in our\n", + " # input arrays. Then, we want to square these differences.\n", + " # Finally, we want to sum the squares and square root the\n", + " # sum.\n", + " sum=0\n", + " \n", + " for i in range(len(u)):\n", + " sum+= (u[i]-v[i])**2\n", + "\n", + " out = sum**0.5\n", + " ### YOUR CODE HERE\n", + " pass\n", + " ### END YOUR CODE\n", + " return out" + ], + "execution_count": 14, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "wvLuK8MuO3LH" + }, + "source": [ + "def euclidean_distance_numpy(u, v):\n", + " \"\"\"Computes the Euclidean distance between two vectors, represented as NumPy\n", + " arrays.\n", + " Args:\n", + " u (np.ndarray): A vector, represented as a NumPy array.\n", + " v (np.ndarray): A vector, represented as a NumPy array.\n", + " Returns:\n", + " float: Euclidean distance between `u` and `v`.\n", + " \"\"\"\n", + " # First, run some checks:\n", + " assert isinstance(u, np.ndarray)\n", + " assert isinstance(v, np.ndarray)\n", + " assert u.shape == v.shape\n", + "\n", + " # Compute the distance!\n", + " # Note:\n", + " # 1) You shouldn't need any loops\n", + " # 2) Some functions you can Google that might be useful:\n", + " # np.sqrt(), np.sum()\n", + " # 3) Try breaking this problem down: first, we want to get\n", + " # the difference between corresponding elements in our\n", + " # input arrays. Then, we want to square these differences.\n", + " # Finally, we want to sum the squares and square root the\n", + " # sum.\n", + "\n", + " out=(np.sum((u-v)**2))**0.5\n", + "\n", + " ### YOUR CODE HERE\n", + " pass\n", + " ### END YOUR CODE\n", + " return out" + ], + "execution_count": 16, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "wu9MimVJE-Eg" + }, + "source": [ + "## Testing native Python function\n", + "assert euclidean_distance_native([7.0], [6.0]) == 1.0\n", + "assert euclidean_distance_native([7.0, 0.0], [3.0, 3.0]) == 5.0\n", + "assert euclidean_distance_native([7.0, 0.0, 0.0], [3.0, 0.0, 3.0]) == 5.0" + ], + "execution_count": 15, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "kJDk88g1E-Ej" + }, + "source": [ + "## Testing NumPy function\n", + "assert euclidean_distance_numpy(\n", + " np.array([7.0]),\n", + " np.array([6.0])\n", + ") == 1.0\n", + "assert euclidean_distance_numpy(\n", + " np.array([7.0, 0.0]),\n", + " np.array([3.0, 3.0])\n", + ") == 5.0\n", + "assert euclidean_distance_numpy(\n", + " np.array([7.0, 0.0, 0.0]),\n", + " np.array([3.0, 0.0, 3.0])\n", + ") == 5.0" + ], + "execution_count": 17, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "n = 1000\n", + "\n", + "# Create some length-n lists and/or n-dimensional arrays\n", + "a = [0.0] * n\n", + "b = [10.0] * n\n", + "a_array = np.array(a)\n", + "b_array = np.array(b)\n", + "\n", + "# Compute runtime for native implementation\n", + "start_time = time.time()\n", + "for i in range(10000):\n", + " euclidean_distance_native(a, b)\n", + "print(\"Native:\", (time.time() - start_time), \"seconds\")\n", + "\n", + "# Compute runtime for numpy implementation\n", + "# Start by grabbing the current time in seconds\n", + "start_time = time.time()\n", + "for i in range(10000):\n", + " euclidean_distance_numpy(a_array, b_array)\n", + "print(\"NumPy:\", (time.time() - start_time), \"seconds\")" + ], + "metadata": { + "id": "E7Z38WwHhoNl", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "9bbe22aa-f315-4596-b3b6-80fc156c8bb6" + }, + "execution_count": 18, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Native: 1.5091254711151123 seconds\n", + "NumPy: 0.09277892112731934 seconds\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Mjik4mQXE-Ek" + }, + "source": [ + "Next, let's take a look at how these two implementations compare in terms of runtime:" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "t4e6MfhHE-Em" + }, + "source": [ + "As you can see, doing vectorized calculations (i.e. no for loops) with NumPy results in significantly faster computations! " + ] + }, + { + "cell_type": "markdown", + "source": [ + "Congrats You've come to the end of this notebook. If you solved everything above, impressive. If not, you might need to read/think a bit more. You can always ask doubts. Also, Note that you should submit it even if you cannot solve everything. We might evaluate these using a script later." + ], + "metadata": { + "id": "XvFE0Q5bhx6-" + } + } + ] +} \ No newline at end of file diff --git a/Assignment/Assignment_2/200489_karan.ipynb b/Assignment/Assignment_2/200489_karan.ipynb new file mode 100644 index 0000000..061be49 --- /dev/null +++ b/Assignment/Assignment_2/200489_karan.ipynb @@ -0,0 +1,655 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Copy of Copy of DL_Stamatics_A2.ipynb", + "provenance": [], + "collapsed_sections": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "rvFM645NE-D2" + }, + "source": [ + "# Assignment 2\n", + "In this assignment, we will go through Perceptron, Linear Classifiers, Loss Functions, Gradient Descent and Back Propagation.\n", + "\n", + "\n", + "PS. this one is not from Stanford's course.\n", + "\n", + "\n", + "\n", + "\\\n", + "\n", + "## Instructions\n", + "* This notebook contain blocks of code, you are required to complete those blocks(where required)\n", + "* You are required to copy this notebook (\"copy to drive\" above) and complete the code.(DO NOT CHANGE THE NAME OF THE FUNCTIONS)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true, + "id": "QLtp15rqE-EU" + }, + "source": [ + "# Part 1: Perceptron\n", + "In this section, we will see how to implement a perceptron. Goal would be for you to delve into the mathematics.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Zao4e-DphaGA" + }, + "source": [ + "## Intro\n", + "What's a perceptron? It's an algorithm modelled on biological computational model to classify things into binary classes. It's a supervides learning algorithm, meaning that you need to provide labelled data containing features and the actual classifications. A perceptron would take these features as input and spit out a binary value (0 or 1). While training the model with training data, we try to minimise the error and learn the parameters involved." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "wDTUoAd6ixm-" + }, + "source": [ + "**How does it work?**\\\n", + "A perceptron is modelled on a biological neuron. A neuron has input dendrites and the output is carried by axons. Similarly, a perceptron takes inputs called \"features\". After processing, a perceptron gives output. For computation, it has a \"weight\" vector which is multipled with feature vector. An activation function is added to introduce some non linearities and the output is given out.\\\n", + "It can be represented as: $$ f=\\sum_{i=1}^{m} w_ix_i +b$$\n", + "\n", + "Let's implement this simple function to give an output.\n", + "\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "iXezofBIgzId" + }, + "source": [ + "import numpy as np\n", + "\n", + "class perceptron():\n", + " def __init__(self,num_input_features=8):\n", + " self.weights = np.random.randn(num_input_features)\n", + " self.bias = np.random.random()\n", + "\n", + " def activation(self,x):\n", + " '''\n", + " Implement heavside step activation function here (google ;))\n", + "\n", + " '''\n", + " if x>=0:\n", + " return 1\n", + " else:\n", + " return 0\n", + " pass\n", + "\n", + " def forward(self,x: np.ndarray):\n", + " '''\n", + " you have random initialized weights and bias\n", + " you can access then using `self.weights` and `self.bias`\n", + " you should use activation function before returning\n", + " \n", + " x : input features\n", + " return : a binary value as the output of the perceptron \n", + " '''\n", + " # YOUR CODE HERE\n", + " n = self.activation(np.dot(x, self.weights)+self.bias)\n", + " return n\n", + " pass\n", + " # YOUR CODE HERE" + ], + "execution_count": 3, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "oSKwDFAyocVo" + }, + "source": [ + "np.random.seed(0)\n", + "perc = perceptron(8)\n", + "assert perc.forward(np.arange(8))==1" + ], + "execution_count": 4, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true, + "id": "NWTTg1e9r7uM" + }, + "source": [ + "# Part 2: Linear Classifier\n", + "In this section, we will see how to implement a linear Classifier.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "DYDO4GcHr7uM" + }, + "source": [ + "## Intro\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "-HFvjH06r7uN" + }, + "source": [ + "**How does it work?**\n", + "\n", + "Linear Classifier uses the following function: $$Y = WX+b$$ Where, $W$ is a 2d array of weights with shape (#features, #classes).\n", + "\n", + "\n", + "Let's implement this classifier.\n", + "\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "9A13CEkGr7uN" + }, + "source": [ + "import numpy as np\n", + "\n", + "class LinearClassifier():\n", + " def __init__(self,num_input_features=32,num_classes=5):\n", + " self.weights = np.random.randn(num_input_features,num_classes)\n", + " self.bias = np.random.rand(num_classes)\n", + "\n", + " def forward(self,x: np.ndarray):\n", + " '''\n", + " x: input features\n", + " you have random initialized weights and bias\n", + " you can access then using `self.weights` and `self.bias`\n", + " return an output vector of num_classes size\n", + " '''\n", + " # YOUR CODE HERE\n", + " out = np.dot(x,self.weights) + self.bias\n", + " pass\n", + " return out\n", + " # YOUR CODE HERE" + ], + "execution_count": 9, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "zgzPxyTsr7uN", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "b6802cb4-f14e-4748-9a80-cd0ce9db1627" + }, + "source": [ + "np.random.seed(0)\n", + "lc = LinearClassifier()\n", + "lc.forward(np.random.rand(1,32))\n", + "# Should be close to:\n", + "# array([[ 1.30208164, 5.58136003, 0.87793013, -4.7332119 , 4.81172123]])" + ], + "execution_count": 10, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[ 1.30208164, 5.58136003, 0.87793013, -4.7332119 , 4.81172123]])" + ] + }, + "metadata": {}, + "execution_count": 10 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true, + "id": "ZVgOVzJetuqo" + }, + "source": [ + "# Part 3: Loss Functions, Gradient descent and Backpropagation\n", + "\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "4pXryjpctuqy" + }, + "source": [ + "## Intro\n", + "\n", + "Loss Functions tells how \"off\" the output od our model is. Based upon the application, you can use several different loss functions. Formally, A loss function is a function $L:(z,y)\\in\\mathbb{R}\\times Y\\longmapsto L(z,y)\\in\\mathbb{R}$ that takes as inputs the predicted value $z$ corresponding to the real data value yy and outputs how different they are We'll implement L1 loss, L2 loss, Logistic loss, hinge loss and cross entropy loss functions." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "QGRb8BHotuqy" + }, + "source": [ + "### **L1 loss**\n", + "L1 loss is the linear loss function $L = \\dfrac{1}{2}|y−z| $\n", + "\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "YxVh6IL2tuqz" + }, + "source": [ + "import numpy as np\n", + "def L1Loss(z,y):\n", + " '''\n", + " y : True output.\n", + " z : Predicted output.\n", + " return : L\n", + " '''\n", + " return 0.5*(abs(y-z))\n", + " pass" + ], + "execution_count": 11, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "2xy8ZS84cKtQ" + }, + "source": [ + "### **L2 loss**\n", + "L2 loss is the quadratic loss function or the least square error function $L = \\dfrac{1}{2}(y−z)^2 $\n", + "\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "JThp5P-KcKtS" + }, + "source": [ + "import numpy as np\n", + "def L2Loss(z,y):\n", + " '''\n", + " y : True output. \n", + " z : Predicted output. \n", + " return : L\n", + " '''\n", + " return 0.5*(abs(y-z)**2)\n", + " pass" + ], + "execution_count": 12, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Z2JNLnWYcLSC" + }, + "source": [ + "### **Hinge Loss**\n", + "Hinge loss is: $ L = max( 0, 1 - yz ) $" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "gQ1YM4J-cLSC" + }, + "source": [ + "import numpy as np\n", + "def hingeLoss(z,y):\n", + " '''\n", + " y : True output. \n", + " z : Predicted output. \n", + " return : L\n", + " '''\n", + " return np.maximum(0,1-y*z)\n", + " pass" + ], + "execution_count": 13, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "m15_MjradMNY" + }, + "source": [ + "### **Cross Entropy Loss**\n", + "Another very famous loss function is Cross Entropy loss: $ L = −[ylog(z)+(1−y)log(1−z)] $." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "snJLqhszdMNY" + }, + "source": [ + "import numpy as np\n", + "def CELoss(z,y):\n", + " '''\n", + " y : True output. \n", + " z : Predicted output. \n", + " return : L\n", + " '''\n", + " \n", + " return -1*(y*np.log(z)+(1-y)*np.log(1-z))\n", + " pass" + ], + "execution_count": 14, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "OsRPsfzxyEVL" + }, + "source": [ + "### **0-1 Loss**\n", + "Loss Function used by perceptron is: $ \\begin{cases} \n", + " 0=z-y & z=y \\\\\n", + " 1=\\dfrac{z-y}{z-y} & z\\neq y\n", + " \\end{cases} $." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "5sA7GxLHyEVM" + }, + "source": [ + "import numpy as np\n", + "def zeroOneLoss(z,y):\n", + " '''\n", + " y : True output. \n", + " z : Predicted output. \n", + " return : L\n", + " '''\n", + " if(z==y):\n", + " return 0;\n", + " else:\n", + " return 1;\n", + " pass" + ], + "execution_count": 15, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "CWhbibHcgRR8" + }, + "source": [ + "## Cost Function\n", + "The cost function $J$ is commonly used to assess the performance of a model, and is defined with the loss function $L$ as follows:\n", + "$$\\boxed{J(\\theta)=\\sum_{i=1}^mL(h_\\theta(x^{(i)}), y^{(i)})}$$\n", + "where $h_\\theta$ is the hypothesis function i.e. the function used to predict the output." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "SSbmhW4og97t" + }, + "source": [ + "lossFunctions = {\n", + " \"l1\" : L1Loss,\n", + " \"l2\" : L2Loss,\n", + " \"hinge\" : hingeLoss,\n", + " \"cross-entropy\" : CELoss,\n", + " \"0-1\" : zeroOneLoss\n", + "}\n", + "\n", + "def cost(Z : np.ndarray, Y : np.ndarray, loss : str):\n", + " '''\n", + " Z : a numpy array of predictions.\n", + " Y : a numpy array of true values.\n", + " return : A numpy array of costs calculated for each example.\n", + " '''\n", + " loss_func = lossFunctions[loss]\n", + " # YOUR CODE HERE\n", + " J = sum(loss_func(Z,Y))\n", + " # YOUR CODE HERE\n", + " pass" + ], + "execution_count": 16, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "upsN7A0zjGqx" + }, + "source": [ + "## Gradient Descent and Back Propagation\n", + "Gradient Descent is an algorithm that minimizes the loss function by calculating it's gradient. By noting $\\alpha\\in\\mathbb{R}$ the learning rate, the update rule for gradient descent is expressed with the learning rate $\\alpha$ and the cost function $J$ as follows:\n", + "\n", + "$$\\boxed{ W \\longleftarrow W -\\alpha\\nabla J( W )}$$\n", + "​\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "AFCN-fYCqidi" + }, + "source": [ + "But we need to find the partial derivative of Loss function wrt every parameter to know what is the slight change that we need to apply to our parameters. This becomes particularly hard if we have more than 1 layer in our algorithm. Here's where **Back Propagation** comes in. It's a way to find gradients wrt every parameter using the chain rule. Backpropagation is a method to update the weights in the neural network by taking into account the actual output and the desired output. The derivative with respect to weight ww is computed using chain rule and is of the following form:\n", + "\n", + "$$\\boxed{\\frac{\\partial L(z,y)}{\\partial w}=\\frac{\\partial L(z,y)}{\\partial a}\\times\\frac{\\partial a}{\\partial z}\\times\\frac{\\partial z}{\\partial w}}$$\n", + "​\n", + " \n", + "As a result, the weight is updated as follows:\n", + "\n", + "$$\\boxed{w\\longleftarrow w-\\alpha\\frac{\\partial L(z,y)}{\\partial w}}$$\n", + "\n", + "So, In a neural network, weights are updated as follows:\n", + "\n", + "* Step 1: Take a batch of training data.\n", + "* Step 2: Perform forward propagation to obtain the corresponding loss.\n", + "* Step 3: Backpropagate the loss to get the gradients.\n", + "* Step 4: Use the gradients to update the weights of the network.\n", + "​\n", + "\n", + "Bonus Problem\n", + " \n", + "Now, Assuming that you know Back Propagation (read a bit about it, if you don't), we'll now implement an image classification model on CIFAR-10." + ] + }, + { + "cell_type": "markdown", + "source": [ + "# **Bonus Problem**\n", + "\n", + "Now, Assuming that you know Back Propagation (read a bit about it, if you don't), we'll now implement an image classification model on CIFAR-10." + ], + "metadata": { + "id": "sJoG5kkYopRN" + } + }, + { + "cell_type": "code", + "source": [ + "import tensorflow as tf \n", + " \n", + "# Display the version\n", + "print(tf.__version__) \n", + " \n", + "# other imports\n", + "import numpy as np\n", + "import matplotlib.pyplot as plt\n", + "from tensorflow.keras.layers import Input, Conv2D, Dense, Flatten, Dropout\n", + "from tensorflow.keras.layers import GlobalMaxPooling2D, MaxPooling2D\n", + "from tensorflow.keras.layers import BatchNormalization\n", + "from tensorflow.keras.models import Model" + ], + "metadata": { + "id": "_4-4RceVsor_" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "yyplk5PLEUsJ" + }, + "source": [ + "# Load in the data\n", + "cifar10 = tf.keras.datasets.cifar10\n", + " \n", + "# Distribute it to train and test set\n", + "(x_train, y_train), (x_test, y_test) = cifar10.load_data()\n", + "print(x_train.shape, y_train.shape, x_test.shape, y_test.shape)\n", + "\n", + "# Reduce pixel values\n", + "x_train, x_test = x_train / 255.0, x_test / 255.0\n", + " \n", + "# flatten the label values\n", + "y_train, y_test = y_train.flatten(), y_test.flatten()" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "qQhkATYhEkkC" + }, + "source": [ + "'''visualize data by plotting images'''\n", + "# YOUR CODE HERE\n", + "pass\n", + "# YOUR CODE HERE" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "yJgho2AEBFbx" + }, + "source": [ + "\n", + "# number of classes\n", + "K = len(set(y_train))\n", + "'''\n", + " calculate total number of classes\n", + " for output layer\n", + "'''\n", + "print(\"number of classes:\", K)\n", + "''' \n", + " Build the model using the functional API\n", + " input layer\n", + "'''\n", + "```\n", + " YOUR CODE HERE\n", + "```\n", + " \n", + "'''Hidden layer'''\n", + "# YOUR CODE HERE\n", + "pass\n", + "# YOUR CODE HERE\n", + " \n", + "\"\"\"last hidden layer i.e.. output layer\"\"\"\n", + "# YOUR CODE HERE\n", + "pass\n", + "# YOUR CODE HERE\n", + " \n", + " '''model description'''\n", + "model.summary()" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "PLc4Bay65TyA" + }, + "source": [ + "# Compile\n", + "...\n", + " YOUR CODE HERE\n", + "..." + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "# Fit\n", + "...\n", + " YOUR CODE HERE\n", + "..." + ], + "metadata": { + "id": "U0fGsDCRsQrn" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "# label mapping\n", + " \n", + "labels = '''airplane automobile bird cat deerdog frog horseship truck'''.split()\n", + " \n", + "# select the image from our test dataset\n", + "image_number = 0\n", + " \n", + "# display the image\n", + "plt.imshow(x_test[image_number])\n", + " \n", + "# load the image in an array\n", + "n = np.array(x_test[image_number])\n", + " \n", + "# reshape it\n", + "p = n.reshape(1, 32, 32, 3)\n", + " \n", + "# pass in the network for prediction and\n", + "# save the predicted label\n", + "predicted_label = labels[model.predict(p).argmax()]\n", + " \n", + "# load the original label\n", + "original_label = labels[y_test[image_number]]\n", + " \n", + "# display the result\n", + "print(\"Original label is {} and predicted label is {}\".format(\n", + " original_label, predicted_label))" + ], + "metadata": { + "id": "RDq_RE6osSh8" + }, + "execution_count": null, + "outputs": [] + } + ] +} \ No newline at end of file diff --git a/Assignment/Assignment_3/200489_Karan.ipynb b/Assignment/Assignment_3/200489_Karan.ipynb new file mode 100644 index 0000000..69a6df2 --- /dev/null +++ b/Assignment/Assignment_3/200489_Karan.ipynb @@ -0,0 +1,299 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Untitled2.ipynb", + "provenance": [], + "collapsed_sections": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "id": "g7pTz8e_i0an" + }, + "outputs": [], + "source": [ + "from keras.models import Sequential\n", + "from keras.layers import LSTM, Dense\n", + "from keras.datasets import mnist\n", + "from keras.utils import np_utils\n", + "from keras import initializers\n", + "\n", + "def init_weights(shape, name=None):\n", + " return initializations.normal(shape, scale=0.01, name=name)\n" + ] + }, + { + "cell_type": "code", + "source": [ + "batch_size = 128\n", + "nb_epoch = 50\n", + "\n", + "img_rows, img_cols = 28, 28\n", + "nb_classes = 10\n", + "\n", + "nb_lstm_outputs = 30\n", + "nb_time_steps = img_rows\n", + "dim_input_vector = img_cols" + ], + "metadata": { + "id": "OQzY5tRwi4CV" + }, + "execution_count": 8, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "(X_train, y_train), (X_test, y_test) = mnist.load_data()\n", + "print('X_train original shape:', X_train.shape)\n", + "input_shape = (nb_time_steps, dim_input_vector)\n", + "\n", + "X_train = X_train.astype('float32') / 255.\n", + "X_test = X_test.astype('float32') / 255.\n", + "Y_train = np_utils.to_categorical(y_train, nb_classes)\n", + "Y_test = np_utils.to_categorical(y_test, nb_classes)\n", + "\n", + "print('X_train shape:', X_train.shape)\n", + "print(X_train.shape[0], 'train samples')\n", + "print(X_test.shape[0], 'test samples')" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "EqKcW4pwmL6a", + "outputId": "87fee708-5ad6-48f1-9438-3ab8256b6a2a" + }, + "execution_count": 9, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "X_train original shape: (60000, 28, 28)\n", + "X_train shape: (60000, 28, 28)\n", + "60000 train samples\n", + "10000 test samples\n", + "X_train original shape: (60000, 28, 28)\n", + "X_train shape: (60000, 28, 28)\n", + "60000 train samples\n", + "10000 test samples\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "model = Sequential()\n", + "model.add(LSTM(nb_lstm_outputs, input_shape=input_shape))\n", + "model.add(Dense(nb_classes, activation='softmax'))\n", + "model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])\n", + "model.summary()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "x3Y-_TRwotyT", + "outputId": "10f5c2b8-8a1a-44ee-bdbe-7f9203dee27b" + }, + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Model: \"sequential\"\n", + "_________________________________________________________________\n", + " Layer (type) Output Shape Param # \n", + "=================================================================\n", + " lstm (LSTM) (None, 30) 7080 \n", + " \n", + " dense (Dense) (None, 10) 310 \n", + " \n", + "=================================================================\n", + "Total params: 7,390\n", + "Trainable params: 7,390\n", + "Non-trainable params: 0\n", + "_________________________________________________________________\n", + "Model: \"sequential_1\"\n", + "_________________________________________________________________\n", + " Layer (type) Output Shape Param # \n", + "=================================================================\n", + " lstm_1 (LSTM) (None, 30) 7080 \n", + " \n", + " dense_1 (Dense) (None, 10) 310 \n", + " \n", + "=================================================================\n", + "Total params: 7,390\n", + "Trainable params: 7,390\n", + "Non-trainable params: 0\n", + "_________________________________________________________________\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "history = model.fit(X_train, Y_train, epochs=nb_epoch, batch_size=batch_size, shuffle=True, verbose=1)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "SLEQlFlIo4ZG", + "outputId": "7cbfcfbb-ddae-4d42-d81d-5fb548a387a3" + }, + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Epoch 1/50\n", + "469/469 [==============================] - 17s 28ms/step - loss: 1.0906 - accuracy: 0.6473\n", + "Epoch 2/50\n", + "469/469 [==============================] - 9s 20ms/step - loss: 0.4382 - accuracy: 0.8672\n", + "Epoch 3/50\n", + "469/469 [==============================] - 10s 21ms/step - loss: 0.2973 - accuracy: 0.9109\n", + "Epoch 4/50\n", + "469/469 [==============================] - 9s 20ms/step - loss: 0.2295 - accuracy: 0.9318\n", + "Epoch 5/50\n", + "469/469 [==============================] - 10s 20ms/step - loss: 0.1896 - accuracy: 0.9437\n", + "Epoch 6/50\n", + "469/469 [==============================] - 9s 20ms/step - loss: 0.1631 - accuracy: 0.9531\n", + "Epoch 7/50\n", + "469/469 [==============================] - 9s 20ms/step - loss: 0.1438 - accuracy: 0.9579\n", + "Epoch 8/50\n", + "469/469 [==============================] - 9s 20ms/step - loss: 0.1308 - accuracy: 0.9617\n", + "Epoch 9/50\n", + "469/469 [==============================] - 10s 20ms/step - loss: 0.1179 - accuracy: 0.9653\n", + "Epoch 10/50\n", + "469/469 [==============================] - 10s 20ms/step - loss: 0.1074 - accuracy: 0.9685\n", + "Epoch 11/50\n", + "469/469 [==============================] - 10s 21ms/step - loss: 0.0996 - accuracy: 0.9707\n", + "Epoch 12/50\n", + "469/469 [==============================] - 9s 20ms/step - loss: 0.0930 - accuracy: 0.9723\n", + "Epoch 13/50\n", + "469/469 [==============================] - 9s 20ms/step - loss: 0.0870 - accuracy: 0.9739\n", + "Epoch 14/50\n", + "469/469 [==============================] - 9s 20ms/step - loss: 0.0812 - accuracy: 0.9756\n", + "Epoch 15/50\n", + "469/469 [==============================] - 9s 20ms/step - loss: 0.0765 - accuracy: 0.9769\n", + "Epoch 16/50\n", + "469/469 [==============================] - 9s 20ms/step - loss: 0.0726 - accuracy: 0.9783\n", + "Epoch 17/50\n", + "469/469 [==============================] - 9s 20ms/step - loss: 0.0691 - accuracy: 0.9793\n", + "Epoch 18/50\n", + "469/469 [==============================] - 10s 21ms/step - loss: 0.0649 - accuracy: 0.9799\n", + "Epoch 19/50\n", + "469/469 [==============================] - 10s 21ms/step - loss: 0.0623 - accuracy: 0.9809\n", + "Epoch 20/50\n", + "469/469 [==============================] - 9s 20ms/step - loss: 0.0596 - accuracy: 0.9822\n", + "Epoch 21/50\n", + "469/469 [==============================] - 9s 20ms/step - loss: 0.0573 - accuracy: 0.9827\n", + "Epoch 22/50\n", + "469/469 [==============================] - 11s 22ms/step - loss: 0.0550 - accuracy: 0.9833\n", + "Epoch 23/50\n", + "469/469 [==============================] - 9s 20ms/step - loss: 0.0519 - accuracy: 0.9842\n", + "Epoch 24/50\n", + "469/469 [==============================] - 9s 20ms/step - loss: 0.0510 - accuracy: 0.9844\n", + "Epoch 25/50\n", + "469/469 [==============================] - 9s 20ms/step - loss: 0.0493 - accuracy: 0.9846\n", + "Epoch 26/50\n", + "469/469 [==============================] - 9s 20ms/step - loss: 0.0480 - accuracy: 0.9856\n", + "Epoch 27/50\n", + "469/469 [==============================] - 10s 20ms/step - loss: 0.0462 - accuracy: 0.9859\n", + "Epoch 28/50\n", + "469/469 [==============================] - 9s 20ms/step - loss: 0.0444 - accuracy: 0.9869\n", + "Epoch 29/50\n", + "469/469 [==============================] - 10s 20ms/step - loss: 0.0427 - accuracy: 0.9869\n", + "Epoch 30/50\n", + "469/469 [==============================] - 10s 20ms/step - loss: 0.0421 - accuracy: 0.9875\n", + "Epoch 31/50\n", + "469/469 [==============================] - 10s 21ms/step - loss: 0.0399 - accuracy: 0.9886\n", + "Epoch 32/50\n", + "469/469 [==============================] - 9s 20ms/step - loss: 0.0396 - accuracy: 0.9878\n", + "Epoch 33/50\n", + "469/469 [==============================] - 9s 20ms/step - loss: 0.0382 - accuracy: 0.9885\n", + "Epoch 34/50\n", + "469/469 [==============================] - 9s 20ms/step - loss: 0.0370 - accuracy: 0.9888\n", + "Epoch 35/50\n", + "469/469 [==============================] - 9s 20ms/step - loss: 0.0353 - accuracy: 0.9896\n", + "Epoch 36/50\n", + "469/469 [==============================] - 9s 20ms/step - loss: 0.0352 - accuracy: 0.9896\n", + "Epoch 37/50\n", + "469/469 [==============================] - 9s 20ms/step - loss: 0.0334 - accuracy: 0.9902\n", + "Epoch 38/50\n", + "469/469 [==============================] - 14s 30ms/step - loss: 0.0324 - accuracy: 0.9902\n", + "Epoch 39/50\n", + "469/469 [==============================] - 9s 20ms/step - loss: 0.0325 - accuracy: 0.9901\n", + "Epoch 40/50\n", + "469/469 [==============================] - 9s 20ms/step - loss: 0.0306 - accuracy: 0.9906\n", + "Epoch 41/50\n", + "469/469 [==============================] - 9s 20ms/step - loss: 0.0306 - accuracy: 0.9905\n", + "Epoch 42/50\n", + "469/469 [==============================] - 9s 20ms/step - loss: 0.0304 - accuracy: 0.9911\n", + "Epoch 43/50\n", + "469/469 [==============================] - 9s 20ms/step - loss: 0.0288 - accuracy: 0.9914\n", + "Epoch 44/50\n", + "469/469 [==============================] - 10s 21ms/step - loss: 0.0283 - accuracy: 0.9916\n", + "Epoch 45/50\n", + "469/469 [==============================] - 10s 22ms/step - loss: 0.0271 - accuracy: 0.9919\n", + "Epoch 46/50\n", + "469/469 [==============================] - 10s 21ms/step - loss: 0.0267 - accuracy: 0.9921\n", + "Epoch 47/50\n", + "469/469 [==============================] - 10s 22ms/step - loss: 0.0269 - accuracy: 0.9919\n", + "Epoch 48/50\n", + "469/469 [==============================] - 10s 20ms/step - loss: 0.0253 - accuracy: 0.9925\n", + "Epoch 49/50\n", + "469/469 [==============================] - 9s 20ms/step - loss: 0.0248 - accuracy: 0.9926\n", + "Epoch 50/50\n", + "469/469 [==============================] - 10s 20ms/step - loss: 0.0247 - accuracy: 0.9925\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "evaluation = model.evaluate(X_test, Y_test, batch_size=batch_size, verbose=1)\n", + "print('Summary: Loss over the test dataset: %.2f, Accuracy: %.2f' % (evaluation[0], evaluation[1]))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "m4uuf0FNqTmy", + "outputId": "1642f901-5863-4a40-89cb-b5c04528f8d6" + }, + "execution_count": 11, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "79/79 [==============================] - 1s 10ms/step - loss: 0.0687 - accuracy: 0.9812\n", + "Summary: Loss over the test dataset: 0.07, Accuracy: 0.98\n", + "79/79 [==============================] - 2s 9ms/step - loss: 2.3058 - accuracy: 0.0618\n", + "Summary: Loss over the test dataset: 2.31, Accuracy: 0.06\n" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/Assignment/Assignment_3/abc b/Assignment/Assignment_3/abc new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Assignment/Assignment_3/abc @@ -0,0 +1 @@ +