{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercises week 42\n", "\n", "**Python Code can be found at https://github.uio.no/larsbog/FYS-STK4155**\n", "\n", "**October 13-17, 2025**\n", "\n", "Date: **Deadline is Friday October 17 at midnight**\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Overarching aims of the exercises this week\n", "\n", "The aim of the exercises this week is to train the neural network you implemented last week.\n", "\n", "To train neural networks, we use gradient descent, since there is no analytical expression for the optimal parameters. This means you will need to compute the gradient of the cost function wrt. the network parameters. And then you will need to implement some gradient method.\n", "\n", "You will begin by computing gradients for a network with one layer, then two layers, then any number of layers. Keeping track of the shapes and doing things step by step will be very important this week.\n", "\n", "We recommend that you do the exercises this week by editing and running this notebook file, as it includes some checks along the way that you have implemented the neural network correctly, and running small parts of the code at a time will be important for understanding the methods. If you have trouble running a notebook, you can run this notebook in google colab instead(https://colab.research.google.com/drive/1FfvbN0XlhV-lATRPyGRTtTBnJr3zNuHL#offline=true&sandboxMode=true), though we recommend that you set up VSCode and your python environment to run code like this locally.\n", "\n", "First, some setup code that you will need.\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import autograd.numpy as np # We need to use this numpy wrapper to make automatic differentiation work later\n", "from autograd import grad, elementwise_grad\n", "from sklearn import datasets\n", "import matplotlib.pyplot as plt\n", "from sklearn.metrics import accuracy_score\n", "\n", "\n", "# Defining some activation functions\n", "def ReLU(z):\n", " return np.where(z > 0, z, 0)\n", "\n", "\n", "# Derivative of the ReLU function\n", "def ReLU_der(z):\n", " return np.where(z > 0, 1, 0)\n", "\n", "\n", "def sigmoid(z):\n", " return 1 / (1 + np.exp(-z))\n", "\n", "\n", "def mse(predict, target):\n", " return np.mean((predict - target) ** 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 1 - Understand the feed forward pass\n", "\n", "**a)** Complete last weeks' exercises if you haven't already (recommended).\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 2 - Gradient with one layer using autograd\n", "\n", "For the first few exercises, we will not use batched inputs. Only a single input vector is passed through the layer at a time.\n", "\n", "In this exercise you will compute the gradient of a single layer. You only need to change the code in the cells right below an exercise, the rest works out of the box. Feel free to make changes and see how stuff works though!\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**a)** If the weights and bias of a layer has shapes (10, 4) and (10), what will the shapes of the gradients of the cost function wrt. these weights and this bias be?\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "