{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "view-in-github"
},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "2_Zny8rw4lon"
},
"source": [
"# (Exercise) Artificial Neural Networks with Keras\n",
"\n",
"This notebook was designed to be run on Google Colab and we recommend clicking on the Google Colab badge to proceed."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "nD8r1k_osIxa"
},
"source": [
"\n",
"\n",
"
1) Takes in an input dataset and its labels, a number of rows, and a number of columns**\n", "\n", "*Hint 1: You can use the `rnd_seed.integers()` generator to generate a set of integers between 0 and the number of samples, with a size of (rows,columns). [Here is some documentation that can help](https://numpy.org/doc/stable/reference/random/generator.html#simple-random-data). It's best practice to take in the random generator as an argument for your function.*\n", "\n", "*Hint 2: You can use matplotlib's `fig, axes = plt.subplots()` to make a grid of axes and call the `imshow()` method on each ax in order to plot the digit. It is recommended that you use the `cmap='binary'` argument in imshow to print the digits in black and white*. Click on the links for the documentation to [`plt.sublopts()`](https://matplotlib.org/3.5.0/api/_as_gen/matplotlib.pyplot.subplots.html), [`plt.imshow()`](https://matplotlib.org/3.5.0/api/_as_gen/matplotlib.pyplot.imshow.html), and [the colormaps (i.e., cmap values)](https://matplotlib.org/stable/gallery/color/colormap_reference.html) available in matplotlib.\n", "\n", "*Hint 3: You can iterate using numpy `ndenumerate()` method, which will return the n-dimensional index of the array and the element located there. This will be useful when iterating through the indices you generated and plotting the corresponding digit and label*" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "id": "zcDF_uMuBKBO" }, "outputs": [], "source": [ "#@title Hint 4: Code Snippet, if you're feeling stuck\n", "\n", "'''\n", "def sample_plotter(X, y, n_rows, n_columns, rnd_gen):\n", " assert type(X) == type(np.empty(0))\n", " indices = rnd_gen.integers(0,X.shape[0], size=(n_rows, n_columns))\n", "\n", " fig, axes = plt.subplots(n_rows, n_columns, figsize=(8,6))\n", "\n", " for idx, element in np.ndenumerate(indices):\n", " axes[idx].imshow(X[element], cmap='binary')\n", " axes[idx].axis('off')\n", " axes[idx].title.set_text(y[element])\n", " return\n", "''';" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "qtmdxBHCEs_o" }, "outputs": [], "source": [ "def sample_plotter(___, ___, ___, ___):\n", "\n", " # Create a set of indices to access the sample images/labels\n", "\n", " # Create a figure with n_rows and n_columns\n", "\n", " # Plot each selected digit\n", " for in :\n", "\n", "\n", " return None" ] }, { "cell_type": "markdown", "metadata": { "id": "Txut6AxUEMac" }, "source": [ "Now that our function is defined, let's go ahead and print out a 4 row by 8 column sample from each dataset.\n", "\n", "## Q5) Grab a 4x8 sample of digits from each dataset and print out the image and labels" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "YZoG0jWzELBI" }, "outputs": [], "source": [ "#Write your code here!" ] }, { "cell_type": "markdown", "metadata": { "id": "HLkcWwScg-nk" }, "source": [ "We're now ready to start developing our neural network. The first thing that we want to do is figure out an appropriate learning rate for our model - after all, we want to choose one that converges to a solution *and* is the least computationally expensive possible.\n", "\n", "Let's start by setting up a keras *callback* [(click here for the documentation)](https://keras.io/api/callbacks/), a type of object that will allow us to change the learning rate after every iteration (i.e., after every batch of data). We will set up what is called an exponential learning rate (that is, the learning will increase by a factor of $k$ after each iteration). Expressed mathematically,\n", "\\begin{align}\n", "\\eta_{\\scriptsize{t}} = \\eta_{\\scriptsize{0}} \\, \\cdot \\, k^{\\scriptsize{t}}\n", "\\end{align}\n", "where $t$ is the current iteration.\n", "\n", "As a reminder, an epoch is an iteration through the entire training dataset, while a batch is an iteration through a predefined subset of . It's important to make this distinction, as ML algorithms are often trained in batches when dealing with large datasets, and we *normally* do not want to change the learning rate in between batches during model training. However, we will do so during this evaluation phase in order to determine an adequate learning rate.\n", "\n", "We will therefore set a callback that will do two things after the end of each batch:\n", "\n", "> 1) Keep a track of the losses
2) Prints out a random `n_rows` by `n_columns` sample of images with their labels