{ "cells": [ { "cell_type": "markdown", "id": "4b4c06bc", "metadata": {}, "source": [ "\n", "\n" ] }, { "cell_type": "markdown", "id": "bcb25e64", "metadata": {}, "source": [ "# Exercises week 41\n", "\n", "**Python Code can be found at https://github.uio.no/larsbog/FYS-STK4155**\n", "\n", "\n", "**October 6-10, 2025**\n", "\n", "Date: **Deadline is Friday October 10 at midnight**\n" ] }, { "cell_type": "markdown", "id": "bb01f126", "metadata": {}, "source": [ "# Overarching aims of the exercises this week\n", "\n", "This week, you will implement the entire feed-forward pass of a neural network! Next week you will compute the gradient of the network by implementing back-propagation manually, and by using autograd which does back-propagation for you (much easier!). Next week, you will also use the gradient to optimize the network with a gradient method! However, there is an optional exercise this week to get started on training the network and getting good results!\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 pieces of the feed-forward pass correctly, and running small parts of the code at a time will be important for understanding the methods.\n", "\n", "If you have trouble running a notebook, you can run this notebook in google colab instead (https://colab.research.google.com/drive/1zKibVQf-iAYaAn2-GlKfgRjHtLnPlBX4#offline=true&sandboxMode=true), an updated link will be provided on the course discord (you can also send an email to k.h.fredly@fys.uio.no if you encounter any trouble), though we recommend that you set up VSCode and your python environment to run code like this locally.\n", "\n", "First, here are some functions you are going to need, don't change this cell. If you are unable to import autograd, just swap in normal numpy until you want to do the final optional exercise.\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "c6f61b09", "metadata": {}, "outputs": [], "source": [ "import autograd.numpy as np # We need to use this numpy wrapper to make automatic differentiation work later\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", "def sigmoid(z):\n", " return 1 / (1 + np.exp(-z))\n", "\n", "\n", "def softmax(z):\n", " \"\"\"Compute softmax values for each set of scores in the rows of the matrix z.\n", " Used with batched input data.\"\"\"\n", " e_z = np.exp(z - np.max(z, axis=0))\n", " return e_z / np.sum(e_z, axis=1)[:, np.newaxis]\n", "\n", "\n", "def softmax_vec(z):\n", " \"\"\"Compute softmax values for each set of scores in the vector z.\n", " Use this function when you use the activation function on one vector at a time\"\"\"\n", " e_z = np.exp(z - np.max(z))\n", " return e_z / np.sum(e_z)" ] }, { "cell_type": "markdown", "id": "6248ec53", "metadata": {}, "source": [ "# Exercise 1\n", "\n", "In this exercise you will compute the activation of the first 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": "code", "execution_count": 2, "id": "37f30740", "metadata": {}, "outputs": [], "source": [ "np.random.seed(2024)\n", "\n", "x = np.random.randn(2) # network input. This is a single input with two features\n", "W1 = np.random.randn(4, 2) # first layer weights" ] }, { "cell_type": "markdown", "id": "4ed2cf3d", "metadata": {}, "source": [ "**a)** Given the shape of the first layer weight matrix, what is the input shape of the neural network? What is the output shape of the first layer?\n" ] }, { "cell_type": "markdown", "id": "b3fc5895", "metadata": {}, "source": [ "