update
This commit is contained in:
@@ -0,0 +1,641 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b209e219",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"<!-- HTML file automatically generated from DocOnce source (https://github.com/doconce/doconce/)\n",
|
||||
"doconce format html Project1.do.txt -->\n",
|
||||
"<!-- dom:TITLE: Project 1 on Machine Learning, deadline October 6 (midnight), 2025 -->"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "6fa4c4bc",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"# Project 1 on Machine Learning, deadline October 6 (midnight), 2025\n",
|
||||
"**Data Analysis and Machine Learning FYS-STK3155/FYS4155**, University of Oslo, Norway\n",
|
||||
"\n",
|
||||
"Date: **September 2**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "735b16c4",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"## Preamble: Note on writing reports, using reference material, AI and other tools\n",
|
||||
"\n",
|
||||
"We want you to answer the three different projects by handing in\n",
|
||||
"reports written like a standard scientific/technical report. The\n",
|
||||
"links at\n",
|
||||
"<https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects>\n",
|
||||
"contain more information. There you can find examples of previous\n",
|
||||
"reports, the projects themselves, how we rade reports etc. How to\n",
|
||||
"write reports will also be discussed during the various lab\n",
|
||||
"sessions. Please do ask us if you are in doubt.\n",
|
||||
"\n",
|
||||
"When using codes and material from other sources, you should refer to\n",
|
||||
"these in the bibliography of your report, indicating wherefrom you for\n",
|
||||
"example got the code, whether this is from the lecture notes,\n",
|
||||
"softwares like Scikit-Learn, TensorFlow, PyTorch or other sources. These sources\n",
|
||||
"should always be cited correctly. How to cite some\n",
|
||||
"of the libraries is often indicated from their corresponding GitHub\n",
|
||||
"sites or websites, see for example how to cite Scikit-Learn at\n",
|
||||
"<https://scikit-learn.org/dev/about.html>.\n",
|
||||
"\n",
|
||||
"We enocurage you to use tools like\n",
|
||||
"[ChatGPT](https://openai.com/chatgpt/) or similar in writing the report. If you use for example ChatGPT,\n",
|
||||
"please do cite it properly and include (if possible) your questions and answers as an addition to the report. This can\n",
|
||||
"be uploaded to for example your website, GitHub/GitLab or similar as supplemental material.\n",
|
||||
"\n",
|
||||
"If you would like to study other data sets, feel free to propose other\n",
|
||||
"sets. What we have proposed here are mere suggestions from our\n",
|
||||
"side. If you opt for another data set, consider using a set which has\n",
|
||||
"been studied in the scientific literature. This makes it easier for\n",
|
||||
"you to compare and analyze your results. Comparing with existing\n",
|
||||
"results from the scientific literature is also an essential element of\n",
|
||||
"the scientific discussion. The University of California at Irvine\n",
|
||||
"with its Machine Learning repository at\n",
|
||||
"<https://archive.ics.uci.edu/ml/index.php> is an excellent site to\n",
|
||||
"look up for examples and\n",
|
||||
"inspiration. [Kaggle.com](https://www.kaggle.com/) is an equally\n",
|
||||
"interesting site. Feel free to explore these sites. When selecting\n",
|
||||
"other data sets, make sure these are sets used for regression problems\n",
|
||||
"(not classification)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "0b7956ca",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"## Regression analysis and resampling methods\n",
|
||||
"\n",
|
||||
"The main aim of this project is to study in more detail various\n",
|
||||
"regression methods, including Ordinary Least Squares (OLS) reegression, Ridge regression and LASSO regression.\n",
|
||||
"In addition to the scientific part, in this course we want also to\n",
|
||||
"give you an experience in writing scientific reports.\n",
|
||||
"\n",
|
||||
"We will study how to fit polynomials to specific\n",
|
||||
"one-dimensional functions (feel free to replace the suggested function with more complicated ones).\n",
|
||||
"\n",
|
||||
"We will use Runge's function (see <https://en.wikipedia.org/wiki/Runge%27s_phenomenon> for a discussion). The one-dimensional function we will study is"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "28ba3d22",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"f(x) = \\frac{1}{1+25x^2}.\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "9a3e10ba",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"Our first step will be to perform an OLS regression analysis of this\n",
|
||||
"function, trying out a polynomial fit with an $x$ dependence of the\n",
|
||||
"form $[x,x^2,\\dots]$. You can use a uniform distribution to set up the\n",
|
||||
"arrays of values for $x \\in [-1,1]$, or alternatively use a fixed step size.\n",
|
||||
"Thereafter we will repeat many of the same steps when using the Ridge and Lasso regression methods,\n",
|
||||
"introducing thereby a dependence on the hyperparameter (penalty) $\\lambda$.\n",
|
||||
"\n",
|
||||
"We will also include bootstrap as a resampling technique in order to\n",
|
||||
"study the so-called **bias-variance tradeoff**. After that we will\n",
|
||||
"include the so-called cross-validation technique."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "8aa547a5",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"### Part a : Ordinary Least Square (OLS) for the Runge function\n",
|
||||
"\n",
|
||||
"We will generate our own dataset for abovementioned function\n",
|
||||
"$\\mathrm{Runge}(x)$ function with $x\\in [-1,1]$. You should explore also the addition\n",
|
||||
"of an added stochastic noise to this function using the normal\n",
|
||||
"distribution $N(0,1)$.\n",
|
||||
"\n",
|
||||
"*Write your own code* (using for example the pseudoinverse function **pinv** from **Numpy** ) and perform a standard **ordinary least square regression**\n",
|
||||
"analysis using polynomials in $x$ up to order $15$ or higher. Explore the dependence on the number of data points and the polynomial degree.\n",
|
||||
"\n",
|
||||
"Evaluate the mean Squared error (MSE)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "68fbf03d",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"MSE(\\boldsymbol{y},\\tilde{\\boldsymbol{y}}) = \\frac{1}{n}\n",
|
||||
"\\sum_{i=0}^{n-1}(y_i-\\tilde{y}_i)^2,\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b49509bc",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"and the $R^2$ score function. If $\\tilde{\\boldsymbol{y}}_i$ is the predicted\n",
|
||||
"value of the $i-th$ sample and $y_i$ is the corresponding true value,\n",
|
||||
"then the score $R^2$ is defined as"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "0fa4ffc6",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"R^2(\\boldsymbol{y}, \\tilde{\\boldsymbol{y}}) = 1 - \\frac{\\sum_{i=0}^{n - 1} (y_i - \\tilde{y}_i)^2}{\\sum_{i=0}^{n - 1} (y_i - \\bar{y})^2},\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "ce462b32",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"where we have defined the mean value of $\\boldsymbol{y}$ as"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "a5fbef36",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\bar{y} = \\frac{1}{n} \\sum_{i=0}^{n - 1} y_i.\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "a6afe9cb",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree 15).\n",
|
||||
"Plot also the parameters $\\theta$ as you increase the order of the polynomial. Comment your results.\n",
|
||||
"\n",
|
||||
"Your code has to include a scaling/centering of the data (for example by\n",
|
||||
"subtracting the mean value), and\n",
|
||||
"a split of the data in training and test data. For the scaling you can\n",
|
||||
"either write your own code or use for example the function for\n",
|
||||
"splitting training data provided by the library **Scikit-Learn** (make\n",
|
||||
"sure you have installed it). This function is called\n",
|
||||
"$train\\_test\\_split$. **You should present a critical discussion of why and how you have scaled or not scaled the data**.\n",
|
||||
"\n",
|
||||
"It is normal in essentially all Machine Learning studies to split the\n",
|
||||
"data in a training set and a test set (eventually also an additional\n",
|
||||
"validation set). There\n",
|
||||
"is no explicit recipe for how much data should be included as training\n",
|
||||
"data and say test data. An accepted rule of thumb is to use\n",
|
||||
"approximately $2/3$ to $4/5$ of the data as training data.\n",
|
||||
"\n",
|
||||
"You can easily reuse the solutions to your exercises from week 35.\n",
|
||||
"See also the lecture slides from week 35 and week 36.\n",
|
||||
"\n",
|
||||
"On scaling, we recommend reading the following section from the scikit-learn software description, see <https://scikit-learn.org/stable/auto_examples/preprocessing/plot_all_scaling.html#plot-all-scaling-standard-scaler-section>."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "3be10f68",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"### Part b: Adding Ridge regression for the Runge function\n",
|
||||
"\n",
|
||||
"Write your own code for the Ridge method as done in the previous\n",
|
||||
"exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the results from the exercise set from week 36 is something you can reuse here.\n",
|
||||
"\n",
|
||||
"Perform the same analysis as you did in the previous exercise but now for different values of $\\lambda$. Compare and\n",
|
||||
"analyze your results with those obtained in part a) with the OLS method. Study the\n",
|
||||
"dependence on $\\lambda$."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "caa7909c",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"### Part c: Writing your own gradient descent code\n",
|
||||
"\n",
|
||||
"Replace now the analytical expressions for the optimal parameters\n",
|
||||
"$\\boldsymbol{\\theta}$ with your own gradient descent code. In this exercise we\n",
|
||||
"focus only on the simplest gradient descent approach with a fixed\n",
|
||||
"learning rate (see the exercises from week 37 and the lecture notes\n",
|
||||
"from week 36).\n",
|
||||
"\n",
|
||||
"Study and compare your results from parts a) and b) with your gradient\n",
|
||||
"descent approch. Discuss in particular the role of the learning rate."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "3aac4df1",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"### Part d: Including momentum and more advanced ways to update the learning the rate\n",
|
||||
"\n",
|
||||
"We keep our focus on OLS and Ridge regression and update our code for\n",
|
||||
"the gradient descent method by including **momentum**, **ADAgrad**,\n",
|
||||
"**RMSprop** and **ADAM** as methods fro iteratively updating your learning\n",
|
||||
"rate. Discuss the results and compare the different methods applied to\n",
|
||||
"the one-dimensional Runge function. The lecture notes from week 37 contain several examples on how to implement these methods."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d0862a53",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"### Part e: Writing our own code for Lasso regression\n",
|
||||
"\n",
|
||||
"LASSO regression (see lecture slides from week 36 and week 37)\n",
|
||||
"represents our first encounter with a machine learning method which\n",
|
||||
"cannot be solved through analytical expressions (as in OLS and Ridge regression). Use the gradient\n",
|
||||
"descent methods you developed in parts c) and d) to solve the LASSO\n",
|
||||
"optimization problem. You can compare your results with \n",
|
||||
"the functionalities of **Scikit-Learn**.\n",
|
||||
"\n",
|
||||
"Discuss (critically) your results for the Runge function from OLS,\n",
|
||||
"Ridge and LASSO regression using the various gradient descent\n",
|
||||
"approaches."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "9170032e",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"### Part f: Stochastic gradient descent\n",
|
||||
"\n",
|
||||
"Our last gradient step is to include stochastic gradient descent using\n",
|
||||
"the same methods to update the learning rates as in parts c-e).\n",
|
||||
"Compare and discuss your results with and without stochastic gradient\n",
|
||||
"and give a critical assessment of the various methods."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "bacd1035",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"### Part g: Bias-variance trade-off and resampling techniques\n",
|
||||
"\n",
|
||||
"Our aim here is to study the bias-variance trade-off by implementing\n",
|
||||
"the **bootstrap** resampling technique. **We will only use the simpler\n",
|
||||
"ordinary least squares here**.\n",
|
||||
"\n",
|
||||
"With a code which does OLS and includes resampling techniques, \n",
|
||||
"we will now discuss the bias-variance trade-off in the context of\n",
|
||||
"continuous predictions such as regression. However, many of the\n",
|
||||
"intuitions and ideas discussed here also carry over to classification\n",
|
||||
"tasks and basically all Machine Learning algorithms. \n",
|
||||
"\n",
|
||||
"Before you perform an analysis of the bias-variance trade-off on your\n",
|
||||
"test data, make first a figure similar to Fig. 2.11 of Hastie,\n",
|
||||
"Tibshirani, and Friedman. Figure 2.11 of this reference displays only\n",
|
||||
"the test and training MSEs. The test MSE can be used to indicate\n",
|
||||
"possible regions of low/high bias and variance. You will most likely\n",
|
||||
"not get an equally smooth curve! You may also need to increase the\n",
|
||||
"polynomial order and play around with the number of data points as\n",
|
||||
"well (see also the exercise set from week 35).\n",
|
||||
"\n",
|
||||
"With this result we move on to the bias-variance trade-off analysis.\n",
|
||||
"\n",
|
||||
"Consider a\n",
|
||||
"dataset $\\mathcal{L}$ consisting of the data\n",
|
||||
"$\\mathbf{X}_\\mathcal{L}=\\{(y_j, \\boldsymbol{x}_j), j=0\\ldots n-1\\}$.\n",
|
||||
"\n",
|
||||
"We assume that the true data is generated from a noisy model"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b871ec69",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\boldsymbol{y}=f(\\boldsymbol{x}) + \\boldsymbol{\\epsilon}.\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b47c19bc",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"Here $\\epsilon$ is normally distributed with mean zero and standard\n",
|
||||
"deviation $\\sigma^2$.\n",
|
||||
"\n",
|
||||
"In our derivation of the ordinary least squares method we defined then\n",
|
||||
"an approximation to the function $f$ in terms of the parameters\n",
|
||||
"$\\boldsymbol{\\theta}$ and the design matrix $\\boldsymbol{X}$ which embody our model,\n",
|
||||
"that is $\\boldsymbol{\\tilde{y}}=\\boldsymbol{X}\\boldsymbol{\\theta}$.\n",
|
||||
"\n",
|
||||
"The parameters $\\boldsymbol{\\theta}$ are in turn found by optimizing the mean\n",
|
||||
"squared error via the so-called cost function"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "6db622c2",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"C(\\boldsymbol{X},\\boldsymbol{\\theta}) =\\frac{1}{n}\\sum_{i=0}^{n-1}(y_i-\\tilde{y}_i)^2=\\mathbb{E}\\left[(\\boldsymbol{y}-\\boldsymbol{\\tilde{y}})^2\\right].\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "5a7eb70d",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"Here the expected value $\\mathbb{E}$ is the sample value. \n",
|
||||
"\n",
|
||||
"Show that you can rewrite this in terms of a term which contains the\n",
|
||||
"variance of the model itself (the so-called variance term), a term\n",
|
||||
"which measures the deviation from the true data and the mean value of\n",
|
||||
"the model (the bias term) and finally the variance of the noise.\n",
|
||||
"\n",
|
||||
"That is, show that"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d50292fe",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\mathbb{E}\\left[(\\boldsymbol{y}-\\boldsymbol{\\tilde{y}})^2\\right]=\\mathrm{Bias}[\\tilde{y}]+\\mathrm{var}[\\tilde{y}]+\\sigma^2,\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "50fa641f",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"with (we approximate $f(\\boldsymbol{x})\\approx \\boldsymbol{y}$)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "2bd429c9",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\mathrm{Bias}[\\tilde{y}]=\\mathbb{E}\\left[\\left(\\boldsymbol{y}-\\mathbb{E}\\left[\\boldsymbol{\\tilde{y}}\\right]\\right)^2\\right],\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "737c2819",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"and"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "41ef92ef",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\mathrm{var}[\\tilde{y}]=\\mathbb{E}\\left[\\left(\\tilde{\\boldsymbol{y}}-\\mathbb{E}\\left[\\boldsymbol{\\tilde{y}}\\right]\\right)^2\\right]=\\frac{1}{n}\\sum_i(\\tilde{y}_i-\\mathbb{E}\\left[\\boldsymbol{\\tilde{y}}\\right])^2.\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b948dab0",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"**Important note**: Since the function $f(x)$ is unknown, in order to be able to evalute the bias, we replace $f(\\boldsymbol{x})$ in the expression for the bias with $\\boldsymbol{y}$. \n",
|
||||
"\n",
|
||||
"The answer to this exercise should be included in the theory part of\n",
|
||||
"the report. This exercise is also part of the weekly exercises of\n",
|
||||
"week 38. Explain what the terms mean and discuss their\n",
|
||||
"interpretations.\n",
|
||||
"\n",
|
||||
"Perform then a bias-variance analysis of the Runge function by\n",
|
||||
"studying the MSE value as function of the complexity of your model.\n",
|
||||
"\n",
|
||||
"Discuss the bias and variance trade-off as function\n",
|
||||
"of your model complexity (the degree of the polynomial) and the number\n",
|
||||
"of data points, and possibly also your training and test data using the **bootstrap** resampling method.\n",
|
||||
"You can follow the code example in the jupyter-book at <https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/chapter3.html#the-bias-variance-tradeoff>."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "6a0548bf",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"### Part h): Cross-validation as resampling techniques, adding more complexity\n",
|
||||
"\n",
|
||||
"The aim here is to implement another widely popular\n",
|
||||
"resampling technique, the so-called cross-validation method. \n",
|
||||
"\n",
|
||||
"Implement the $k$-fold cross-validation algorithm (feel free to use\n",
|
||||
"the functionality of **Scikit-Learn** or write your own code) and\n",
|
||||
"evaluate again the MSE function resulting from the test folds.\n",
|
||||
"\n",
|
||||
"Compare the MSE you get from your cross-validation code with the one\n",
|
||||
"you got from your **bootstrap** code from the previous exercise. Comment and interpret your results. \n",
|
||||
"\n",
|
||||
"In addition to using the ordinary least squares method, you should\n",
|
||||
"include both Ridge and Lasso regression in the final analysis."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "df9845cb",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"## Background literature\n",
|
||||
"\n",
|
||||
"1. For a discussion and derivation of the variances and mean squared errors using linear regression, see the [Lecture notes on ridge regression by Wessel N. van Wieringen](https://arxiv.org/abs/1509.09169)\n",
|
||||
"\n",
|
||||
"2. The textbook of [Trevor Hastie, Robert Tibshirani, Jerome H. Friedman, The Elements of Statistical Learning, Springer](https://www.springer.com/gp/book/9780387848570), chapters 3 and 7 are the most relevant ones for the analysis here."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b9e04791",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"## Introduction to numerical projects\n",
|
||||
"\n",
|
||||
"Here follows a brief recipe and recommendation on how to answer the various questions when preparing your answers. \n",
|
||||
"\n",
|
||||
" * Give a short description of the nature of the problem and the eventual numerical methods you have used.\n",
|
||||
"\n",
|
||||
" * Describe the algorithm you have used and/or developed. Here you may find it convenient to use pseudocoding. In many cases you can describe the algorithm in the program itself.\n",
|
||||
"\n",
|
||||
" * Include the source code of your program. Comment your program properly. You should have the code at your GitHub/GitLab link. You can also place the code in an appendix of your report.\n",
|
||||
"\n",
|
||||
" * If possible, try to find analytic solutions, or known limits in order to test your program when developing the code.\n",
|
||||
"\n",
|
||||
" * Include your results either in figure form or in a table. Remember to label your results. All tables and figures should have relevant captions and labels on the axes.\n",
|
||||
"\n",
|
||||
" * Try to evaluate the reliabilty and numerical stability/precision of your results. If possible, include a qualitative and/or quantitative discussion of the numerical stability, eventual loss of precision etc.\n",
|
||||
"\n",
|
||||
" * Try to give an interpretation of you results in your answers to the problems.\n",
|
||||
"\n",
|
||||
" * Critique: if possible include your comments and reflections about the exercise, whether you felt you learnt something, ideas for improvements and other thoughts you've made when solving the exercise. We wish to keep this course at the interactive level and your comments can help us improve it.\n",
|
||||
"\n",
|
||||
" * Try to establish a practice where you log your work at the computerlab. You may find such a logbook very handy at later stages in your work, especially when you don't properly remember what a previous test version of your program did. Here you could also record the time spent on solving the exercise, various algorithms you may have tested or other topics which you feel worthy of mentioning."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "3fab6237",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"## Format for electronic delivery of report and programs\n",
|
||||
"\n",
|
||||
"The preferred format for the report is a PDF file. You can also use DOC or postscript formats or as an ipython notebook file. As programming language we prefer that you choose between C/C++, Fortran2008, Julia or Python. The following prescription should be followed when preparing the report:\n",
|
||||
"\n",
|
||||
" * Use Canvas to hand in your projects, log in at <https://www.uio.no/english/services/it/education/canvas/> with your normal UiO username and password.\n",
|
||||
"\n",
|
||||
" * Upload **only** the report file or the link to your GitHub/GitLab or similar typo of repos! For the source code file(s) you have developed please provide us with your link to your GitHub/GitLab or similar domain. The report file should include all of your discussions and a list of the codes you have developed. Do not include library files which are available at the course homepage, unless you have made specific changes to them.\n",
|
||||
"\n",
|
||||
" * In your GitHub/GitLab or similar repository, please include a folder which contains selected results. These can be in the form of output from your code for a selected set of runs and input parameters.\n",
|
||||
"\n",
|
||||
"Finally, \n",
|
||||
"we encourage you to collaborate. Optimal working groups consist of \n",
|
||||
"2-3 students. You can then hand in a common report."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "3388eb60",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"## Software and needed installations\n",
|
||||
"\n",
|
||||
"If you have Python installed (we recommend Python3) and you feel pretty familiar with installing different packages, \n",
|
||||
"we recommend that you install the following Python packages via **pip** as\n",
|
||||
"1. pip install numpy scipy matplotlib ipython scikit-learn tensorflow sympy pandas pillow\n",
|
||||
"\n",
|
||||
"For Python3, replace **pip** with **pip3**.\n",
|
||||
"\n",
|
||||
"See below for a discussion of **tensorflow** and **scikit-learn**. \n",
|
||||
"\n",
|
||||
"For OSX users we recommend also, after having installed Xcode, to install **brew**. Brew allows \n",
|
||||
"for a seamless installation of additional software via for example\n",
|
||||
"1. brew install python3\n",
|
||||
"\n",
|
||||
"For Linux users, with its variety of distributions like for example the widely popular Ubuntu distribution\n",
|
||||
"you can use **pip** as well and simply install Python as \n",
|
||||
"1. sudo apt-get install python3 (or python for python2.7)\n",
|
||||
"\n",
|
||||
"etc etc. \n",
|
||||
"\n",
|
||||
"If you don't want to install various Python packages with their dependencies separately, we recommend two widely used distrubutions which set up all relevant dependencies for Python, namely\n",
|
||||
"1. [Anaconda](https://docs.anaconda.com/) Anaconda is an open source distribution of the Python and R programming languages for large-scale data processing, predictive analytics, and scientific computing, that aims to simplify package management and deployment. Package versions are managed by the package management system **conda**\n",
|
||||
"\n",
|
||||
"2. [Enthought canopy](https://www.enthought.com/product/canopy/) is a Python distribution for scientific and analytic computing distribution and analysis environment, available for free and under a commercial license.\n",
|
||||
"\n",
|
||||
"Popular software packages written in Python for ML are\n",
|
||||
"\n",
|
||||
"* [Scikit-learn](http://scikit-learn.org/stable/), \n",
|
||||
"\n",
|
||||
"* [Tensorflow](https://www.tensorflow.org/),\n",
|
||||
"\n",
|
||||
"* [PyTorch](http://pytorch.org/) and \n",
|
||||
"\n",
|
||||
"* [Keras](https://keras.io/).\n",
|
||||
"\n",
|
||||
"These are all freely available at their respective GitHub sites. They \n",
|
||||
"encompass communities of developers in the thousands or more. And the number\n",
|
||||
"of code developers and contributors keeps increasing."
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -175,18 +175,20 @@ MathJax.Hub.Config({
|
||||
<h2 id="preamble-note-on-writing-reports-using-reference-material-ai-and-other-tools" class="anchor">Preamble: Note on writing reports, using reference material, AI and other tools </h2>
|
||||
|
||||
<p>We want you to answer the three different projects by handing in
|
||||
reports written like a standard scientific/technical report. The links
|
||||
at <a href="https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects" target="_self"><tt>https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects</tt></a>
|
||||
Furthermore, at the same link,
|
||||
you can find examples of previous reports. How to write reports will
|
||||
also be discussed during the various lab sessions. Please do ask us if you are in doubt.
|
||||
reports written like a standard scientific/technical report. The
|
||||
links at
|
||||
<a href="https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects" target="_self"><tt>https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects</tt></a>
|
||||
contain more information. There you can find examples of previous
|
||||
reports, the projects themselves, how we rade reports etc. How to
|
||||
write reports will also be discussed during the various lab
|
||||
sessions. Please do ask us if you are in doubt.
|
||||
</p>
|
||||
|
||||
<p>When using codes and material from other sources, you should refer to
|
||||
these in the bibliography of your report, indicating wherefrom you for
|
||||
example got the code, whether this is from the lecture notes,
|
||||
softwares like Scikit-Learn, TensorFlow, PyTorch or other sources such
|
||||
AI software. These should always be cited correctly. How to cite some
|
||||
softwares like Scikit-Learn, TensorFlow, PyTorch or other sources. These sources
|
||||
should always be cited correctly. How to cite some
|
||||
of the libraries is often indicated from their corresponding GitHub
|
||||
sites or websites, see for example how to cite Scikit-Learn at
|
||||
<a href="https://scikit-learn.org/dev/about.html" target="_self"><tt>https://scikit-learn.org/dev/about.html</tt></a>.
|
||||
|
||||
@@ -154,7 +154,7 @@ MathJax.Hub.Config({
|
||||
<h2 id="regression-analysis-and-resampling-methods" class="anchor">Regression analysis and resampling methods </h2>
|
||||
|
||||
<p>The main aim of this project is to study in more detail various
|
||||
regression methods, including the Ordinary Least Squares (OLS) method.
|
||||
regression methods, including Ordinary Least Squares (OLS) reegression, Ridge regression and LASSO regression.
|
||||
In addition to the scientific part, in this course we want also to
|
||||
give you an experience in writing scientific reports.
|
||||
</p>
|
||||
@@ -170,27 +170,26 @@ $$
|
||||
|
||||
<p>Our first step will be to perform an OLS regression analysis of this
|
||||
function, trying out a polynomial fit with an \( x \) dependence of the
|
||||
form \( [x,x^2,\dots] \). We can use a uniform distribution to set up the
|
||||
form \( [x,x^2,\dots] \). You can use a uniform distribution to set up the
|
||||
arrays of values for \( x \in [-1,1] \), or alternatively use a fixed step size.
|
||||
Thereafter we will repeat much of the
|
||||
same procedure using the Ridge and Lasso regression methods,
|
||||
introducing thus a dependence on the hyperparameter (penalty) \( \lambda \).
|
||||
Thereafter we will repeat many of the same steps when using the Ridge and Lasso regression methods,
|
||||
introducing thereby a dependence on the hyperparameter (penalty) \( \lambda \).
|
||||
</p>
|
||||
|
||||
<p>We will also include bootstrap as a resampling technique in order to
|
||||
study the so-called <b>bias-variance tradeoff</b>. After that we will
|
||||
include the cross-validation technique.
|
||||
include the so-called cross-validation technique.
|
||||
</p>
|
||||
<h3 id="part-a-ordinary-least-square-ols-for-the-runge-function" class="anchor">Part a : Ordinary Least Square (OLS) for the Runge function </h3>
|
||||
|
||||
<p>We will generate our own dataset for a function
|
||||
<p>We will generate our own dataset for abovementioned function
|
||||
\( \mathrm{Runge}(x) \) function with \( x\in [-1,1] \). You should explore also the addition
|
||||
of an added stochastic noise to this function using the normal
|
||||
distribution \( N(0,1) \).
|
||||
</p>
|
||||
|
||||
<p><em>Write your own code</em> (using for example the pseudoinverse function <b>pinv</b> from <b>Numpy</b> ) and perform a standard <b>ordinary least square regression</b>
|
||||
analysis using polynomials in \( x \) up to order \( 15 \). Explore the dependence on the number of data points and the polynomial degree.
|
||||
analysis using polynomials in \( x \) up to order \( 15 \) or higher. Explore the dependence on the number of data points and the polynomial degree.
|
||||
</p>
|
||||
|
||||
<p>Evaluate the mean Squared error (MSE)</p>
|
||||
@@ -214,13 +213,13 @@ $$
|
||||
\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i.
|
||||
$$
|
||||
|
||||
<p>Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree 20).
|
||||
<p>Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree 15).
|
||||
Plot also the parameters \( \theta \) as you increase the order of the polynomial. Comment your results.
|
||||
</p>
|
||||
|
||||
<p>Your code has to include a scaling/centering of the data (for example by
|
||||
subtracting the mean value), and
|
||||
a split of the data in training and test data. For this exercise you can
|
||||
a split of the data in training and test data. For the scaling you can
|
||||
either write your own code or use for example the function for
|
||||
splitting training data provided by the library <b>Scikit-Learn</b> (make
|
||||
sure you have installed it). This function is called
|
||||
@@ -243,11 +242,11 @@ See also the lecture slides from week 35 and week 36.
|
||||
<h3 id="part-b-adding-ridge-regression-for-the-runge-function" class="anchor">Part b: Adding Ridge regression for the Runge function </h3>
|
||||
|
||||
<p>Write your own code for the Ridge method as done in the previous
|
||||
exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the exercise from week 36 is something you can reuse here.
|
||||
exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the results from the exercise set from week 36 is something you can reuse here.
|
||||
</p>
|
||||
|
||||
<p>Perform the same analysis as you did in the previous exercise but now for different values of \( \lambda \). Compare and
|
||||
analyze your results with those obtained in part a) with the ordinary least squares method. Study the
|
||||
analyze your results with those obtained in part a) with the OLS method. Study the
|
||||
dependence on \( \lambda \).
|
||||
</p>
|
||||
<h3 id="part-c-writing-your-own-gradient-descent-code" class="anchor">Part c: Writing your own gradient descent code </h3>
|
||||
@@ -268,15 +267,15 @@ descent approch. Discuss in particular the role of the learning rate.
|
||||
the gradient descent method by including <b>momentum</b>, <b>ADAgrad</b>,
|
||||
<b>RMSprop</b> and <b>ADAM</b> as methods fro iteratively updating your learning
|
||||
rate. Discuss the results and compare the different methods applied to
|
||||
the one-dimensional Runge function.
|
||||
the one-dimensional Runge function. The lecture notes from week 37 contain several examples on how to implement these methods.
|
||||
</p>
|
||||
<h3 id="part-e-writing-our-own-code-for-lasso-regression" class="anchor">Part e: Writing our own code for Lasso regression </h3>
|
||||
|
||||
<p>LASSO regression (see lecture slides from week 36 and week 37)
|
||||
represents our first encounter with a machine learning method which
|
||||
cannot be solved through analytical expressions. Use the gradient
|
||||
cannot be solved through analytical expressions (as in OLS and Ridge regression). Use the gradient
|
||||
descent methods you developed in parts c) and d) to solve the LASSO
|
||||
optimization problem. You can compare your results using
|
||||
optimization problem. You can compare your results with
|
||||
the functionalities of <b>Scikit-Learn</b>.
|
||||
</p>
|
||||
|
||||
@@ -286,14 +285,16 @@ approaches.
|
||||
</p>
|
||||
<h3 id="part-f-stochastic-gradient-descent" class="anchor">Part f: Stochastic gradient descent </h3>
|
||||
|
||||
<p>Our last gradient step is to include stochastic gradient descent using the
|
||||
same methods to update the learning rates as in parts c-e).
|
||||
Compare and discuss your results with and without stochastic gradient and give a critical assessment of the various methods.
|
||||
<p>Our last gradient step is to include stochastic gradient descent using
|
||||
the same methods to update the learning rates as in parts c-e).
|
||||
Compare and discuss your results with and without stochastic gradient
|
||||
and give a critical assessment of the various methods.
|
||||
</p>
|
||||
<h3 id="part-g-bias-variance-trade-off-and-resampling-techniques" class="anchor">Part g: Bias-variance trade-off and resampling techniques </h3>
|
||||
|
||||
<p>Our aim here is to study the bias-variance trade-off by implementing the <b>bootstrap</b> resampling technique.
|
||||
<b>We will only use the simpler ordinary least squares here</b>.
|
||||
<p>Our aim here is to study the bias-variance trade-off by implementing
|
||||
the <b>bootstrap</b> resampling technique. <b>We will only use the simpler
|
||||
ordinary least squares here</b>.
|
||||
</p>
|
||||
|
||||
<p>With a code which does OLS and includes resampling techniques,
|
||||
@@ -303,11 +304,14 @@ intuitions and ideas discussed here also carry over to classification
|
||||
tasks and basically all Machine Learning algorithms.
|
||||
</p>
|
||||
|
||||
<p>Before you perform an analysis of the bias-variance trade-off on your test data, make
|
||||
first a figure similar to Fig. 2.11 of Hastie, Tibshirani, and
|
||||
Friedman. Figure 2.11 of this reference displays only the test and training MSEs. The test MSE can be used to
|
||||
indicate possible regions of low/high bias and variance. You will most likely not get an
|
||||
equally smooth curve!
|
||||
<p>Before you perform an analysis of the bias-variance trade-off on your
|
||||
test data, make first a figure similar to Fig. 2.11 of Hastie,
|
||||
Tibshirani, and Friedman. Figure 2.11 of this reference displays only
|
||||
the test and training MSEs. The test MSE can be used to indicate
|
||||
possible regions of low/high bias and variance. You will most likely
|
||||
not get an equally smooth curve! You may also need to increase the
|
||||
polynomial order and play around with the number of data points as
|
||||
well (see also the exercise set from week 35).
|
||||
</p>
|
||||
|
||||
<p>With this result we move on to the bias-variance trade-off analysis.</p>
|
||||
@@ -317,7 +321,7 @@ dataset \( \mathcal{L} \) consisting of the data
|
||||
\( \mathbf{X}_\mathcal{L}=\{(y_j, \boldsymbol{x}_j), j=0\ldots n-1\} \).
|
||||
</p>
|
||||
|
||||
<p>As in part d), we assume that the true data is generated from a noisy model</p>
|
||||
<p>We assume that the true data is generated from a noisy model</p>
|
||||
|
||||
$$
|
||||
\boldsymbol{y}=f(\boldsymbol{x}) + \boldsymbol{\epsilon}.
|
||||
@@ -329,29 +333,32 @@ deviation \( \sigma^2 \).
|
||||
|
||||
<p>In our derivation of the ordinary least squares method we defined then
|
||||
an approximation to the function \( f \) in terms of the parameters
|
||||
\( \boldsymbol{\beta} \) and the design matrix \( \boldsymbol{X} \) which embody our model,
|
||||
that is \( \boldsymbol{\tilde{y}}=\boldsymbol{X}\boldsymbol{\beta} \).
|
||||
\( \boldsymbol{\theta} \) and the design matrix \( \boldsymbol{X} \) which embody our model,
|
||||
that is \( \boldsymbol{\tilde{y}}=\boldsymbol{X}\boldsymbol{\theta} \).
|
||||
</p>
|
||||
|
||||
<p>The parameters \( \boldsymbol{\beta} \) are in turn found by optimizing the mean
|
||||
<p>The parameters \( \boldsymbol{\theta} \) are in turn found by optimizing the mean
|
||||
squared error via the so-called cost function
|
||||
</p>
|
||||
|
||||
$$
|
||||
C(\boldsymbol{X},\boldsymbol{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right].
|
||||
C(\boldsymbol{X},\boldsymbol{\theta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right].
|
||||
$$
|
||||
|
||||
<p>Here the expected value \( \mathbb{E} \) is the sample value. </p>
|
||||
|
||||
<p>Show that you can rewrite this in terms of a term which contains the variance of the model itself (the so-called variance term), a
|
||||
term which measures the deviation from the true data and the mean value of the model (the bias term) and finally the variance of the noise.
|
||||
That is, show that
|
||||
<p>Show that you can rewrite this in terms of a term which contains the
|
||||
variance of the model itself (the so-called variance term), a term
|
||||
which measures the deviation from the true data and the mean value of
|
||||
the model (the bias term) and finally the variance of the noise.
|
||||
</p>
|
||||
|
||||
<p>That is, show that</p>
|
||||
$$
|
||||
\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\mathrm{Bias}[\tilde{y}]+\mathrm{var}[\tilde{y}]+\sigma^2,
|
||||
$$
|
||||
|
||||
<p>with </p>
|
||||
<p>with (we approximate \( f(\boldsymbol{x})\approx \boldsymbol{y} \)) </p>
|
||||
$$
|
||||
\mathrm{Bias}[\tilde{y}]=\mathbb{E}\left[\left(\boldsymbol{y}-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right]\right)^2\right],
|
||||
$$
|
||||
@@ -361,8 +368,12 @@ $$
|
||||
\mathrm{var}[\tilde{y}]=\mathbb{E}\left[\left(\tilde{\boldsymbol{y}}-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right]\right)^2\right]=\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2.
|
||||
$$
|
||||
|
||||
<p>The answer to this exercise should be included in the theory part of the report. This exercise is also part of the weekly exercises of week 38.
|
||||
Explain what the terms mean and discuss their interpretations.
|
||||
<p><b>Important note</b>: Since the function \( f(x) \) is unknown, in order to be able to evalute the bias, we replace \( f(\boldsymbol{x}) \) in the expression for the bias with \( \boldsymbol{y} \). </p>
|
||||
|
||||
<p>The answer to this exercise should be included in the theory part of
|
||||
the report. This exercise is also part of the weekly exercises of
|
||||
week 38. Explain what the terms mean and discuss their
|
||||
interpretations.
|
||||
</p>
|
||||
|
||||
<p>Perform then a bias-variance analysis of the Runge function by
|
||||
@@ -380,16 +391,18 @@ You can follow the code example in the jupyter-book at <a href="https://compphys
|
||||
resampling technique, the so-called cross-validation method.
|
||||
</p>
|
||||
|
||||
<p>Implement the \( k \)-fold cross-validation algorithm (feel free to use the functionality of <b>Scikit-Learn</b> or write your own code) and evaluate again the MSE function resulting
|
||||
from the test folds.
|
||||
<p>Implement the \( k \)-fold cross-validation algorithm (feel free to use
|
||||
the functionality of <b>Scikit-Learn</b> or write your own code) and
|
||||
evaluate again the MSE function resulting from the test folds.
|
||||
</p>
|
||||
|
||||
<p>Compare the MSE you get from your cross-validation code with the one
|
||||
you got from your <b>bootstrap</b> code. Comment your results. Try \( 5-10 \)
|
||||
folds.
|
||||
you got from your <b>bootstrap</b> code from the previous exercise. Comment and interpret your results.
|
||||
</p>
|
||||
|
||||
<p>In addition to using the ordinary least squares method, you should include both Ridge and Lasso regression in the analysis. </p>
|
||||
<p>In addition to using the ordinary least squares method, you should
|
||||
include both Ridge and Lasso regression in the final analysis.
|
||||
</p>
|
||||
<h2 id="background-literature" class="anchor">Background literature </h2>
|
||||
|
||||
<ol>
|
||||
|
||||
@@ -175,18 +175,20 @@ MathJax.Hub.Config({
|
||||
<h2 id="preamble-note-on-writing-reports-using-reference-material-ai-and-other-tools" class="anchor">Preamble: Note on writing reports, using reference material, AI and other tools </h2>
|
||||
|
||||
<p>We want you to answer the three different projects by handing in
|
||||
reports written like a standard scientific/technical report. The links
|
||||
at <a href="https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects" target="_self"><tt>https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects</tt></a>
|
||||
Furthermore, at the same link,
|
||||
you can find examples of previous reports. How to write reports will
|
||||
also be discussed during the various lab sessions. Please do ask us if you are in doubt.
|
||||
reports written like a standard scientific/technical report. The
|
||||
links at
|
||||
<a href="https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects" target="_self"><tt>https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects</tt></a>
|
||||
contain more information. There you can find examples of previous
|
||||
reports, the projects themselves, how we rade reports etc. How to
|
||||
write reports will also be discussed during the various lab
|
||||
sessions. Please do ask us if you are in doubt.
|
||||
</p>
|
||||
|
||||
<p>When using codes and material from other sources, you should refer to
|
||||
these in the bibliography of your report, indicating wherefrom you for
|
||||
example got the code, whether this is from the lecture notes,
|
||||
softwares like Scikit-Learn, TensorFlow, PyTorch or other sources such
|
||||
AI software. These should always be cited correctly. How to cite some
|
||||
softwares like Scikit-Learn, TensorFlow, PyTorch or other sources. These sources
|
||||
should always be cited correctly. How to cite some
|
||||
of the libraries is often indicated from their corresponding GitHub
|
||||
sites or websites, see for example how to cite Scikit-Learn at
|
||||
<a href="https://scikit-learn.org/dev/about.html" target="_self"><tt>https://scikit-learn.org/dev/about.html</tt></a>.
|
||||
|
||||
@@ -210,18 +210,20 @@ MathJax.Hub.Config({
|
||||
<h2 id="preamble-note-on-writing-reports-using-reference-material-ai-and-other-tools">Preamble: Note on writing reports, using reference material, AI and other tools </h2>
|
||||
|
||||
<p>We want you to answer the three different projects by handing in
|
||||
reports written like a standard scientific/technical report. The links
|
||||
at <a href="https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects" target="_blank"><tt>https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects</tt></a>
|
||||
Furthermore, at the same link,
|
||||
you can find examples of previous reports. How to write reports will
|
||||
also be discussed during the various lab sessions. Please do ask us if you are in doubt.
|
||||
reports written like a standard scientific/technical report. The
|
||||
links at
|
||||
<a href="https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects" target="_blank"><tt>https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects</tt></a>
|
||||
contain more information. There you can find examples of previous
|
||||
reports, the projects themselves, how we rade reports etc. How to
|
||||
write reports will also be discussed during the various lab
|
||||
sessions. Please do ask us if you are in doubt.
|
||||
</p>
|
||||
|
||||
<p>When using codes and material from other sources, you should refer to
|
||||
these in the bibliography of your report, indicating wherefrom you for
|
||||
example got the code, whether this is from the lecture notes,
|
||||
softwares like Scikit-Learn, TensorFlow, PyTorch or other sources such
|
||||
AI software. These should always be cited correctly. How to cite some
|
||||
softwares like Scikit-Learn, TensorFlow, PyTorch or other sources. These sources
|
||||
should always be cited correctly. How to cite some
|
||||
of the libraries is often indicated from their corresponding GitHub
|
||||
sites or websites, see for example how to cite Scikit-Learn at
|
||||
<a href="https://scikit-learn.org/dev/about.html" target="_blank"><tt>https://scikit-learn.org/dev/about.html</tt></a>.
|
||||
@@ -253,7 +255,7 @@ other data sets, make sure these are sets used for regression problems
|
||||
<h2 id="regression-analysis-and-resampling-methods">Regression analysis and resampling methods </h2>
|
||||
|
||||
<p>The main aim of this project is to study in more detail various
|
||||
regression methods, including the Ordinary Least Squares (OLS) method.
|
||||
regression methods, including Ordinary Least Squares (OLS) reegression, Ridge regression and LASSO regression.
|
||||
In addition to the scientific part, in this course we want also to
|
||||
give you an experience in writing scientific reports.
|
||||
</p>
|
||||
@@ -269,27 +271,26 @@ $$
|
||||
|
||||
<p>Our first step will be to perform an OLS regression analysis of this
|
||||
function, trying out a polynomial fit with an \( x \) dependence of the
|
||||
form \( [x,x^2,\dots] \). We can use a uniform distribution to set up the
|
||||
form \( [x,x^2,\dots] \). You can use a uniform distribution to set up the
|
||||
arrays of values for \( x \in [-1,1] \), or alternatively use a fixed step size.
|
||||
Thereafter we will repeat much of the
|
||||
same procedure using the Ridge and Lasso regression methods,
|
||||
introducing thus a dependence on the hyperparameter (penalty) \( \lambda \).
|
||||
Thereafter we will repeat many of the same steps when using the Ridge and Lasso regression methods,
|
||||
introducing thereby a dependence on the hyperparameter (penalty) \( \lambda \).
|
||||
</p>
|
||||
|
||||
<p>We will also include bootstrap as a resampling technique in order to
|
||||
study the so-called <b>bias-variance tradeoff</b>. After that we will
|
||||
include the cross-validation technique.
|
||||
include the so-called cross-validation technique.
|
||||
</p>
|
||||
<h3 id="part-a-ordinary-least-square-ols-for-the-runge-function">Part a : Ordinary Least Square (OLS) for the Runge function </h3>
|
||||
|
||||
<p>We will generate our own dataset for a function
|
||||
<p>We will generate our own dataset for abovementioned function
|
||||
\( \mathrm{Runge}(x) \) function with \( x\in [-1,1] \). You should explore also the addition
|
||||
of an added stochastic noise to this function using the normal
|
||||
distribution \( N(0,1) \).
|
||||
</p>
|
||||
|
||||
<p><em>Write your own code</em> (using for example the pseudoinverse function <b>pinv</b> from <b>Numpy</b> ) and perform a standard <b>ordinary least square regression</b>
|
||||
analysis using polynomials in \( x \) up to order \( 15 \). Explore the dependence on the number of data points and the polynomial degree.
|
||||
analysis using polynomials in \( x \) up to order \( 15 \) or higher. Explore the dependence on the number of data points and the polynomial degree.
|
||||
</p>
|
||||
|
||||
<p>Evaluate the mean Squared error (MSE)</p>
|
||||
@@ -313,13 +314,13 @@ $$
|
||||
\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i.
|
||||
$$
|
||||
|
||||
<p>Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree 20).
|
||||
<p>Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree 15).
|
||||
Plot also the parameters \( \theta \) as you increase the order of the polynomial. Comment your results.
|
||||
</p>
|
||||
|
||||
<p>Your code has to include a scaling/centering of the data (for example by
|
||||
subtracting the mean value), and
|
||||
a split of the data in training and test data. For this exercise you can
|
||||
a split of the data in training and test data. For the scaling you can
|
||||
either write your own code or use for example the function for
|
||||
splitting training data provided by the library <b>Scikit-Learn</b> (make
|
||||
sure you have installed it). This function is called
|
||||
@@ -342,11 +343,11 @@ See also the lecture slides from week 35 and week 36.
|
||||
<h3 id="part-b-adding-ridge-regression-for-the-runge-function">Part b: Adding Ridge regression for the Runge function </h3>
|
||||
|
||||
<p>Write your own code for the Ridge method as done in the previous
|
||||
exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the exercise from week 36 is something you can reuse here.
|
||||
exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the results from the exercise set from week 36 is something you can reuse here.
|
||||
</p>
|
||||
|
||||
<p>Perform the same analysis as you did in the previous exercise but now for different values of \( \lambda \). Compare and
|
||||
analyze your results with those obtained in part a) with the ordinary least squares method. Study the
|
||||
analyze your results with those obtained in part a) with the OLS method. Study the
|
||||
dependence on \( \lambda \).
|
||||
</p>
|
||||
<h3 id="part-c-writing-your-own-gradient-descent-code">Part c: Writing your own gradient descent code </h3>
|
||||
@@ -367,15 +368,15 @@ descent approch. Discuss in particular the role of the learning rate.
|
||||
the gradient descent method by including <b>momentum</b>, <b>ADAgrad</b>,
|
||||
<b>RMSprop</b> and <b>ADAM</b> as methods fro iteratively updating your learning
|
||||
rate. Discuss the results and compare the different methods applied to
|
||||
the one-dimensional Runge function.
|
||||
the one-dimensional Runge function. The lecture notes from week 37 contain several examples on how to implement these methods.
|
||||
</p>
|
||||
<h3 id="part-e-writing-our-own-code-for-lasso-regression">Part e: Writing our own code for Lasso regression </h3>
|
||||
|
||||
<p>LASSO regression (see lecture slides from week 36 and week 37)
|
||||
represents our first encounter with a machine learning method which
|
||||
cannot be solved through analytical expressions. Use the gradient
|
||||
cannot be solved through analytical expressions (as in OLS and Ridge regression). Use the gradient
|
||||
descent methods you developed in parts c) and d) to solve the LASSO
|
||||
optimization problem. You can compare your results using
|
||||
optimization problem. You can compare your results with
|
||||
the functionalities of <b>Scikit-Learn</b>.
|
||||
</p>
|
||||
|
||||
@@ -385,14 +386,16 @@ approaches.
|
||||
</p>
|
||||
<h3 id="part-f-stochastic-gradient-descent">Part f: Stochastic gradient descent </h3>
|
||||
|
||||
<p>Our last gradient step is to include stochastic gradient descent using the
|
||||
same methods to update the learning rates as in parts c-e).
|
||||
Compare and discuss your results with and without stochastic gradient and give a critical assessment of the various methods.
|
||||
<p>Our last gradient step is to include stochastic gradient descent using
|
||||
the same methods to update the learning rates as in parts c-e).
|
||||
Compare and discuss your results with and without stochastic gradient
|
||||
and give a critical assessment of the various methods.
|
||||
</p>
|
||||
<h3 id="part-g-bias-variance-trade-off-and-resampling-techniques">Part g: Bias-variance trade-off and resampling techniques </h3>
|
||||
|
||||
<p>Our aim here is to study the bias-variance trade-off by implementing the <b>bootstrap</b> resampling technique.
|
||||
<b>We will only use the simpler ordinary least squares here</b>.
|
||||
<p>Our aim here is to study the bias-variance trade-off by implementing
|
||||
the <b>bootstrap</b> resampling technique. <b>We will only use the simpler
|
||||
ordinary least squares here</b>.
|
||||
</p>
|
||||
|
||||
<p>With a code which does OLS and includes resampling techniques,
|
||||
@@ -402,11 +405,14 @@ intuitions and ideas discussed here also carry over to classification
|
||||
tasks and basically all Machine Learning algorithms.
|
||||
</p>
|
||||
|
||||
<p>Before you perform an analysis of the bias-variance trade-off on your test data, make
|
||||
first a figure similar to Fig. 2.11 of Hastie, Tibshirani, and
|
||||
Friedman. Figure 2.11 of this reference displays only the test and training MSEs. The test MSE can be used to
|
||||
indicate possible regions of low/high bias and variance. You will most likely not get an
|
||||
equally smooth curve!
|
||||
<p>Before you perform an analysis of the bias-variance trade-off on your
|
||||
test data, make first a figure similar to Fig. 2.11 of Hastie,
|
||||
Tibshirani, and Friedman. Figure 2.11 of this reference displays only
|
||||
the test and training MSEs. The test MSE can be used to indicate
|
||||
possible regions of low/high bias and variance. You will most likely
|
||||
not get an equally smooth curve! You may also need to increase the
|
||||
polynomial order and play around with the number of data points as
|
||||
well (see also the exercise set from week 35).
|
||||
</p>
|
||||
|
||||
<p>With this result we move on to the bias-variance trade-off analysis.</p>
|
||||
@@ -416,7 +422,7 @@ dataset \( \mathcal{L} \) consisting of the data
|
||||
\( \mathbf{X}_\mathcal{L}=\{(y_j, \boldsymbol{x}_j), j=0\ldots n-1\} \).
|
||||
</p>
|
||||
|
||||
<p>As in part d), we assume that the true data is generated from a noisy model</p>
|
||||
<p>We assume that the true data is generated from a noisy model</p>
|
||||
|
||||
$$
|
||||
\boldsymbol{y}=f(\boldsymbol{x}) + \boldsymbol{\epsilon}.
|
||||
@@ -428,29 +434,32 @@ deviation \( \sigma^2 \).
|
||||
|
||||
<p>In our derivation of the ordinary least squares method we defined then
|
||||
an approximation to the function \( f \) in terms of the parameters
|
||||
\( \boldsymbol{\beta} \) and the design matrix \( \boldsymbol{X} \) which embody our model,
|
||||
that is \( \boldsymbol{\tilde{y}}=\boldsymbol{X}\boldsymbol{\beta} \).
|
||||
\( \boldsymbol{\theta} \) and the design matrix \( \boldsymbol{X} \) which embody our model,
|
||||
that is \( \boldsymbol{\tilde{y}}=\boldsymbol{X}\boldsymbol{\theta} \).
|
||||
</p>
|
||||
|
||||
<p>The parameters \( \boldsymbol{\beta} \) are in turn found by optimizing the mean
|
||||
<p>The parameters \( \boldsymbol{\theta} \) are in turn found by optimizing the mean
|
||||
squared error via the so-called cost function
|
||||
</p>
|
||||
|
||||
$$
|
||||
C(\boldsymbol{X},\boldsymbol{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right].
|
||||
C(\boldsymbol{X},\boldsymbol{\theta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right].
|
||||
$$
|
||||
|
||||
<p>Here the expected value \( \mathbb{E} \) is the sample value. </p>
|
||||
|
||||
<p>Show that you can rewrite this in terms of a term which contains the variance of the model itself (the so-called variance term), a
|
||||
term which measures the deviation from the true data and the mean value of the model (the bias term) and finally the variance of the noise.
|
||||
That is, show that
|
||||
<p>Show that you can rewrite this in terms of a term which contains the
|
||||
variance of the model itself (the so-called variance term), a term
|
||||
which measures the deviation from the true data and the mean value of
|
||||
the model (the bias term) and finally the variance of the noise.
|
||||
</p>
|
||||
|
||||
<p>That is, show that</p>
|
||||
$$
|
||||
\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\mathrm{Bias}[\tilde{y}]+\mathrm{var}[\tilde{y}]+\sigma^2,
|
||||
$$
|
||||
|
||||
<p>with </p>
|
||||
<p>with (we approximate \( f(\boldsymbol{x})\approx \boldsymbol{y} \)) </p>
|
||||
$$
|
||||
\mathrm{Bias}[\tilde{y}]=\mathbb{E}\left[\left(\boldsymbol{y}-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right]\right)^2\right],
|
||||
$$
|
||||
@@ -460,8 +469,12 @@ $$
|
||||
\mathrm{var}[\tilde{y}]=\mathbb{E}\left[\left(\tilde{\boldsymbol{y}}-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right]\right)^2\right]=\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2.
|
||||
$$
|
||||
|
||||
<p>The answer to this exercise should be included in the theory part of the report. This exercise is also part of the weekly exercises of week 38.
|
||||
Explain what the terms mean and discuss their interpretations.
|
||||
<p><b>Important note</b>: Since the function \( f(x) \) is unknown, in order to be able to evalute the bias, we replace \( f(\boldsymbol{x}) \) in the expression for the bias with \( \boldsymbol{y} \). </p>
|
||||
|
||||
<p>The answer to this exercise should be included in the theory part of
|
||||
the report. This exercise is also part of the weekly exercises of
|
||||
week 38. Explain what the terms mean and discuss their
|
||||
interpretations.
|
||||
</p>
|
||||
|
||||
<p>Perform then a bias-variance analysis of the Runge function by
|
||||
@@ -479,16 +492,18 @@ You can follow the code example in the jupyter-book at <a href="https://compphys
|
||||
resampling technique, the so-called cross-validation method.
|
||||
</p>
|
||||
|
||||
<p>Implement the \( k \)-fold cross-validation algorithm (feel free to use the functionality of <b>Scikit-Learn</b> or write your own code) and evaluate again the MSE function resulting
|
||||
from the test folds.
|
||||
<p>Implement the \( k \)-fold cross-validation algorithm (feel free to use
|
||||
the functionality of <b>Scikit-Learn</b> or write your own code) and
|
||||
evaluate again the MSE function resulting from the test folds.
|
||||
</p>
|
||||
|
||||
<p>Compare the MSE you get from your cross-validation code with the one
|
||||
you got from your <b>bootstrap</b> code. Comment your results. Try \( 5-10 \)
|
||||
folds.
|
||||
you got from your <b>bootstrap</b> code from the previous exercise. Comment and interpret your results.
|
||||
</p>
|
||||
|
||||
<p>In addition to using the ordinary least squares method, you should include both Ridge and Lasso regression in the analysis. </p>
|
||||
<p>In addition to using the ordinary least squares method, you should
|
||||
include both Ridge and Lasso regression in the final analysis.
|
||||
</p>
|
||||
<h2 id="background-literature">Background literature </h2>
|
||||
|
||||
<ol>
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f57c8fa4",
|
||||
"id": "b209e219",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -14,20 +14,20 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "26a16b0d",
|
||||
"id": "6fa4c4bc",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"# Project 1 on Machine Learning, deadline October 6 (midnight), 2025\n",
|
||||
"**[Data Analysis and Machine Learning FYS-STK3155/FYS4155](http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html)**, University of Oslo, Norway\n",
|
||||
"**Data Analysis and Machine Learning FYS-STK3155/FYS4155**, University of Oslo, Norway\n",
|
||||
"\n",
|
||||
"Date: **September 2**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f77c6ce6",
|
||||
"id": "735b16c4",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -35,30 +35,30 @@
|
||||
"## Preamble: Note on writing reports, using reference material, AI and other tools\n",
|
||||
"\n",
|
||||
"We want you to answer the three different projects by handing in\n",
|
||||
"reports written like a standard scientific/technical report. The link\n",
|
||||
"at\n",
|
||||
"<https://github.com/CompPhysics/MachineLearning/blob/master/doc/Projects/ProjectWriting/projectwriting.ipynb>\n",
|
||||
"gives some guidance. See also the grading suggestion at\n",
|
||||
"<https://github.com/CompPhysics/MachineLearning/blob/master/doc/Projects/EvaluationGrading/EvaluationForm.md>.\n",
|
||||
"reports written like a standard scientific/technical report. The\n",
|
||||
"links at\n",
|
||||
"<https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects>\n",
|
||||
"contain more information. There you can find examples of previous\n",
|
||||
"reports, the projects themselves, how we rade reports etc. How to\n",
|
||||
"write reports will also be discussed during the various lab\n",
|
||||
"sessions. Please do ask us if you are in doubt.\n",
|
||||
"\n",
|
||||
"Furthermore, at\n",
|
||||
"<https://github.com/CompPhysics/MachineLearning/blob/master/doc/Projects/ReportExample/>\n",
|
||||
"you can find examples of previous reports. How to write reports will\n",
|
||||
"also be discussed during the various lab sessions. Please do ask us if you are in doubt.\n",
|
||||
"\n",
|
||||
"When using codes and material from other sources, you should refer to these in the bibliography of your report, indicating wherefrom you for example\n",
|
||||
"got the code, whether this is from the lecture notes, softwares like\n",
|
||||
"Scikit-Learn, TensorFlow, PyTorch or other sources such AI software. These should\n",
|
||||
"always be cited correctly. How to cite some of the libraries is often\n",
|
||||
"indicated from their corresponding GitHub sites or websites, see for example how to cite Scikit-Learn at <https://scikit-learn.org/dev/about.html>. \n",
|
||||
"When using codes and material from other sources, you should refer to\n",
|
||||
"these in the bibliography of your report, indicating wherefrom you for\n",
|
||||
"example got the code, whether this is from the lecture notes,\n",
|
||||
"softwares like Scikit-Learn, TensorFlow, PyTorch or other sources. These sources\n",
|
||||
"should always be cited correctly. How to cite some\n",
|
||||
"of the libraries is often indicated from their corresponding GitHub\n",
|
||||
"sites or websites, see for example how to cite Scikit-Learn at\n",
|
||||
"<https://scikit-learn.org/dev/about.html>.\n",
|
||||
"\n",
|
||||
"We enocurage you to use tools like\n",
|
||||
"[ChatGPT](https://openai.com/chatgpt/) or similar in writing the report. If you use for example ChatGPT,\n",
|
||||
"please do cite it properly and include (if possible) your questions and answers as an addition to the report. This can\n",
|
||||
"be uploaded to for example your website, GitHub/GitLab or similar as supplemental material.\n",
|
||||
"\n",
|
||||
"If you would like to study other data sets, feel free to\n",
|
||||
"propose other sets. What we have proposed here are mere suggestions from our\n",
|
||||
"If you would like to study other data sets, feel free to propose other\n",
|
||||
"sets. What we have proposed here are mere suggestions from our\n",
|
||||
"side. If you opt for another data set, consider using a set which has\n",
|
||||
"been studied in the scientific literature. This makes it easier for\n",
|
||||
"you to compare and analyze your results. Comparing with existing\n",
|
||||
@@ -68,12 +68,14 @@
|
||||
"<https://archive.ics.uci.edu/ml/index.php> is an excellent site to\n",
|
||||
"look up for examples and\n",
|
||||
"inspiration. [Kaggle.com](https://www.kaggle.com/) is an equally\n",
|
||||
"interesting site. Feel free to explore these sites. When selecting other data sets, make sure these are sets used for regression problems (not classification)."
|
||||
"interesting site. Feel free to explore these sites. When selecting\n",
|
||||
"other data sets, make sure these are sets used for regression problems\n",
|
||||
"(not classification)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "fc679cbc",
|
||||
"id": "0b7956ca",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -81,17 +83,19 @@
|
||||
"## Regression analysis and resampling methods\n",
|
||||
"\n",
|
||||
"The main aim of this project is to study in more detail various\n",
|
||||
"regression methods, including the Ordinary Least Squares (OLS) method.\n",
|
||||
"regression methods, including Ordinary Least Squares (OLS) reegression, Ridge regression and LASSO regression.\n",
|
||||
"In addition to the scientific part, in this course we want also to\n",
|
||||
"give you an experience in writing scientific reports.\n",
|
||||
"\n",
|
||||
"We will first study how to fit polynomials to specific\n",
|
||||
"one-dimensional functions. We will start with a function given by Runge's function (see <https://en.wikipedia.org/wiki/Runge%27s_phenomenon> for a discussion). The one-dimensional function we will study first is"
|
||||
"We will study how to fit polynomials to specific\n",
|
||||
"one-dimensional functions (feel free to replace the suggested function with more complicated ones).\n",
|
||||
"\n",
|
||||
"We will use Runge's function (see <https://en.wikipedia.org/wiki/Runge%27s_phenomenon> for a discussion). The one-dimensional function we will study is"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "7a2784c7",
|
||||
"id": "28ba3d22",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -103,48 +107,46 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "16851f60",
|
||||
"id": "9a3e10ba",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"Our first step will be to perform an OLS regression analysis of this\n",
|
||||
"function, trying out a polynomial fit with an $x$ dependence of the\n",
|
||||
"form $[x,x^2,\\dots]$. We can use a uniform distribution to set up the\n",
|
||||
"form $[x,x^2,\\dots]$. You can use a uniform distribution to set up the\n",
|
||||
"arrays of values for $x \\in [-1,1]$, or alternatively use a fixed step size.\n",
|
||||
"Thereafter we will repeat much of the\n",
|
||||
"same procedure using the Ridge and Lasso regression methods,\n",
|
||||
"introducing thus a dependence on the hyperparameter (penalty) $\\lambda$.\n",
|
||||
"Thereafter we will repeat many of the same steps when using the Ridge and Lasso regression methods,\n",
|
||||
"introducing thereby a dependence on the hyperparameter (penalty) $\\lambda$.\n",
|
||||
"\n",
|
||||
"We will also include bootstrap as a resampling technique in order to\n",
|
||||
"study the so-called **bias-variance tradeoff**. After that we will\n",
|
||||
"include the cross-validation technique."
|
||||
"include the so-called cross-validation technique."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "0ba8f2df",
|
||||
"id": "8aa547a5",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"### Part a : Ordinary Least Square (OLS) on the Franke function\n",
|
||||
"### Part a : Ordinary Least Square (OLS) for the Runge function\n",
|
||||
"\n",
|
||||
"We will generate our own dataset for a function\n",
|
||||
"$\\mathrm{FrankeFunction}(x,y)$ with $x,y \\in [0,1]$. The function\n",
|
||||
"$f(x,y)$ is the Franke function. You should explore also the addition\n",
|
||||
"We will generate our own dataset for abovementioned function\n",
|
||||
"$\\mathrm{Runge}(x)$ function with $x\\in [-1,1]$. You should explore also the addition\n",
|
||||
"of an added stochastic noise to this function using the normal\n",
|
||||
"distribution $N(0,1)$.\n",
|
||||
"\n",
|
||||
"*Write your own code* (using for example the pseudoinverse function **pinv** from **Numpy** ) and perform a standard **ordinary least square regression**\n",
|
||||
"analysis using polynomials in $x$ up to order $15$. Explore the dependence on the number of data points and the polynomial degree.\n",
|
||||
"analysis using polynomials in $x$ up to order $15$ or higher. Explore the dependence on the number of data points and the polynomial degree.\n",
|
||||
"\n",
|
||||
"Evaluate the mean Squared error (MSE)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "9fa926bd",
|
||||
"id": "68fbf03d",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -157,7 +159,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "29a3ee67",
|
||||
"id": "b49509bc",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -169,7 +171,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "95634981",
|
||||
"id": "0fa4ffc6",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -181,7 +183,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "8a1dafa5",
|
||||
"id": "ce462b32",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -191,7 +193,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "23e66218",
|
||||
"id": "a5fbef36",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -203,17 +205,17 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b353e056",
|
||||
"id": "a6afe9cb",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree 20).\n",
|
||||
"Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree 15).\n",
|
||||
"Plot also the parameters $\\theta$ as you increase the order of the polynomial. Comment your results.\n",
|
||||
"\n",
|
||||
"Your code has to include a scaling/centering of the data (for example by\n",
|
||||
"subtracting the mean value), and\n",
|
||||
"a split of the data in training and test data. For this exercise you can\n",
|
||||
"a split of the data in training and test data. For the scaling you can\n",
|
||||
"either write your own code or use for example the function for\n",
|
||||
"splitting training data provided by the library **Scikit-Learn** (make\n",
|
||||
"sure you have installed it). This function is called\n",
|
||||
@@ -234,7 +236,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "9f3a2963",
|
||||
"id": "3be10f68",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -242,16 +244,16 @@
|
||||
"### Part b: Adding Ridge regression for the Runge function\n",
|
||||
"\n",
|
||||
"Write your own code for the Ridge method as done in the previous\n",
|
||||
"exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the exercise from week 36 is something you can reuse here.\n",
|
||||
"exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the results from the exercise set from week 36 is something you can reuse here.\n",
|
||||
"\n",
|
||||
"Perform the same analysis as you did in the previous exercise but now for different values of $\\lambda$. Compare and\n",
|
||||
"analyze your results with those obtained in part a) with the ordinary least squares method. Study the\n",
|
||||
"analyze your results with those obtained in part a) with the OLS method. Study the\n",
|
||||
"dependence on $\\lambda$."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "2d8fb899",
|
||||
"id": "caa7909c",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -270,7 +272,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "6ad3c9ce",
|
||||
"id": "3aac4df1",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -281,12 +283,12 @@
|
||||
"the gradient descent method by including **momentum**, **ADAgrad**,\n",
|
||||
"**RMSprop** and **ADAM** as methods fro iteratively updating your learning\n",
|
||||
"rate. Discuss the results and compare the different methods applied to\n",
|
||||
"the one-dimensional Runge function."
|
||||
"the one-dimensional Runge function. The lecture notes from week 37 contain several examples on how to implement these methods."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "234254d4",
|
||||
"id": "d0862a53",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -295,9 +297,9 @@
|
||||
"\n",
|
||||
"LASSO regression (see lecture slides from week 36 and week 37)\n",
|
||||
"represents our first encounter with a machine learning method which\n",
|
||||
"cannot be solved through analytical expressions. Use the gradient\n",
|
||||
"cannot be solved through analytical expressions (as in OLS and Ridge regression). Use the gradient\n",
|
||||
"descent methods you developed in parts c) and d) to solve the LASSO\n",
|
||||
"optimization problem. You can compare your results using\n",
|
||||
"optimization problem. You can compare your results with \n",
|
||||
"the functionalities of **Scikit-Learn**.\n",
|
||||
"\n",
|
||||
"Discuss (critically) your results for the Runge function from OLS,\n",
|
||||
@@ -307,29 +309,31 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "31a94810",
|
||||
"id": "9170032e",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"### Part f: Stochastic gradient descent\n",
|
||||
"\n",
|
||||
"Our last gradient step is to include stochastic gradient descent using the\n",
|
||||
"same methods to update the learning rates as in parts c-e).\n",
|
||||
"Compare and discuss your results with and without stochastic gradient and give a critical assessment of the various methods."
|
||||
"Our last gradient step is to include stochastic gradient descent using\n",
|
||||
"the same methods to update the learning rates as in parts c-e).\n",
|
||||
"Compare and discuss your results with and without stochastic gradient\n",
|
||||
"and give a critical assessment of the various methods."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "e657f5a5",
|
||||
"id": "bacd1035",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"### Part g: Bias-variance trade-off and resampling techniques\n",
|
||||
"\n",
|
||||
"Our aim here is to study the bias-variance trade-off by implementing the **bootstrap** resampling technique.\n",
|
||||
"**We will only use the simpler ordinary least squares here**.\n",
|
||||
"Our aim here is to study the bias-variance trade-off by implementing\n",
|
||||
"the **bootstrap** resampling technique. **We will only use the simpler\n",
|
||||
"ordinary least squares here**.\n",
|
||||
"\n",
|
||||
"With a code which does OLS and includes resampling techniques, \n",
|
||||
"we will now discuss the bias-variance trade-off in the context of\n",
|
||||
@@ -337,11 +341,14 @@
|
||||
"intuitions and ideas discussed here also carry over to classification\n",
|
||||
"tasks and basically all Machine Learning algorithms. \n",
|
||||
"\n",
|
||||
"Before you perform an analysis of the bias-variance trade-off on your test data, make\n",
|
||||
"first a figure similar to Fig. 2.11 of Hastie, Tibshirani, and\n",
|
||||
"Friedman. Figure 2.11 of this reference displays only the test and training MSEs. The test MSE can be used to \n",
|
||||
"indicate possible regions of low/high bias and variance. You will most likely not get an\n",
|
||||
"equally smooth curve!\n",
|
||||
"Before you perform an analysis of the bias-variance trade-off on your\n",
|
||||
"test data, make first a figure similar to Fig. 2.11 of Hastie,\n",
|
||||
"Tibshirani, and Friedman. Figure 2.11 of this reference displays only\n",
|
||||
"the test and training MSEs. The test MSE can be used to indicate\n",
|
||||
"possible regions of low/high bias and variance. You will most likely\n",
|
||||
"not get an equally smooth curve! You may also need to increase the\n",
|
||||
"polynomial order and play around with the number of data points as\n",
|
||||
"well (see also the exercise set from week 35).\n",
|
||||
"\n",
|
||||
"With this result we move on to the bias-variance trade-off analysis.\n",
|
||||
"\n",
|
||||
@@ -349,12 +356,12 @@
|
||||
"dataset $\\mathcal{L}$ consisting of the data\n",
|
||||
"$\\mathbf{X}_\\mathcal{L}=\\{(y_j, \\boldsymbol{x}_j), j=0\\ldots n-1\\}$.\n",
|
||||
"\n",
|
||||
"As in part d), we assume that the true data is generated from a noisy model"
|
||||
"We assume that the true data is generated from a noisy model"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "32261f48",
|
||||
"id": "b871ec69",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -366,7 +373,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b9f27316",
|
||||
"id": "b47c19bc",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -376,42 +383,45 @@
|
||||
"\n",
|
||||
"In our derivation of the ordinary least squares method we defined then\n",
|
||||
"an approximation to the function $f$ in terms of the parameters\n",
|
||||
"$\\boldsymbol{\\beta}$ and the design matrix $\\boldsymbol{X}$ which embody our model,\n",
|
||||
"that is $\\boldsymbol{\\tilde{y}}=\\boldsymbol{X}\\boldsymbol{\\beta}$.\n",
|
||||
"$\\boldsymbol{\\theta}$ and the design matrix $\\boldsymbol{X}$ which embody our model,\n",
|
||||
"that is $\\boldsymbol{\\tilde{y}}=\\boldsymbol{X}\\boldsymbol{\\theta}$.\n",
|
||||
"\n",
|
||||
"The parameters $\\boldsymbol{\\beta}$ are in turn found by optimizing the mean\n",
|
||||
"The parameters $\\boldsymbol{\\theta}$ are in turn found by optimizing the mean\n",
|
||||
"squared error via the so-called cost function"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "cf5f4bba",
|
||||
"id": "6db622c2",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"C(\\boldsymbol{X},\\boldsymbol{\\beta}) =\\frac{1}{n}\\sum_{i=0}^{n-1}(y_i-\\tilde{y}_i)^2=\\mathbb{E}\\left[(\\boldsymbol{y}-\\boldsymbol{\\tilde{y}})^2\\right].\n",
|
||||
"C(\\boldsymbol{X},\\boldsymbol{\\theta}) =\\frac{1}{n}\\sum_{i=0}^{n-1}(y_i-\\tilde{y}_i)^2=\\mathbb{E}\\left[(\\boldsymbol{y}-\\boldsymbol{\\tilde{y}})^2\\right].\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b8c5d66e",
|
||||
"id": "5a7eb70d",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"Here the expected value $\\mathbb{E}$ is the sample value. \n",
|
||||
"\n",
|
||||
"Show that you can rewrite this in terms of a term which contains the variance of the model itself (the so-called variance term), a\n",
|
||||
"term which measures the deviation from the true data and the mean value of the model (the bias term) and finally the variance of the noise.\n",
|
||||
"Show that you can rewrite this in terms of a term which contains the\n",
|
||||
"variance of the model itself (the so-called variance term), a term\n",
|
||||
"which measures the deviation from the true data and the mean value of\n",
|
||||
"the model (the bias term) and finally the variance of the noise.\n",
|
||||
"\n",
|
||||
"That is, show that"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "c2f3364d",
|
||||
"id": "d50292fe",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -423,17 +433,17 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "96540fa5",
|
||||
"id": "50fa641f",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"with"
|
||||
"with (we approximate $f(\\boldsymbol{x})\\approx \\boldsymbol{y}$)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "0d8d58c5",
|
||||
"id": "2bd429c9",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -445,7 +455,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "6145f2ca",
|
||||
"id": "737c2819",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -455,7 +465,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "39915c14",
|
||||
"id": "41ef92ef",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -467,13 +477,17 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f738ba25",
|
||||
"id": "b948dab0",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"The answer to this exercise should be included in the theory part of the report. This exercise is also part of the weekly exercises of week 38.\n",
|
||||
"Explain what the terms mean and discuss their interpretations.\n",
|
||||
"**Important note**: Since the function $f(x)$ is unknown, in order to be able to evalute the bias, we replace $f(\\boldsymbol{x})$ in the expression for the bias with $\\boldsymbol{y}$. \n",
|
||||
"\n",
|
||||
"The answer to this exercise should be included in the theory part of\n",
|
||||
"the report. This exercise is also part of the weekly exercises of\n",
|
||||
"week 38. Explain what the terms mean and discuss their\n",
|
||||
"interpretations.\n",
|
||||
"\n",
|
||||
"Perform then a bias-variance analysis of the Runge function by\n",
|
||||
"studying the MSE value as function of the complexity of your model.\n",
|
||||
@@ -486,7 +500,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "ad9cec79",
|
||||
"id": "6a0548bf",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -496,19 +510,20 @@
|
||||
"The aim here is to implement another widely popular\n",
|
||||
"resampling technique, the so-called cross-validation method. \n",
|
||||
"\n",
|
||||
"Implement the $k$-fold cross-validation algorithm (feel free to use the functionality of **Scikit-Learn** or write your own code) and evaluate again the MSE function resulting\n",
|
||||
"from the test folds. \n",
|
||||
"Implement the $k$-fold cross-validation algorithm (feel free to use\n",
|
||||
"the functionality of **Scikit-Learn** or write your own code) and\n",
|
||||
"evaluate again the MSE function resulting from the test folds.\n",
|
||||
"\n",
|
||||
"Compare the MSE you get from your cross-validation code with the one\n",
|
||||
"you got from your **bootstrap** code. Comment your results. Try $5-10$\n",
|
||||
"folds. \n",
|
||||
"you got from your **bootstrap** code from the previous exercise. Comment and interpret your results. \n",
|
||||
"\n",
|
||||
"In addition to using the ordinary least squares method, you should include both Ridge and Lasso regression in the analysis."
|
||||
"In addition to using the ordinary least squares method, you should\n",
|
||||
"include both Ridge and Lasso regression in the final analysis."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "264c7e54",
|
||||
"id": "df9845cb",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -517,12 +532,12 @@
|
||||
"\n",
|
||||
"1. For a discussion and derivation of the variances and mean squared errors using linear regression, see the [Lecture notes on ridge regression by Wessel N. van Wieringen](https://arxiv.org/abs/1509.09169)\n",
|
||||
"\n",
|
||||
"2. The textbook of [Trevor Hastie, Robert Tibshirani, Jerome H. Friedman, The Elements of Statistical Learning, Springer](https://www.springer.com/gp/book/9780387848570), chapters 3 and 7 are the most relevant ones for the analysis here."
|
||||
"2. The textbook of [Trevor Hastie, Robert Tibshirani, Jerome H. Friedman, The Elements of Statistical Learning, Springer](https://www.springer.com/gp/book/9780387848570), chapters 3 and 7 are the most relevant ones for the analysis of parts g) and h)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "5261eeb8",
|
||||
"id": "b9e04791",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -552,7 +567,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "1ede6542",
|
||||
"id": "3fab6237",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -574,7 +589,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "242e8a69",
|
||||
"id": "3388eb60",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -620,7 +635,25 @@
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {},
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"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.9.15"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "fabb4bef",
|
||||
"id": "b209e219",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -14,7 +14,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "408cd05f",
|
||||
"id": "6fa4c4bc",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -27,7 +27,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "c5348eff",
|
||||
"id": "735b16c4",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -35,17 +35,19 @@
|
||||
"## Preamble: Note on writing reports, using reference material, AI and other tools\n",
|
||||
"\n",
|
||||
"We want you to answer the three different projects by handing in\n",
|
||||
"reports written like a standard scientific/technical report. The links\n",
|
||||
"at <https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects>\n",
|
||||
"Furthermore, at the same link, \n",
|
||||
"you can find examples of previous reports. How to write reports will\n",
|
||||
"also be discussed during the various lab sessions. Please do ask us if you are in doubt.\n",
|
||||
"reports written like a standard scientific/technical report. The\n",
|
||||
"links at\n",
|
||||
"<https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects>\n",
|
||||
"contain more information. There you can find examples of previous\n",
|
||||
"reports, the projects themselves, how we rade reports etc. How to\n",
|
||||
"write reports will also be discussed during the various lab\n",
|
||||
"sessions. Please do ask us if you are in doubt.\n",
|
||||
"\n",
|
||||
"When using codes and material from other sources, you should refer to\n",
|
||||
"these in the bibliography of your report, indicating wherefrom you for\n",
|
||||
"example got the code, whether this is from the lecture notes,\n",
|
||||
"softwares like Scikit-Learn, TensorFlow, PyTorch or other sources such\n",
|
||||
"AI software. These should always be cited correctly. How to cite some\n",
|
||||
"softwares like Scikit-Learn, TensorFlow, PyTorch or other sources. These sources\n",
|
||||
"should always be cited correctly. How to cite some\n",
|
||||
"of the libraries is often indicated from their corresponding GitHub\n",
|
||||
"sites or websites, see for example how to cite Scikit-Learn at\n",
|
||||
"<https://scikit-learn.org/dev/about.html>.\n",
|
||||
@@ -73,7 +75,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "3b4ce1f7",
|
||||
"id": "0b7956ca",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -81,7 +83,7 @@
|
||||
"## Regression analysis and resampling methods\n",
|
||||
"\n",
|
||||
"The main aim of this project is to study in more detail various\n",
|
||||
"regression methods, including the Ordinary Least Squares (OLS) method.\n",
|
||||
"regression methods, including Ordinary Least Squares (OLS) reegression, Ridge regression and LASSO regression.\n",
|
||||
"In addition to the scientific part, in this course we want also to\n",
|
||||
"give you an experience in writing scientific reports.\n",
|
||||
"\n",
|
||||
@@ -93,7 +95,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "ad2e0491",
|
||||
"id": "28ba3d22",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -105,47 +107,46 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "ad3e88a2",
|
||||
"id": "9a3e10ba",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"Our first step will be to perform an OLS regression analysis of this\n",
|
||||
"function, trying out a polynomial fit with an $x$ dependence of the\n",
|
||||
"form $[x,x^2,\\dots]$. We can use a uniform distribution to set up the\n",
|
||||
"form $[x,x^2,\\dots]$. You can use a uniform distribution to set up the\n",
|
||||
"arrays of values for $x \\in [-1,1]$, or alternatively use a fixed step size.\n",
|
||||
"Thereafter we will repeat much of the\n",
|
||||
"same procedure using the Ridge and Lasso regression methods,\n",
|
||||
"introducing thus a dependence on the hyperparameter (penalty) $\\lambda$.\n",
|
||||
"Thereafter we will repeat many of the same steps when using the Ridge and Lasso regression methods,\n",
|
||||
"introducing thereby a dependence on the hyperparameter (penalty) $\\lambda$.\n",
|
||||
"\n",
|
||||
"We will also include bootstrap as a resampling technique in order to\n",
|
||||
"study the so-called **bias-variance tradeoff**. After that we will\n",
|
||||
"include the cross-validation technique."
|
||||
"include the so-called cross-validation technique."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "0cf7b59c",
|
||||
"id": "8aa547a5",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"### Part a : Ordinary Least Square (OLS) for the Runge function\n",
|
||||
"\n",
|
||||
"We will generate our own dataset for a function\n",
|
||||
"We will generate our own dataset for abovementioned function\n",
|
||||
"$\\mathrm{Runge}(x)$ function with $x\\in [-1,1]$. You should explore also the addition\n",
|
||||
"of an added stochastic noise to this function using the normal\n",
|
||||
"distribution $N(0,1)$.\n",
|
||||
"\n",
|
||||
"*Write your own code* (using for example the pseudoinverse function **pinv** from **Numpy** ) and perform a standard **ordinary least square regression**\n",
|
||||
"analysis using polynomials in $x$ up to order $15$. Explore the dependence on the number of data points and the polynomial degree.\n",
|
||||
"analysis using polynomials in $x$ up to order $15$ or higher. Explore the dependence on the number of data points and the polynomial degree.\n",
|
||||
"\n",
|
||||
"Evaluate the mean Squared error (MSE)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "fbd565ab",
|
||||
"id": "68fbf03d",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -158,7 +159,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "af65f0a5",
|
||||
"id": "b49509bc",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -170,7 +171,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "840b2c14",
|
||||
"id": "0fa4ffc6",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -182,7 +183,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "006f0e51",
|
||||
"id": "ce462b32",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -192,7 +193,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "cfe91edf",
|
||||
"id": "a5fbef36",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -204,17 +205,17 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "2aea20dd",
|
||||
"id": "a6afe9cb",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree 20).\n",
|
||||
"Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree 15).\n",
|
||||
"Plot also the parameters $\\theta$ as you increase the order of the polynomial. Comment your results.\n",
|
||||
"\n",
|
||||
"Your code has to include a scaling/centering of the data (for example by\n",
|
||||
"subtracting the mean value), and\n",
|
||||
"a split of the data in training and test data. For this exercise you can\n",
|
||||
"a split of the data in training and test data. For the scaling you can\n",
|
||||
"either write your own code or use for example the function for\n",
|
||||
"splitting training data provided by the library **Scikit-Learn** (make\n",
|
||||
"sure you have installed it). This function is called\n",
|
||||
@@ -235,7 +236,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "6be73dff",
|
||||
"id": "3be10f68",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -243,16 +244,16 @@
|
||||
"### Part b: Adding Ridge regression for the Runge function\n",
|
||||
"\n",
|
||||
"Write your own code for the Ridge method as done in the previous\n",
|
||||
"exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the exercise from week 36 is something you can reuse here.\n",
|
||||
"exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the results from the exercise set from week 36 is something you can reuse here.\n",
|
||||
"\n",
|
||||
"Perform the same analysis as you did in the previous exercise but now for different values of $\\lambda$. Compare and\n",
|
||||
"analyze your results with those obtained in part a) with the ordinary least squares method. Study the\n",
|
||||
"analyze your results with those obtained in part a) with the OLS method. Study the\n",
|
||||
"dependence on $\\lambda$."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "14a95206",
|
||||
"id": "caa7909c",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -271,7 +272,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "30cf0ac3",
|
||||
"id": "3aac4df1",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -282,12 +283,12 @@
|
||||
"the gradient descent method by including **momentum**, **ADAgrad**,\n",
|
||||
"**RMSprop** and **ADAM** as methods fro iteratively updating your learning\n",
|
||||
"rate. Discuss the results and compare the different methods applied to\n",
|
||||
"the one-dimensional Runge function."
|
||||
"the one-dimensional Runge function. The lecture notes from week 37 contain several examples on how to implement these methods."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "99fc2b25",
|
||||
"id": "d0862a53",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -296,9 +297,9 @@
|
||||
"\n",
|
||||
"LASSO regression (see lecture slides from week 36 and week 37)\n",
|
||||
"represents our first encounter with a machine learning method which\n",
|
||||
"cannot be solved through analytical expressions. Use the gradient\n",
|
||||
"cannot be solved through analytical expressions (as in OLS and Ridge regression). Use the gradient\n",
|
||||
"descent methods you developed in parts c) and d) to solve the LASSO\n",
|
||||
"optimization problem. You can compare your results using\n",
|
||||
"optimization problem. You can compare your results with \n",
|
||||
"the functionalities of **Scikit-Learn**.\n",
|
||||
"\n",
|
||||
"Discuss (critically) your results for the Runge function from OLS,\n",
|
||||
@@ -308,29 +309,31 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "26655c62",
|
||||
"id": "9170032e",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"### Part f: Stochastic gradient descent\n",
|
||||
"\n",
|
||||
"Our last gradient step is to include stochastic gradient descent using the\n",
|
||||
"same methods to update the learning rates as in parts c-e).\n",
|
||||
"Compare and discuss your results with and without stochastic gradient and give a critical assessment of the various methods."
|
||||
"Our last gradient step is to include stochastic gradient descent using\n",
|
||||
"the same methods to update the learning rates as in parts c-e).\n",
|
||||
"Compare and discuss your results with and without stochastic gradient\n",
|
||||
"and give a critical assessment of the various methods."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "990fba0d",
|
||||
"id": "bacd1035",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"### Part g: Bias-variance trade-off and resampling techniques\n",
|
||||
"\n",
|
||||
"Our aim here is to study the bias-variance trade-off by implementing the **bootstrap** resampling technique.\n",
|
||||
"**We will only use the simpler ordinary least squares here**.\n",
|
||||
"Our aim here is to study the bias-variance trade-off by implementing\n",
|
||||
"the **bootstrap** resampling technique. **We will only use the simpler\n",
|
||||
"ordinary least squares here**.\n",
|
||||
"\n",
|
||||
"With a code which does OLS and includes resampling techniques, \n",
|
||||
"we will now discuss the bias-variance trade-off in the context of\n",
|
||||
@@ -338,11 +341,14 @@
|
||||
"intuitions and ideas discussed here also carry over to classification\n",
|
||||
"tasks and basically all Machine Learning algorithms. \n",
|
||||
"\n",
|
||||
"Before you perform an analysis of the bias-variance trade-off on your test data, make\n",
|
||||
"first a figure similar to Fig. 2.11 of Hastie, Tibshirani, and\n",
|
||||
"Friedman. Figure 2.11 of this reference displays only the test and training MSEs. The test MSE can be used to \n",
|
||||
"indicate possible regions of low/high bias and variance. You will most likely not get an\n",
|
||||
"equally smooth curve!\n",
|
||||
"Before you perform an analysis of the bias-variance trade-off on your\n",
|
||||
"test data, make first a figure similar to Fig. 2.11 of Hastie,\n",
|
||||
"Tibshirani, and Friedman. Figure 2.11 of this reference displays only\n",
|
||||
"the test and training MSEs. The test MSE can be used to indicate\n",
|
||||
"possible regions of low/high bias and variance. You will most likely\n",
|
||||
"not get an equally smooth curve! You may also need to increase the\n",
|
||||
"polynomial order and play around with the number of data points as\n",
|
||||
"well (see also the exercise set from week 35).\n",
|
||||
"\n",
|
||||
"With this result we move on to the bias-variance trade-off analysis.\n",
|
||||
"\n",
|
||||
@@ -350,12 +356,12 @@
|
||||
"dataset $\\mathcal{L}$ consisting of the data\n",
|
||||
"$\\mathbf{X}_\\mathcal{L}=\\{(y_j, \\boldsymbol{x}_j), j=0\\ldots n-1\\}$.\n",
|
||||
"\n",
|
||||
"As in part d), we assume that the true data is generated from a noisy model"
|
||||
"We assume that the true data is generated from a noisy model"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "0a53c96b",
|
||||
"id": "b871ec69",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -367,7 +373,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "57a94082",
|
||||
"id": "b47c19bc",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -377,42 +383,45 @@
|
||||
"\n",
|
||||
"In our derivation of the ordinary least squares method we defined then\n",
|
||||
"an approximation to the function $f$ in terms of the parameters\n",
|
||||
"$\\boldsymbol{\\beta}$ and the design matrix $\\boldsymbol{X}$ which embody our model,\n",
|
||||
"that is $\\boldsymbol{\\tilde{y}}=\\boldsymbol{X}\\boldsymbol{\\beta}$.\n",
|
||||
"$\\boldsymbol{\\theta}$ and the design matrix $\\boldsymbol{X}$ which embody our model,\n",
|
||||
"that is $\\boldsymbol{\\tilde{y}}=\\boldsymbol{X}\\boldsymbol{\\theta}$.\n",
|
||||
"\n",
|
||||
"The parameters $\\boldsymbol{\\beta}$ are in turn found by optimizing the mean\n",
|
||||
"The parameters $\\boldsymbol{\\theta}$ are in turn found by optimizing the mean\n",
|
||||
"squared error via the so-called cost function"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "6ec185aa",
|
||||
"id": "6db622c2",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"C(\\boldsymbol{X},\\boldsymbol{\\beta}) =\\frac{1}{n}\\sum_{i=0}^{n-1}(y_i-\\tilde{y}_i)^2=\\mathbb{E}\\left[(\\boldsymbol{y}-\\boldsymbol{\\tilde{y}})^2\\right].\n",
|
||||
"C(\\boldsymbol{X},\\boldsymbol{\\theta}) =\\frac{1}{n}\\sum_{i=0}^{n-1}(y_i-\\tilde{y}_i)^2=\\mathbb{E}\\left[(\\boldsymbol{y}-\\boldsymbol{\\tilde{y}})^2\\right].\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "ad16677a",
|
||||
"id": "5a7eb70d",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"Here the expected value $\\mathbb{E}$ is the sample value. \n",
|
||||
"\n",
|
||||
"Show that you can rewrite this in terms of a term which contains the variance of the model itself (the so-called variance term), a\n",
|
||||
"term which measures the deviation from the true data and the mean value of the model (the bias term) and finally the variance of the noise.\n",
|
||||
"Show that you can rewrite this in terms of a term which contains the\n",
|
||||
"variance of the model itself (the so-called variance term), a term\n",
|
||||
"which measures the deviation from the true data and the mean value of\n",
|
||||
"the model (the bias term) and finally the variance of the noise.\n",
|
||||
"\n",
|
||||
"That is, show that"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "ea1fc9fb",
|
||||
"id": "d50292fe",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -424,17 +433,17 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "0fce3b1e",
|
||||
"id": "50fa641f",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"with"
|
||||
"with (we approximate $f(\\boldsymbol{x})\\approx \\boldsymbol{y}$)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "0a28aa27",
|
||||
"id": "2bd429c9",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -446,7 +455,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "bc135ab9",
|
||||
"id": "737c2819",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -456,7 +465,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "c7bee420",
|
||||
"id": "41ef92ef",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -468,13 +477,17 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "86efb942",
|
||||
"id": "b948dab0",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"The answer to this exercise should be included in the theory part of the report. This exercise is also part of the weekly exercises of week 38.\n",
|
||||
"Explain what the terms mean and discuss their interpretations.\n",
|
||||
"**Important note**: Since the function $f(x)$ is unknown, in order to be able to evalute the bias, we replace $f(\\boldsymbol{x})$ in the expression for the bias with $\\boldsymbol{y}$. \n",
|
||||
"\n",
|
||||
"The answer to this exercise should be included in the theory part of\n",
|
||||
"the report. This exercise is also part of the weekly exercises of\n",
|
||||
"week 38. Explain what the terms mean and discuss their\n",
|
||||
"interpretations.\n",
|
||||
"\n",
|
||||
"Perform then a bias-variance analysis of the Runge function by\n",
|
||||
"studying the MSE value as function of the complexity of your model.\n",
|
||||
@@ -487,7 +500,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "027501c3",
|
||||
"id": "6a0548bf",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -497,19 +510,20 @@
|
||||
"The aim here is to implement another widely popular\n",
|
||||
"resampling technique, the so-called cross-validation method. \n",
|
||||
"\n",
|
||||
"Implement the $k$-fold cross-validation algorithm (feel free to use the functionality of **Scikit-Learn** or write your own code) and evaluate again the MSE function resulting\n",
|
||||
"from the test folds. \n",
|
||||
"Implement the $k$-fold cross-validation algorithm (feel free to use\n",
|
||||
"the functionality of **Scikit-Learn** or write your own code) and\n",
|
||||
"evaluate again the MSE function resulting from the test folds.\n",
|
||||
"\n",
|
||||
"Compare the MSE you get from your cross-validation code with the one\n",
|
||||
"you got from your **bootstrap** code. Comment your results. Try $5-10$\n",
|
||||
"folds. \n",
|
||||
"you got from your **bootstrap** code from the previous exercise. Comment and interpret your results. \n",
|
||||
"\n",
|
||||
"In addition to using the ordinary least squares method, you should include both Ridge and Lasso regression in the analysis."
|
||||
"In addition to using the ordinary least squares method, you should\n",
|
||||
"include both Ridge and Lasso regression in the final analysis."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "3c583b47",
|
||||
"id": "df9845cb",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -518,12 +532,12 @@
|
||||
"\n",
|
||||
"1. For a discussion and derivation of the variances and mean squared errors using linear regression, see the [Lecture notes on ridge regression by Wessel N. van Wieringen](https://arxiv.org/abs/1509.09169)\n",
|
||||
"\n",
|
||||
"2. The textbook of [Trevor Hastie, Robert Tibshirani, Jerome H. Friedman, The Elements of Statistical Learning, Springer](https://www.springer.com/gp/book/9780387848570), chapters 3 and 7 are the most relevant ones for the analysis here."
|
||||
"2. The textbook of [Trevor Hastie, Robert Tibshirani, Jerome H. Friedman, The Elements of Statistical Learning, Springer](https://www.springer.com/gp/book/9780387848570), chapters 3 and 7 are the most relevant ones for the analysis of parts g) and h)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "2d085c9a",
|
||||
"id": "b9e04791",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -553,7 +567,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "6df68456",
|
||||
"id": "3fab6237",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -575,7 +589,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b68cec2c",
|
||||
"id": "3388eb60",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
@@ -621,7 +635,25 @@
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {},
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"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.9.15"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -136,17 +136,19 @@ September 2
|
||||
\subsection{Preamble: Note on writing reports, using reference material, AI and other tools}
|
||||
|
||||
We want you to answer the three different projects by handing in
|
||||
reports written like a standard scientific/technical report. The links
|
||||
at \href{{https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects}}{\nolinkurl{https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects}}
|
||||
Furthermore, at the same link,
|
||||
you can find examples of previous reports. How to write reports will
|
||||
also be discussed during the various lab sessions. Please do ask us if you are in doubt.
|
||||
reports written like a standard scientific/technical report. The
|
||||
links at
|
||||
\href{{https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects}}{\nolinkurl{https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects}}
|
||||
contain more information. There you can find examples of previous
|
||||
reports, the projects themselves, how we rade reports etc. How to
|
||||
write reports will also be discussed during the various lab
|
||||
sessions. Please do ask us if you are in doubt.
|
||||
|
||||
When using codes and material from other sources, you should refer to
|
||||
these in the bibliography of your report, indicating wherefrom you for
|
||||
example got the code, whether this is from the lecture notes,
|
||||
softwares like Scikit-Learn, TensorFlow, PyTorch or other sources such
|
||||
AI software. These should always be cited correctly. How to cite some
|
||||
softwares like Scikit-Learn, TensorFlow, PyTorch or other sources. These sources
|
||||
should always be cited correctly. How to cite some
|
||||
of the libraries is often indicated from their corresponding GitHub
|
||||
sites or websites, see for example how to cite Scikit-Learn at
|
||||
\href{{https://scikit-learn.org/dev/about.html}}{\nolinkurl{https://scikit-learn.org/dev/about.html}}.
|
||||
@@ -175,7 +177,7 @@ other data sets, make sure these are sets used for regression problems
|
||||
\subsection{Regression analysis and resampling methods}
|
||||
|
||||
The main aim of this project is to study in more detail various
|
||||
regression methods, including the Ordinary Least Squares (OLS) method.
|
||||
regression methods, including Ordinary Least Squares (OLS) reegression, Ridge regression and LASSO regression.
|
||||
In addition to the scientific part, in this course we want also to
|
||||
give you an experience in writing scientific reports.
|
||||
|
||||
@@ -189,24 +191,23 @@ f(x) = \frac{1}{1+25x^2}.
|
||||
|
||||
Our first step will be to perform an OLS regression analysis of this
|
||||
function, trying out a polynomial fit with an $x$ dependence of the
|
||||
form $[x,x^2,\dots]$. We can use a uniform distribution to set up the
|
||||
form $[x,x^2,\dots]$. You can use a uniform distribution to set up the
|
||||
arrays of values for $x \in [-1,1]$, or alternatively use a fixed step size.
|
||||
Thereafter we will repeat much of the
|
||||
same procedure using the Ridge and Lasso regression methods,
|
||||
introducing thus a dependence on the hyperparameter (penalty) $\lambda$.
|
||||
Thereafter we will repeat many of the same steps when using the Ridge and Lasso regression methods,
|
||||
introducing thereby a dependence on the hyperparameter (penalty) $\lambda$.
|
||||
|
||||
We will also include bootstrap as a resampling technique in order to
|
||||
study the so-called \textbf{bias-variance tradeoff}. After that we will
|
||||
include the cross-validation technique.
|
||||
include the so-called cross-validation technique.
|
||||
|
||||
\paragraph{Part a : Ordinary Least Square (OLS) for the Runge function.}
|
||||
We will generate our own dataset for a function
|
||||
We will generate our own dataset for abovementioned function
|
||||
$\mathrm{Runge}(x)$ function with $x\in [-1,1]$. You should explore also the addition
|
||||
of an added stochastic noise to this function using the normal
|
||||
distribution $N(0,1)$.
|
||||
|
||||
\emph{Write your own code} (using for example the pseudoinverse function \textbf{pinv} from \textbf{Numpy} ) and perform a standard \textbf{ordinary least square regression}
|
||||
analysis using polynomials in $x$ up to order $15$. Explore the dependence on the number of data points and the polynomial degree.
|
||||
analysis using polynomials in $x$ up to order $15$ or higher. Explore the dependence on the number of data points and the polynomial degree.
|
||||
|
||||
Evaluate the mean Squared error (MSE)
|
||||
|
||||
@@ -228,12 +229,12 @@ where we have defined the mean value of $\bm{y}$ as
|
||||
\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i.
|
||||
\]
|
||||
|
||||
Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree 20).
|
||||
Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree 15).
|
||||
Plot also the parameters $\theta$ as you increase the order of the polynomial. Comment your results.
|
||||
|
||||
Your code has to include a scaling/centering of the data (for example by
|
||||
subtracting the mean value), and
|
||||
a split of the data in training and test data. For this exercise you can
|
||||
a split of the data in training and test data. For the scaling you can
|
||||
either write your own code or use for example the function for
|
||||
splitting training data provided by the library \textbf{Scikit-Learn} (make
|
||||
sure you have installed it). This function is called
|
||||
@@ -253,10 +254,10 @@ On scaling, we recommend reading the following section from the scikit-learn sof
|
||||
|
||||
\paragraph{Part b: Adding Ridge regression for the Runge function.}
|
||||
Write your own code for the Ridge method as done in the previous
|
||||
exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the exercise from week 36 is something you can reuse here.
|
||||
exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the results from the exercise set from week 36 is something you can reuse here.
|
||||
|
||||
Perform the same analysis as you did in the previous exercise but now for different values of $\lambda$. Compare and
|
||||
analyze your results with those obtained in part a) with the ordinary least squares method. Study the
|
||||
analyze your results with those obtained in part a) with the OLS method. Study the
|
||||
dependence on $\lambda$.
|
||||
|
||||
\paragraph{Part c: Writing your own gradient descent code.}
|
||||
@@ -274,14 +275,14 @@ We keep our focus on OLS and Ridge regression and update our code for
|
||||
the gradient descent method by including \textbf{momentum}, \textbf{ADAgrad},
|
||||
\textbf{RMSprop} and \textbf{ADAM} as methods fro iteratively updating your learning
|
||||
rate. Discuss the results and compare the different methods applied to
|
||||
the one-dimensional Runge function.
|
||||
the one-dimensional Runge function. The lecture notes from week 37 contain several examples on how to implement these methods.
|
||||
|
||||
\paragraph{Part e: Writing our own code for Lasso regression.}
|
||||
LASSO regression (see lecture slides from week 36 and week 37)
|
||||
represents our first encounter with a machine learning method which
|
||||
cannot be solved through analytical expressions. Use the gradient
|
||||
cannot be solved through analytical expressions (as in OLS and Ridge regression). Use the gradient
|
||||
descent methods you developed in parts c) and d) to solve the LASSO
|
||||
optimization problem. You can compare your results using
|
||||
optimization problem. You can compare your results with
|
||||
the functionalities of \textbf{Scikit-Learn}.
|
||||
|
||||
Discuss (critically) your results for the Runge function from OLS,
|
||||
@@ -289,13 +290,15 @@ Ridge and LASSO regression using the various gradient descent
|
||||
approaches.
|
||||
|
||||
\paragraph{Part f: Stochastic gradient descent.}
|
||||
Our last gradient step is to include stochastic gradient descent using the
|
||||
same methods to update the learning rates as in parts c-e).
|
||||
Compare and discuss your results with and without stochastic gradient and give a critical assessment of the various methods.
|
||||
Our last gradient step is to include stochastic gradient descent using
|
||||
the same methods to update the learning rates as in parts c-e).
|
||||
Compare and discuss your results with and without stochastic gradient
|
||||
and give a critical assessment of the various methods.
|
||||
|
||||
\paragraph{Part g: Bias-variance trade-off and resampling techniques.}
|
||||
Our aim here is to study the bias-variance trade-off by implementing the \textbf{bootstrap} resampling technique.
|
||||
\textbf{We will only use the simpler ordinary least squares here}.
|
||||
Our aim here is to study the bias-variance trade-off by implementing
|
||||
the \textbf{bootstrap} resampling technique. \textbf{We will only use the simpler
|
||||
ordinary least squares here}.
|
||||
|
||||
With a code which does OLS and includes resampling techniques,
|
||||
we will now discuss the bias-variance trade-off in the context of
|
||||
@@ -303,11 +306,14 @@ continuous predictions such as regression. However, many of the
|
||||
intuitions and ideas discussed here also carry over to classification
|
||||
tasks and basically all Machine Learning algorithms.
|
||||
|
||||
Before you perform an analysis of the bias-variance trade-off on your test data, make
|
||||
first a figure similar to Fig.~2.11 of Hastie, Tibshirani, and
|
||||
Friedman. Figure 2.11 of this reference displays only the test and training MSEs. The test MSE can be used to
|
||||
indicate possible regions of low/high bias and variance. You will most likely not get an
|
||||
equally smooth curve!
|
||||
Before you perform an analysis of the bias-variance trade-off on your
|
||||
test data, make first a figure similar to Fig.~2.11 of Hastie,
|
||||
Tibshirani, and Friedman. Figure 2.11 of this reference displays only
|
||||
the test and training MSEs. The test MSE can be used to indicate
|
||||
possible regions of low/high bias and variance. You will most likely
|
||||
not get an equally smooth curve! You may also need to increase the
|
||||
polynomial order and play around with the number of data points as
|
||||
well (see also the exercise set from week 35).
|
||||
|
||||
With this result we move on to the bias-variance trade-off analysis.
|
||||
|
||||
@@ -315,7 +321,7 @@ Consider a
|
||||
dataset $\mathcal{L}$ consisting of the data
|
||||
$\mathbf{X}_\mathcal{L}=\{(y_j, \boldsymbol{x}_j), j=0\ldots n-1\}$.
|
||||
|
||||
As in part d), we assume that the true data is generated from a noisy model
|
||||
We assume that the true data is generated from a noisy model
|
||||
|
||||
\[
|
||||
\bm{y}=f(\boldsymbol{x}) + \bm{\epsilon}.
|
||||
@@ -326,24 +332,27 @@ deviation $\sigma^2$.
|
||||
|
||||
In our derivation of the ordinary least squares method we defined then
|
||||
an approximation to the function $f$ in terms of the parameters
|
||||
$\bm{\beta}$ and the design matrix $\bm{X}$ which embody our model,
|
||||
that is $\bm{\tilde{y}}=\bm{X}\bm{\beta}$.
|
||||
$\bm{\theta}$ and the design matrix $\bm{X}$ which embody our model,
|
||||
that is $\bm{\tilde{y}}=\bm{X}\bm{\theta}$.
|
||||
|
||||
The parameters $\bm{\beta}$ are in turn found by optimizing the mean
|
||||
The parameters $\bm{\theta}$ are in turn found by optimizing the mean
|
||||
squared error via the so-called cost function
|
||||
|
||||
\[
|
||||
C(\bm{X},\bm{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\bm{y}-\bm{\tilde{y}})^2\right].
|
||||
C(\bm{X},\bm{\theta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\bm{y}-\bm{\tilde{y}})^2\right].
|
||||
\]
|
||||
Here the expected value $\mathbb{E}$ is the sample value.
|
||||
|
||||
Show that you can rewrite this in terms of a term which contains the variance of the model itself (the so-called variance term), a
|
||||
term which measures the deviation from the true data and the mean value of the model (the bias term) and finally the variance of the noise.
|
||||
Show that you can rewrite this in terms of a term which contains the
|
||||
variance of the model itself (the so-called variance term), a term
|
||||
which measures the deviation from the true data and the mean value of
|
||||
the model (the bias term) and finally the variance of the noise.
|
||||
|
||||
That is, show that
|
||||
\[
|
||||
\mathbb{E}\left[(\bm{y}-\bm{\tilde{y}})^2\right]=\mathrm{Bias}[\tilde{y}]+\mathrm{var}[\tilde{y}]+\sigma^2,
|
||||
\]
|
||||
with
|
||||
with (we approximate $f(\bm{x})\approx \bm{y}$)
|
||||
\[
|
||||
\mathrm{Bias}[\tilde{y}]=\mathbb{E}\left[\left(\bm{y}-\mathbb{E}\left[\bm{\tilde{y}}\right]\right)^2\right],
|
||||
\]
|
||||
@@ -351,8 +360,13 @@ and
|
||||
\[
|
||||
\mathrm{var}[\tilde{y}]=\mathbb{E}\left[\left(\tilde{\bm{y}}-\mathbb{E}\left[\bm{\tilde{y}}\right]\right)^2\right]=\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\bm{\tilde{y}}\right])^2.
|
||||
\]
|
||||
The answer to this exercise should be included in the theory part of the report. This exercise is also part of the weekly exercises of week 38.
|
||||
Explain what the terms mean and discuss their interpretations.
|
||||
|
||||
\textbf{Important note}: Since the function $f(x)$ is unknown, in order to be able to evalute the bias, we replace $f(\bm{x})$ in the expression for the bias with $\bm{y}$.
|
||||
|
||||
The answer to this exercise should be included in the theory part of
|
||||
the report. This exercise is also part of the weekly exercises of
|
||||
week 38. Explain what the terms mean and discuss their
|
||||
interpretations.
|
||||
|
||||
Perform then a bias-variance analysis of the Runge function by
|
||||
studying the MSE value as function of the complexity of your model.
|
||||
@@ -366,14 +380,15 @@ You can follow the code example in the jupyter-book at \href{{https://compphysic
|
||||
The aim here is to implement another widely popular
|
||||
resampling technique, the so-called cross-validation method.
|
||||
|
||||
Implement the $k$-fold cross-validation algorithm (feel free to use the functionality of \textbf{Scikit-Learn} or write your own code) and evaluate again the MSE function resulting
|
||||
from the test folds.
|
||||
Implement the $k$-fold cross-validation algorithm (feel free to use
|
||||
the functionality of \textbf{Scikit-Learn} or write your own code) and
|
||||
evaluate again the MSE function resulting from the test folds.
|
||||
|
||||
Compare the MSE you get from your cross-validation code with the one
|
||||
you got from your \textbf{bootstrap} code. Comment your results. Try $5-10$
|
||||
folds.
|
||||
you got from your \textbf{bootstrap} code from the previous exercise. Comment and interpret your results.
|
||||
|
||||
In addition to using the ordinary least squares method, you should include both Ridge and Lasso regression in the analysis.
|
||||
In addition to using the ordinary least squares method, you should
|
||||
include both Ridge and Lasso regression in the final analysis.
|
||||
|
||||
\subsection{Background literature}
|
||||
|
||||
|
||||
@@ -110,17 +110,19 @@ September 2
|
||||
\subsection*{Preamble: Note on writing reports, using reference material, AI and other tools}
|
||||
|
||||
We want you to answer the three different projects by handing in
|
||||
reports written like a standard scientific/technical report. The links
|
||||
at \href{{https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects}}{\nolinkurl{https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects}}
|
||||
Furthermore, at the same link,
|
||||
you can find examples of previous reports. How to write reports will
|
||||
also be discussed during the various lab sessions. Please do ask us if you are in doubt.
|
||||
reports written like a standard scientific/technical report. The
|
||||
links at
|
||||
\href{{https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects}}{\nolinkurl{https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects}}
|
||||
contain more information. There you can find examples of previous
|
||||
reports, the projects themselves, how we rade reports etc. How to
|
||||
write reports will also be discussed during the various lab
|
||||
sessions. Please do ask us if you are in doubt.
|
||||
|
||||
When using codes and material from other sources, you should refer to
|
||||
these in the bibliography of your report, indicating wherefrom you for
|
||||
example got the code, whether this is from the lecture notes,
|
||||
softwares like Scikit-Learn, TensorFlow, PyTorch or other sources such
|
||||
AI software. These should always be cited correctly. How to cite some
|
||||
softwares like Scikit-Learn, TensorFlow, PyTorch or other sources. These sources
|
||||
should always be cited correctly. How to cite some
|
||||
of the libraries is often indicated from their corresponding GitHub
|
||||
sites or websites, see for example how to cite Scikit-Learn at
|
||||
\href{{https://scikit-learn.org/dev/about.html}}{\nolinkurl{https://scikit-learn.org/dev/about.html}}.
|
||||
@@ -149,7 +151,7 @@ other data sets, make sure these are sets used for regression problems
|
||||
\subsection*{Regression analysis and resampling methods}
|
||||
|
||||
The main aim of this project is to study in more detail various
|
||||
regression methods, including the Ordinary Least Squares (OLS) method.
|
||||
regression methods, including Ordinary Least Squares (OLS) reegression, Ridge regression and LASSO regression.
|
||||
In addition to the scientific part, in this course we want also to
|
||||
give you an experience in writing scientific reports.
|
||||
|
||||
@@ -163,24 +165,23 @@ f(x) = \frac{1}{1+25x^2}.
|
||||
|
||||
Our first step will be to perform an OLS regression analysis of this
|
||||
function, trying out a polynomial fit with an $x$ dependence of the
|
||||
form $[x,x^2,\dots]$. We can use a uniform distribution to set up the
|
||||
form $[x,x^2,\dots]$. You can use a uniform distribution to set up the
|
||||
arrays of values for $x \in [-1,1]$, or alternatively use a fixed step size.
|
||||
Thereafter we will repeat much of the
|
||||
same procedure using the Ridge and Lasso regression methods,
|
||||
introducing thus a dependence on the hyperparameter (penalty) $\lambda$.
|
||||
Thereafter we will repeat many of the same steps when using the Ridge and Lasso regression methods,
|
||||
introducing thereby a dependence on the hyperparameter (penalty) $\lambda$.
|
||||
|
||||
We will also include bootstrap as a resampling technique in order to
|
||||
study the so-called \textbf{bias-variance tradeoff}. After that we will
|
||||
include the cross-validation technique.
|
||||
include the so-called cross-validation technique.
|
||||
|
||||
\paragraph{Part a : Ordinary Least Square (OLS) for the Runge function.}
|
||||
We will generate our own dataset for a function
|
||||
We will generate our own dataset for abovementioned function
|
||||
$\mathrm{Runge}(x)$ function with $x\in [-1,1]$. You should explore also the addition
|
||||
of an added stochastic noise to this function using the normal
|
||||
distribution $N(0,1)$.
|
||||
|
||||
\emph{Write your own code} (using for example the pseudoinverse function \textbf{pinv} from \textbf{Numpy} ) and perform a standard \textbf{ordinary least square regression}
|
||||
analysis using polynomials in $x$ up to order $15$. Explore the dependence on the number of data points and the polynomial degree.
|
||||
analysis using polynomials in $x$ up to order $15$ or higher. Explore the dependence on the number of data points and the polynomial degree.
|
||||
|
||||
Evaluate the mean Squared error (MSE)
|
||||
|
||||
@@ -202,12 +203,12 @@ where we have defined the mean value of $\bm{y}$ as
|
||||
\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i.
|
||||
\]
|
||||
|
||||
Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree 20).
|
||||
Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree 15).
|
||||
Plot also the parameters $\theta$ as you increase the order of the polynomial. Comment your results.
|
||||
|
||||
Your code has to include a scaling/centering of the data (for example by
|
||||
subtracting the mean value), and
|
||||
a split of the data in training and test data. For this exercise you can
|
||||
a split of the data in training and test data. For the scaling you can
|
||||
either write your own code or use for example the function for
|
||||
splitting training data provided by the library \textbf{Scikit-Learn} (make
|
||||
sure you have installed it). This function is called
|
||||
@@ -227,10 +228,10 @@ On scaling, we recommend reading the following section from the scikit-learn sof
|
||||
|
||||
\paragraph{Part b: Adding Ridge regression for the Runge function.}
|
||||
Write your own code for the Ridge method as done in the previous
|
||||
exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the exercise from week 36 is something you can reuse here.
|
||||
exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the results from the exercise set from week 36 is something you can reuse here.
|
||||
|
||||
Perform the same analysis as you did in the previous exercise but now for different values of $\lambda$. Compare and
|
||||
analyze your results with those obtained in part a) with the ordinary least squares method. Study the
|
||||
analyze your results with those obtained in part a) with the OLS method. Study the
|
||||
dependence on $\lambda$.
|
||||
|
||||
\paragraph{Part c: Writing your own gradient descent code.}
|
||||
@@ -248,14 +249,14 @@ We keep our focus on OLS and Ridge regression and update our code for
|
||||
the gradient descent method by including \textbf{momentum}, \textbf{ADAgrad},
|
||||
\textbf{RMSprop} and \textbf{ADAM} as methods fro iteratively updating your learning
|
||||
rate. Discuss the results and compare the different methods applied to
|
||||
the one-dimensional Runge function.
|
||||
the one-dimensional Runge function. The lecture notes from week 37 contain several examples on how to implement these methods.
|
||||
|
||||
\paragraph{Part e: Writing our own code for Lasso regression.}
|
||||
LASSO regression (see lecture slides from week 36 and week 37)
|
||||
represents our first encounter with a machine learning method which
|
||||
cannot be solved through analytical expressions. Use the gradient
|
||||
cannot be solved through analytical expressions (as in OLS and Ridge regression). Use the gradient
|
||||
descent methods you developed in parts c) and d) to solve the LASSO
|
||||
optimization problem. You can compare your results using
|
||||
optimization problem. You can compare your results with
|
||||
the functionalities of \textbf{Scikit-Learn}.
|
||||
|
||||
Discuss (critically) your results for the Runge function from OLS,
|
||||
@@ -263,13 +264,15 @@ Ridge and LASSO regression using the various gradient descent
|
||||
approaches.
|
||||
|
||||
\paragraph{Part f: Stochastic gradient descent.}
|
||||
Our last gradient step is to include stochastic gradient descent using the
|
||||
same methods to update the learning rates as in parts c-e).
|
||||
Compare and discuss your results with and without stochastic gradient and give a critical assessment of the various methods.
|
||||
Our last gradient step is to include stochastic gradient descent using
|
||||
the same methods to update the learning rates as in parts c-e).
|
||||
Compare and discuss your results with and without stochastic gradient
|
||||
and give a critical assessment of the various methods.
|
||||
|
||||
\paragraph{Part g: Bias-variance trade-off and resampling techniques.}
|
||||
Our aim here is to study the bias-variance trade-off by implementing the \textbf{bootstrap} resampling technique.
|
||||
\textbf{We will only use the simpler ordinary least squares here}.
|
||||
Our aim here is to study the bias-variance trade-off by implementing
|
||||
the \textbf{bootstrap} resampling technique. \textbf{We will only use the simpler
|
||||
ordinary least squares here}.
|
||||
|
||||
With a code which does OLS and includes resampling techniques,
|
||||
we will now discuss the bias-variance trade-off in the context of
|
||||
@@ -277,11 +280,14 @@ continuous predictions such as regression. However, many of the
|
||||
intuitions and ideas discussed here also carry over to classification
|
||||
tasks and basically all Machine Learning algorithms.
|
||||
|
||||
Before you perform an analysis of the bias-variance trade-off on your test data, make
|
||||
first a figure similar to Fig.~2.11 of Hastie, Tibshirani, and
|
||||
Friedman. Figure 2.11 of this reference displays only the test and training MSEs. The test MSE can be used to
|
||||
indicate possible regions of low/high bias and variance. You will most likely not get an
|
||||
equally smooth curve!
|
||||
Before you perform an analysis of the bias-variance trade-off on your
|
||||
test data, make first a figure similar to Fig.~2.11 of Hastie,
|
||||
Tibshirani, and Friedman. Figure 2.11 of this reference displays only
|
||||
the test and training MSEs. The test MSE can be used to indicate
|
||||
possible regions of low/high bias and variance. You will most likely
|
||||
not get an equally smooth curve! You may also need to increase the
|
||||
polynomial order and play around with the number of data points as
|
||||
well (see also the exercise set from week 35).
|
||||
|
||||
With this result we move on to the bias-variance trade-off analysis.
|
||||
|
||||
@@ -289,7 +295,7 @@ Consider a
|
||||
dataset $\mathcal{L}$ consisting of the data
|
||||
$\mathbf{X}_\mathcal{L}=\{(y_j, \boldsymbol{x}_j), j=0\ldots n-1\}$.
|
||||
|
||||
As in part d), we assume that the true data is generated from a noisy model
|
||||
We assume that the true data is generated from a noisy model
|
||||
|
||||
\[
|
||||
\bm{y}=f(\boldsymbol{x}) + \bm{\epsilon}.
|
||||
@@ -300,24 +306,27 @@ deviation $\sigma^2$.
|
||||
|
||||
In our derivation of the ordinary least squares method we defined then
|
||||
an approximation to the function $f$ in terms of the parameters
|
||||
$\bm{\beta}$ and the design matrix $\bm{X}$ which embody our model,
|
||||
that is $\bm{\tilde{y}}=\bm{X}\bm{\beta}$.
|
||||
$\bm{\theta}$ and the design matrix $\bm{X}$ which embody our model,
|
||||
that is $\bm{\tilde{y}}=\bm{X}\bm{\theta}$.
|
||||
|
||||
The parameters $\bm{\beta}$ are in turn found by optimizing the mean
|
||||
The parameters $\bm{\theta}$ are in turn found by optimizing the mean
|
||||
squared error via the so-called cost function
|
||||
|
||||
\[
|
||||
C(\bm{X},\bm{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\bm{y}-\bm{\tilde{y}})^2\right].
|
||||
C(\bm{X},\bm{\theta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\bm{y}-\bm{\tilde{y}})^2\right].
|
||||
\]
|
||||
Here the expected value $\mathbb{E}$ is the sample value.
|
||||
|
||||
Show that you can rewrite this in terms of a term which contains the variance of the model itself (the so-called variance term), a
|
||||
term which measures the deviation from the true data and the mean value of the model (the bias term) and finally the variance of the noise.
|
||||
Show that you can rewrite this in terms of a term which contains the
|
||||
variance of the model itself (the so-called variance term), a term
|
||||
which measures the deviation from the true data and the mean value of
|
||||
the model (the bias term) and finally the variance of the noise.
|
||||
|
||||
That is, show that
|
||||
\[
|
||||
\mathbb{E}\left[(\bm{y}-\bm{\tilde{y}})^2\right]=\mathrm{Bias}[\tilde{y}]+\mathrm{var}[\tilde{y}]+\sigma^2,
|
||||
\]
|
||||
with
|
||||
with (we approximate $f(\bm{x})\approx \bm{y}$)
|
||||
\[
|
||||
\mathrm{Bias}[\tilde{y}]=\mathbb{E}\left[\left(\bm{y}-\mathbb{E}\left[\bm{\tilde{y}}\right]\right)^2\right],
|
||||
\]
|
||||
@@ -325,8 +334,13 @@ and
|
||||
\[
|
||||
\mathrm{var}[\tilde{y}]=\mathbb{E}\left[\left(\tilde{\bm{y}}-\mathbb{E}\left[\bm{\tilde{y}}\right]\right)^2\right]=\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\bm{\tilde{y}}\right])^2.
|
||||
\]
|
||||
The answer to this exercise should be included in the theory part of the report. This exercise is also part of the weekly exercises of week 38.
|
||||
Explain what the terms mean and discuss their interpretations.
|
||||
|
||||
\textbf{Important note}: Since the function $f(x)$ is unknown, in order to be able to evalute the bias, we replace $f(\bm{x})$ in the expression for the bias with $\bm{y}$.
|
||||
|
||||
The answer to this exercise should be included in the theory part of
|
||||
the report. This exercise is also part of the weekly exercises of
|
||||
week 38. Explain what the terms mean and discuss their
|
||||
interpretations.
|
||||
|
||||
Perform then a bias-variance analysis of the Runge function by
|
||||
studying the MSE value as function of the complexity of your model.
|
||||
@@ -340,14 +354,15 @@ You can follow the code example in the jupyter-book at \href{{https://compphysic
|
||||
The aim here is to implement another widely popular
|
||||
resampling technique, the so-called cross-validation method.
|
||||
|
||||
Implement the $k$-fold cross-validation algorithm (feel free to use the functionality of \textbf{Scikit-Learn} or write your own code) and evaluate again the MSE function resulting
|
||||
from the test folds.
|
||||
Implement the $k$-fold cross-validation algorithm (feel free to use
|
||||
the functionality of \textbf{Scikit-Learn} or write your own code) and
|
||||
evaluate again the MSE function resulting from the test folds.
|
||||
|
||||
Compare the MSE you get from your cross-validation code with the one
|
||||
you got from your \textbf{bootstrap} code. Comment your results. Try $5-10$
|
||||
folds.
|
||||
you got from your \textbf{bootstrap} code from the previous exercise. Comment and interpret your results.
|
||||
|
||||
In addition to using the ordinary least squares method, you should include both Ridge and Lasso regression in the analysis.
|
||||
In addition to using the ordinary least squares method, you should
|
||||
include both Ridge and Lasso regression in the final analysis.
|
||||
|
||||
\subsection*{Background literature}
|
||||
|
||||
|
||||
@@ -0,0 +1,238 @@
|
||||
<!--
|
||||
HTML file automatically generated from DocOnce source
|
||||
(https://github.com/doconce/doconce/)
|
||||
doconce format html Project1.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=Project1-bs
|
||||
-->
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/doconce/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Project 1 on Machine Learning, deadline October 6 (midnight), 2025">
|
||||
<title>Project 1 on Machine Learning, deadline October 6 (midnight), 2025</title>
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<!-- doconce format html Project1.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=Project1-bs -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
<!-- not necessary
|
||||
<link href="https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
|
||||
-->
|
||||
<style type="text/css">
|
||||
/* Add scrollbar to dropdown menus in bootstrap navigation bar */
|
||||
.dropdown-menu {
|
||||
height: auto;
|
||||
max-height: 400px;
|
||||
overflow-x: hidden;
|
||||
}
|
||||
/* Adds an invisible element before each target to offset for the navigation
|
||||
bar */
|
||||
.anchor::before {
|
||||
content:"";
|
||||
display:block;
|
||||
height:50px; /* fixed header height for style bootstrap */
|
||||
margin:-50px 0 0; /* negative fixed header height */
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Preamble: Note on writing reports, using reference material, '
|
||||
'AI and other tools',
|
||||
2,
|
||||
None,
|
||||
'preamble-note-on-writing-reports-using-reference-material-ai-and-other-tools'),
|
||||
('Regression analysis and resampling methods',
|
||||
2,
|
||||
None,
|
||||
'regression-analysis-and-resampling-methods'),
|
||||
('Part a : Ordinary Least Square (OLS) for the Runge function',
|
||||
3,
|
||||
None,
|
||||
'part-a-ordinary-least-square-ols-for-the-runge-function'),
|
||||
('Part b: Adding Ridge regression for the Runge function',
|
||||
3,
|
||||
None,
|
||||
'part-b-adding-ridge-regression-for-the-runge-function'),
|
||||
('Part c: Writing your own gradient descent code',
|
||||
3,
|
||||
None,
|
||||
'part-c-writing-your-own-gradient-descent-code'),
|
||||
('Part d: Including momentum and more advanced ways to update '
|
||||
'the learning the rate',
|
||||
3,
|
||||
None,
|
||||
'part-d-including-momentum-and-more-advanced-ways-to-update-the-learning-the-rate'),
|
||||
('Part e: Writing our own code for Lasso regression',
|
||||
3,
|
||||
None,
|
||||
'part-e-writing-our-own-code-for-lasso-regression'),
|
||||
('Part f: Stochastic gradient descent',
|
||||
3,
|
||||
None,
|
||||
'part-f-stochastic-gradient-descent'),
|
||||
('Part g: Bias-variance trade-off and resampling techniques',
|
||||
3,
|
||||
None,
|
||||
'part-g-bias-variance-trade-off-and-resampling-techniques'),
|
||||
('Part h): Cross-validation as resampling techniques, adding '
|
||||
'more complexity',
|
||||
3,
|
||||
None,
|
||||
'part-h-cross-validation-as-resampling-techniques-adding-more-complexity'),
|
||||
('Background literature', 2, None, 'background-literature'),
|
||||
('Introduction to numerical projects',
|
||||
2,
|
||||
None,
|
||||
'introduction-to-numerical-projects'),
|
||||
('Format for electronic delivery of report and programs',
|
||||
2,
|
||||
None,
|
||||
'format-for-electronic-delivery-of-report-and-programs'),
|
||||
('Software and needed installations',
|
||||
2,
|
||||
None,
|
||||
'software-and-needed-installations')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
|
||||
|
||||
|
||||
<script type="text/x-mathjax-config">
|
||||
MathJax.Hub.Config({
|
||||
TeX: {
|
||||
equationNumbers: { autoNumber: "none" },
|
||||
extensions: ["AMSmath.js", "AMSsymbols.js", "autobold.js", "color.js"]
|
||||
}
|
||||
});
|
||||
</script>
|
||||
<script type="text/javascript" async
|
||||
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
|
||||
</script>
|
||||
|
||||
|
||||
<!-- Bootstrap navigation bar -->
|
||||
<div class="navbar navbar-default navbar-fixed-top">
|
||||
<div class="navbar-header">
|
||||
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="Project1-bs.html">Project 1 on Machine Learning, deadline October 6 (midnight), 2025</a>
|
||||
</div>
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
<ul class="nav navbar-nav navbar-right">
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="#preamble-note-on-writing-reports-using-reference-material-ai-and-other-tools" style="font-size: 80%;"><b>Preamble: Note on writing reports, using reference material, AI and other tools</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._Project1-bs001.html#regression-analysis-and-resampling-methods" style="font-size: 80%;"><b>Regression analysis and resampling methods</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._Project1-bs001.html#part-a-ordinary-least-square-ols-for-the-runge-function" style="font-size: 80%;"> Part a : Ordinary Least Square (OLS) for the Runge function</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._Project1-bs001.html#part-b-adding-ridge-regression-for-the-runge-function" style="font-size: 80%;"> Part b: Adding Ridge regression for the Runge function</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._Project1-bs001.html#part-c-writing-your-own-gradient-descent-code" style="font-size: 80%;"> Part c: Writing your own gradient descent code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._Project1-bs001.html#part-d-including-momentum-and-more-advanced-ways-to-update-the-learning-the-rate" style="font-size: 80%;"> Part d: Including momentum and more advanced ways to update the learning the rate</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._Project1-bs001.html#part-e-writing-our-own-code-for-lasso-regression" style="font-size: 80%;"> Part e: Writing our own code for Lasso regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._Project1-bs001.html#part-f-stochastic-gradient-descent" style="font-size: 80%;"> Part f: Stochastic gradient descent</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._Project1-bs001.html#part-g-bias-variance-trade-off-and-resampling-techniques" style="font-size: 80%;"> Part g: Bias-variance trade-off and resampling techniques</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._Project1-bs001.html#part-h-cross-validation-as-resampling-techniques-adding-more-complexity" style="font-size: 80%;"> Part h): Cross-validation as resampling techniques, adding more complexity</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._Project1-bs001.html#background-literature" style="font-size: 80%;"><b>Background literature</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._Project1-bs001.html#introduction-to-numerical-projects" style="font-size: 80%;"><b>Introduction to numerical projects</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._Project1-bs001.html#format-for-electronic-delivery-of-report-and-programs" style="font-size: 80%;"><b>Format for electronic delivery of report and programs</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._Project1-bs001.html#software-and-needed-installations" style="font-size: 80%;"><b>Software and needed installations</b></a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div> <!-- end of navigation bar -->
|
||||
<div class="container">
|
||||
<p> </p><p> </p><p> </p> <!-- add vertical space -->
|
||||
<a name="part0000"></a>
|
||||
<!-- ------------------- main content ---------------------- -->
|
||||
<div class="jumbotron">
|
||||
<center>
|
||||
<h1>Project 1 on Machine Learning, deadline October 6 (midnight), 2025</h1>
|
||||
</center> <!-- document title -->
|
||||
|
||||
<!-- author(s): Data Analysis and Machine Learning FYS-STK3155/FYS4155 -->
|
||||
<center>
|
||||
<b>Data Analysis and Machine Learning FYS-STK3155/FYS4155</b>
|
||||
</center>
|
||||
<!-- institution -->
|
||||
<center>
|
||||
<b>University of Oslo, Norway</b>
|
||||
</center>
|
||||
<br>
|
||||
<center>
|
||||
<h4>September 2</h4>
|
||||
</center> <!-- date -->
|
||||
<br>
|
||||
|
||||
|
||||
</div> <!-- end jumbotron -->
|
||||
<h2 id="preamble-note-on-writing-reports-using-reference-material-ai-and-other-tools" class="anchor">Preamble: Note on writing reports, using reference material, AI and other tools </h2>
|
||||
|
||||
<p>We want you to answer the three different projects by handing in
|
||||
reports written like a standard scientific/technical report. The
|
||||
links at
|
||||
<a href="https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects" target="_self"><tt>https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects</tt></a>
|
||||
contain more information. There you can find examples of previous
|
||||
reports, the projects themselves, how we rade reports etc. How to
|
||||
write reports will also be discussed during the various lab
|
||||
sessions. Please do ask us if you are in doubt.
|
||||
</p>
|
||||
|
||||
<p>When using codes and material from other sources, you should refer to
|
||||
these in the bibliography of your report, indicating wherefrom you for
|
||||
example got the code, whether this is from the lecture notes,
|
||||
softwares like Scikit-Learn, TensorFlow, PyTorch or other sources. These sources
|
||||
should always be cited correctly. How to cite some
|
||||
of the libraries is often indicated from their corresponding GitHub
|
||||
sites or websites, see for example how to cite Scikit-Learn at
|
||||
<a href="https://scikit-learn.org/dev/about.html" target="_self"><tt>https://scikit-learn.org/dev/about.html</tt></a>.
|
||||
</p>
|
||||
|
||||
<p>We enocurage you to use tools like
|
||||
<a href="https://openai.com/chatgpt/" target="_self">ChatGPT</a> or similar in writing the report. If you use for example ChatGPT,
|
||||
please do cite it properly and include (if possible) your questions and answers as an addition to the report. This can
|
||||
be uploaded to for example your website, GitHub/GitLab or similar as supplemental material.
|
||||
</p>
|
||||
|
||||
<p>If you would like to study other data sets, feel free to propose other
|
||||
sets. What we have proposed here are mere suggestions from our
|
||||
side. If you opt for another data set, consider using a set which has
|
||||
been studied in the scientific literature. This makes it easier for
|
||||
you to compare and analyze your results. Comparing with existing
|
||||
results from the scientific literature is also an essential element of
|
||||
the scientific discussion. The University of California at Irvine
|
||||
with its Machine Learning repository at
|
||||
<a href="https://archive.ics.uci.edu/ml/index.php" target="_self"><tt>https://archive.ics.uci.edu/ml/index.php</tt></a> is an excellent site to
|
||||
look up for examples and
|
||||
inspiration. <a href="https://www.kaggle.com/" target="_self">Kaggle.com</a> is an equally
|
||||
interesting site. Feel free to explore these sites. When selecting
|
||||
other data sets, make sure these are sets used for regression problems
|
||||
(not classification).
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
<ul class="pagination">
|
||||
<li class="active"><a href="._Project1-bs000.html">1</a></li>
|
||||
<li><a href="._Project1-bs001.html">2</a></li>
|
||||
<li><a href="._Project1-bs001.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
</div> <!-- end container -->
|
||||
<!-- include javascript, jQuery *first* -->
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
|
||||
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
|
||||
<!-- Bootstrap footer
|
||||
<footer>
|
||||
<a href="https://..."><img width="250" align=right src="https://..."></a>
|
||||
</footer>
|
||||
-->
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -0,0 +1,503 @@
|
||||
<!--
|
||||
HTML file automatically generated from DocOnce source
|
||||
(https://github.com/doconce/doconce/)
|
||||
doconce format html Project1.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=Project1-bs
|
||||
-->
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/doconce/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Project 1 on Machine Learning, deadline October 6 (midnight), 2025">
|
||||
<title>Project 1 on Machine Learning, deadline October 6 (midnight), 2025</title>
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<!-- doconce format html Project1.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=Project1-bs -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
<!-- not necessary
|
||||
<link href="https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
|
||||
-->
|
||||
<style type="text/css">
|
||||
/* Add scrollbar to dropdown menus in bootstrap navigation bar */
|
||||
.dropdown-menu {
|
||||
height: auto;
|
||||
max-height: 400px;
|
||||
overflow-x: hidden;
|
||||
}
|
||||
/* Adds an invisible element before each target to offset for the navigation
|
||||
bar */
|
||||
.anchor::before {
|
||||
content:"";
|
||||
display:block;
|
||||
height:50px; /* fixed header height for style bootstrap */
|
||||
margin:-50px 0 0; /* negative fixed header height */
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Preamble: Note on writing reports, using reference material, '
|
||||
'AI and other tools',
|
||||
2,
|
||||
None,
|
||||
'preamble-note-on-writing-reports-using-reference-material-ai-and-other-tools'),
|
||||
('Regression analysis and resampling methods',
|
||||
2,
|
||||
None,
|
||||
'regression-analysis-and-resampling-methods'),
|
||||
('Part a : Ordinary Least Square (OLS) for the Runge function',
|
||||
3,
|
||||
None,
|
||||
'part-a-ordinary-least-square-ols-for-the-runge-function'),
|
||||
('Part b: Adding Ridge regression for the Runge function',
|
||||
3,
|
||||
None,
|
||||
'part-b-adding-ridge-regression-for-the-runge-function'),
|
||||
('Part c: Writing your own gradient descent code',
|
||||
3,
|
||||
None,
|
||||
'part-c-writing-your-own-gradient-descent-code'),
|
||||
('Part d: Including momentum and more advanced ways to update '
|
||||
'the learning the rate',
|
||||
3,
|
||||
None,
|
||||
'part-d-including-momentum-and-more-advanced-ways-to-update-the-learning-the-rate'),
|
||||
('Part e: Writing our own code for Lasso regression',
|
||||
3,
|
||||
None,
|
||||
'part-e-writing-our-own-code-for-lasso-regression'),
|
||||
('Part f: Stochastic gradient descent',
|
||||
3,
|
||||
None,
|
||||
'part-f-stochastic-gradient-descent'),
|
||||
('Part g: Bias-variance trade-off and resampling techniques',
|
||||
3,
|
||||
None,
|
||||
'part-g-bias-variance-trade-off-and-resampling-techniques'),
|
||||
('Part h): Cross-validation as resampling techniques, adding '
|
||||
'more complexity',
|
||||
3,
|
||||
None,
|
||||
'part-h-cross-validation-as-resampling-techniques-adding-more-complexity'),
|
||||
('Background literature', 2, None, 'background-literature'),
|
||||
('Introduction to numerical projects',
|
||||
2,
|
||||
None,
|
||||
'introduction-to-numerical-projects'),
|
||||
('Format for electronic delivery of report and programs',
|
||||
2,
|
||||
None,
|
||||
'format-for-electronic-delivery-of-report-and-programs'),
|
||||
('Software and needed installations',
|
||||
2,
|
||||
None,
|
||||
'software-and-needed-installations')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
|
||||
|
||||
|
||||
<script type="text/x-mathjax-config">
|
||||
MathJax.Hub.Config({
|
||||
TeX: {
|
||||
equationNumbers: { autoNumber: "none" },
|
||||
extensions: ["AMSmath.js", "AMSsymbols.js", "autobold.js", "color.js"]
|
||||
}
|
||||
});
|
||||
</script>
|
||||
<script type="text/javascript" async
|
||||
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
|
||||
</script>
|
||||
|
||||
|
||||
<!-- Bootstrap navigation bar -->
|
||||
<div class="navbar navbar-default navbar-fixed-top">
|
||||
<div class="navbar-header">
|
||||
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="Project1-bs.html">Project 1 on Machine Learning, deadline October 6 (midnight), 2025</a>
|
||||
</div>
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
<ul class="nav navbar-nav navbar-right">
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._Project1-bs000.html#preamble-note-on-writing-reports-using-reference-material-ai-and-other-tools" style="font-size: 80%;"><b>Preamble: Note on writing reports, using reference material, AI and other tools</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#regression-analysis-and-resampling-methods" style="font-size: 80%;"><b>Regression analysis and resampling methods</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#part-a-ordinary-least-square-ols-for-the-runge-function" style="font-size: 80%;"> Part a : Ordinary Least Square (OLS) for the Runge function</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#part-b-adding-ridge-regression-for-the-runge-function" style="font-size: 80%;"> Part b: Adding Ridge regression for the Runge function</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#part-c-writing-your-own-gradient-descent-code" style="font-size: 80%;"> Part c: Writing your own gradient descent code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#part-d-including-momentum-and-more-advanced-ways-to-update-the-learning-the-rate" style="font-size: 80%;"> Part d: Including momentum and more advanced ways to update the learning the rate</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#part-e-writing-our-own-code-for-lasso-regression" style="font-size: 80%;"> Part e: Writing our own code for Lasso regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#part-f-stochastic-gradient-descent" style="font-size: 80%;"> Part f: Stochastic gradient descent</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#part-g-bias-variance-trade-off-and-resampling-techniques" style="font-size: 80%;"> Part g: Bias-variance trade-off and resampling techniques</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#part-h-cross-validation-as-resampling-techniques-adding-more-complexity" style="font-size: 80%;"> Part h): Cross-validation as resampling techniques, adding more complexity</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#background-literature" style="font-size: 80%;"><b>Background literature</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#introduction-to-numerical-projects" style="font-size: 80%;"><b>Introduction to numerical projects</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#format-for-electronic-delivery-of-report-and-programs" style="font-size: 80%;"><b>Format for electronic delivery of report and programs</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="#software-and-needed-installations" style="font-size: 80%;"><b>Software and needed installations</b></a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div> <!-- end of navigation bar -->
|
||||
<div class="container">
|
||||
<p> </p><p> </p><p> </p> <!-- add vertical space -->
|
||||
<a name="part0001"></a>
|
||||
<!-- !split -->
|
||||
<h2 id="regression-analysis-and-resampling-methods" class="anchor">Regression analysis and resampling methods </h2>
|
||||
|
||||
<p>The main aim of this project is to study in more detail various
|
||||
regression methods, including Ordinary Least Squares (OLS) reegression, Ridge regression and LASSO regression.
|
||||
In addition to the scientific part, in this course we want also to
|
||||
give you an experience in writing scientific reports.
|
||||
</p>
|
||||
|
||||
<p>We will study how to fit polynomials to specific
|
||||
one-dimensional functions (feel free to replace the suggested function with more complicated ones).
|
||||
</p>
|
||||
|
||||
<p>We will use Runge's function (see <a href="https://en.wikipedia.org/wiki/Runge%27s_phenomenon" target="_self"><tt>https://en.wikipedia.org/wiki/Runge%27s_phenomenon</tt></a> for a discussion). The one-dimensional function we will study is</p>
|
||||
$$
|
||||
f(x) = \frac{1}{1+25x^2}.
|
||||
$$
|
||||
|
||||
<p>Our first step will be to perform an OLS regression analysis of this
|
||||
function, trying out a polynomial fit with an \( x \) dependence of the
|
||||
form \( [x,x^2,\dots] \). You can use a uniform distribution to set up the
|
||||
arrays of values for \( x \in [-1,1] \), or alternatively use a fixed step size.
|
||||
Thereafter we will repeat many of the same steps when using the Ridge and Lasso regression methods,
|
||||
introducing thereby a dependence on the hyperparameter (penalty) \( \lambda \).
|
||||
</p>
|
||||
|
||||
<p>We will also include bootstrap as a resampling technique in order to
|
||||
study the so-called <b>bias-variance tradeoff</b>. After that we will
|
||||
include the so-called cross-validation technique.
|
||||
</p>
|
||||
<h3 id="part-a-ordinary-least-square-ols-for-the-runge-function" class="anchor">Part a : Ordinary Least Square (OLS) for the Runge function </h3>
|
||||
|
||||
<p>We will generate our own dataset for abovementioned function
|
||||
\( \mathrm{Runge}(x) \) function with \( x\in [-1,1] \). You should explore also the addition
|
||||
of an added stochastic noise to this function using the normal
|
||||
distribution \( N(0,1) \).
|
||||
</p>
|
||||
|
||||
<p><em>Write your own code</em> (using for example the pseudoinverse function <b>pinv</b> from <b>Numpy</b> ) and perform a standard <b>ordinary least square regression</b>
|
||||
analysis using polynomials in \( x \) up to order \( 15 \) or higher. Explore the dependence on the number of data points and the polynomial degree.
|
||||
</p>
|
||||
|
||||
<p>Evaluate the mean Squared error (MSE)</p>
|
||||
|
||||
$$ MSE(\boldsymbol{y},\tilde{\boldsymbol{y}}) = \frac{1}{n}
|
||||
\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2,
|
||||
$$
|
||||
|
||||
<p>and the \( R^2 \) score function. If \( \tilde{\boldsymbol{y}}_i \) is the predicted
|
||||
value of the \( i-th \) sample and \( y_i \) is the corresponding true value,
|
||||
then the score \( R^2 \) is defined as
|
||||
</p>
|
||||
|
||||
$$
|
||||
R^2(\boldsymbol{y}, \tilde{\boldsymbol{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2},
|
||||
$$
|
||||
|
||||
<p>where we have defined the mean value of \( \boldsymbol{y} \) as</p>
|
||||
|
||||
$$
|
||||
\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i.
|
||||
$$
|
||||
|
||||
<p>Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree 15).
|
||||
Plot also the parameters \( \theta \) as you increase the order of the polynomial. Comment your results.
|
||||
</p>
|
||||
|
||||
<p>Your code has to include a scaling/centering of the data (for example by
|
||||
subtracting the mean value), and
|
||||
a split of the data in training and test data. For the scaling you can
|
||||
either write your own code or use for example the function for
|
||||
splitting training data provided by the library <b>Scikit-Learn</b> (make
|
||||
sure you have installed it). This function is called
|
||||
\( train\_test\_split \). <b>You should present a critical discussion of why and how you have scaled or not scaled the data</b>.
|
||||
</p>
|
||||
|
||||
<p>It is normal in essentially all Machine Learning studies to split the
|
||||
data in a training set and a test set (eventually also an additional
|
||||
validation set). There
|
||||
is no explicit recipe for how much data should be included as training
|
||||
data and say test data. An accepted rule of thumb is to use
|
||||
approximately \( 2/3 \) to \( 4/5 \) of the data as training data.
|
||||
</p>
|
||||
|
||||
<p>You can easily reuse the solutions to your exercises from week 35.
|
||||
See also the lecture slides from week 35 and week 36.
|
||||
</p>
|
||||
|
||||
<p>On scaling, we recommend reading the following section from the scikit-learn software description, see <a href="https://scikit-learn.org/stable/auto_examples/preprocessing/plot_all_scaling.html#plot-all-scaling-standard-scaler-section" target="_self"><tt>https://scikit-learn.org/stable/auto_examples/preprocessing/plot_all_scaling.html#plot-all-scaling-standard-scaler-section</tt></a>.</p>
|
||||
<h3 id="part-b-adding-ridge-regression-for-the-runge-function" class="anchor">Part b: Adding Ridge regression for the Runge function </h3>
|
||||
|
||||
<p>Write your own code for the Ridge method as done in the previous
|
||||
exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the results from the exercise set from week 36 is something you can reuse here.
|
||||
</p>
|
||||
|
||||
<p>Perform the same analysis as you did in the previous exercise but now for different values of \( \lambda \). Compare and
|
||||
analyze your results with those obtained in part a) with the OLS method. Study the
|
||||
dependence on \( \lambda \).
|
||||
</p>
|
||||
<h3 id="part-c-writing-your-own-gradient-descent-code" class="anchor">Part c: Writing your own gradient descent code </h3>
|
||||
|
||||
<p>Replace now the analytical expressions for the optimal parameters
|
||||
\( \boldsymbol{\theta} \) with your own gradient descent code. In this exercise we
|
||||
focus only on the simplest gradient descent approach with a fixed
|
||||
learning rate (see the exercises from week 37 and the lecture notes
|
||||
from week 36).
|
||||
</p>
|
||||
|
||||
<p>Study and compare your results from parts a) and b) with your gradient
|
||||
descent approch. Discuss in particular the role of the learning rate.
|
||||
</p>
|
||||
<h3 id="part-d-including-momentum-and-more-advanced-ways-to-update-the-learning-the-rate" class="anchor">Part d: Including momentum and more advanced ways to update the learning the rate </h3>
|
||||
|
||||
<p>We keep our focus on OLS and Ridge regression and update our code for
|
||||
the gradient descent method by including <b>momentum</b>, <b>ADAgrad</b>,
|
||||
<b>RMSprop</b> and <b>ADAM</b> as methods fro iteratively updating your learning
|
||||
rate. Discuss the results and compare the different methods applied to
|
||||
the one-dimensional Runge function. The lecture notes from week 37 contain several examples on how to implement these methods.
|
||||
</p>
|
||||
<h3 id="part-e-writing-our-own-code-for-lasso-regression" class="anchor">Part e: Writing our own code for Lasso regression </h3>
|
||||
|
||||
<p>LASSO regression (see lecture slides from week 36 and week 37)
|
||||
represents our first encounter with a machine learning method which
|
||||
cannot be solved through analytical expressions (as in OLS and Ridge regression). Use the gradient
|
||||
descent methods you developed in parts c) and d) to solve the LASSO
|
||||
optimization problem. You can compare your results with
|
||||
the functionalities of <b>Scikit-Learn</b>.
|
||||
</p>
|
||||
|
||||
<p>Discuss (critically) your results for the Runge function from OLS,
|
||||
Ridge and LASSO regression using the various gradient descent
|
||||
approaches.
|
||||
</p>
|
||||
<h3 id="part-f-stochastic-gradient-descent" class="anchor">Part f: Stochastic gradient descent </h3>
|
||||
|
||||
<p>Our last gradient step is to include stochastic gradient descent using
|
||||
the same methods to update the learning rates as in parts c-e).
|
||||
Compare and discuss your results with and without stochastic gradient
|
||||
and give a critical assessment of the various methods.
|
||||
</p>
|
||||
<h3 id="part-g-bias-variance-trade-off-and-resampling-techniques" class="anchor">Part g: Bias-variance trade-off and resampling techniques </h3>
|
||||
|
||||
<p>Our aim here is to study the bias-variance trade-off by implementing
|
||||
the <b>bootstrap</b> resampling technique. <b>We will only use the simpler
|
||||
ordinary least squares here</b>.
|
||||
</p>
|
||||
|
||||
<p>With a code which does OLS and includes resampling techniques,
|
||||
we will now discuss the bias-variance trade-off in the context of
|
||||
continuous predictions such as regression. However, many of the
|
||||
intuitions and ideas discussed here also carry over to classification
|
||||
tasks and basically all Machine Learning algorithms.
|
||||
</p>
|
||||
|
||||
<p>Before you perform an analysis of the bias-variance trade-off on your
|
||||
test data, make first a figure similar to Fig. 2.11 of Hastie,
|
||||
Tibshirani, and Friedman. Figure 2.11 of this reference displays only
|
||||
the test and training MSEs. The test MSE can be used to indicate
|
||||
possible regions of low/high bias and variance. You will most likely
|
||||
not get an equally smooth curve! You may also need to increase the
|
||||
polynomial order and play around with the number of data points as
|
||||
well (see also the exercise set from week 35).
|
||||
</p>
|
||||
|
||||
<p>With this result we move on to the bias-variance trade-off analysis.</p>
|
||||
|
||||
<p>Consider a
|
||||
dataset \( \mathcal{L} \) consisting of the data
|
||||
\( \mathbf{X}_\mathcal{L}=\{(y_j, \boldsymbol{x}_j), j=0\ldots n-1\} \).
|
||||
</p>
|
||||
|
||||
<p>We assume that the true data is generated from a noisy model</p>
|
||||
|
||||
$$
|
||||
\boldsymbol{y}=f(\boldsymbol{x}) + \boldsymbol{\epsilon}.
|
||||
$$
|
||||
|
||||
<p>Here \( \epsilon \) is normally distributed with mean zero and standard
|
||||
deviation \( \sigma^2 \).
|
||||
</p>
|
||||
|
||||
<p>In our derivation of the ordinary least squares method we defined then
|
||||
an approximation to the function \( f \) in terms of the parameters
|
||||
\( \boldsymbol{\theta} \) and the design matrix \( \boldsymbol{X} \) which embody our model,
|
||||
that is \( \boldsymbol{\tilde{y}}=\boldsymbol{X}\boldsymbol{\theta} \).
|
||||
</p>
|
||||
|
||||
<p>The parameters \( \boldsymbol{\theta} \) are in turn found by optimizing the mean
|
||||
squared error via the so-called cost function
|
||||
</p>
|
||||
|
||||
$$
|
||||
C(\boldsymbol{X},\boldsymbol{\theta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right].
|
||||
$$
|
||||
|
||||
<p>Here the expected value \( \mathbb{E} \) is the sample value. </p>
|
||||
|
||||
<p>Show that you can rewrite this in terms of a term which contains the
|
||||
variance of the model itself (the so-called variance term), a term
|
||||
which measures the deviation from the true data and the mean value of
|
||||
the model (the bias term) and finally the variance of the noise.
|
||||
</p>
|
||||
|
||||
<p>That is, show that</p>
|
||||
$$
|
||||
\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\mathrm{Bias}[\tilde{y}]+\mathrm{var}[\tilde{y}]+\sigma^2,
|
||||
$$
|
||||
|
||||
<p>with (we approximate \( f(\boldsymbol{x})\approx \boldsymbol{y} \)) </p>
|
||||
$$
|
||||
\mathrm{Bias}[\tilde{y}]=\mathbb{E}\left[\left(\boldsymbol{y}-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right]\right)^2\right],
|
||||
$$
|
||||
|
||||
<p>and </p>
|
||||
$$
|
||||
\mathrm{var}[\tilde{y}]=\mathbb{E}\left[\left(\tilde{\boldsymbol{y}}-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right]\right)^2\right]=\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2.
|
||||
$$
|
||||
|
||||
<p><b>Important note</b>: Since the function \( f(x) \) is unknown, in order to be able to evalute the bias, we replace \( f(\boldsymbol{x}) \) in the expression for the bias with \( \boldsymbol{y} \). </p>
|
||||
|
||||
<p>The answer to this exercise should be included in the theory part of
|
||||
the report. This exercise is also part of the weekly exercises of
|
||||
week 38. Explain what the terms mean and discuss their
|
||||
interpretations.
|
||||
</p>
|
||||
|
||||
<p>Perform then a bias-variance analysis of the Runge function by
|
||||
studying the MSE value as function of the complexity of your model.
|
||||
</p>
|
||||
|
||||
<p>Discuss the bias and variance trade-off as function
|
||||
of your model complexity (the degree of the polynomial) and the number
|
||||
of data points, and possibly also your training and test data using the <b>bootstrap</b> resampling method.
|
||||
You can follow the code example in the jupyter-book at <a href="https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/chapter3.html#the-bias-variance-tradeoff" target="_self"><tt>https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/chapter3.html#the-bias-variance-tradeoff</tt></a>.
|
||||
</p>
|
||||
<h3 id="part-h-cross-validation-as-resampling-techniques-adding-more-complexity" class="anchor">Part h): Cross-validation as resampling techniques, adding more complexity </h3>
|
||||
|
||||
<p>The aim here is to implement another widely popular
|
||||
resampling technique, the so-called cross-validation method.
|
||||
</p>
|
||||
|
||||
<p>Implement the \( k \)-fold cross-validation algorithm (feel free to use
|
||||
the functionality of <b>Scikit-Learn</b> or write your own code) and
|
||||
evaluate again the MSE function resulting from the test folds.
|
||||
</p>
|
||||
|
||||
<p>Compare the MSE you get from your cross-validation code with the one
|
||||
you got from your <b>bootstrap</b> code from the previous exercise. Comment and interpret your results.
|
||||
</p>
|
||||
|
||||
<p>In addition to using the ordinary least squares method, you should
|
||||
include both Ridge and Lasso regression in the final analysis.
|
||||
</p>
|
||||
<h2 id="background-literature" class="anchor">Background literature </h2>
|
||||
|
||||
<ol>
|
||||
<li> For a discussion and derivation of the variances and mean squared errors using linear regression, see the <a href="https://arxiv.org/abs/1509.09169" target="_self">Lecture notes on ridge regression by Wessel N. van Wieringen</a></li>
|
||||
<li> The textbook of <a href="https://www.springer.com/gp/book/9780387848570" target="_self">Trevor Hastie, Robert Tibshirani, Jerome H. Friedman, The Elements of Statistical Learning, Springer</a>, chapters 3 and 7 are the most relevant ones for the analysis here.</li>
|
||||
</ol>
|
||||
<h2 id="introduction-to-numerical-projects" class="anchor">Introduction to numerical projects </h2>
|
||||
|
||||
<p>Here follows a brief recipe and recommendation on how to answer the various questions when preparing your answers. </p>
|
||||
|
||||
<ul>
|
||||
<li> Give a short description of the nature of the problem and the eventual numerical methods you have used.</li>
|
||||
<li> Describe the algorithm you have used and/or developed. Here you may find it convenient to use pseudocoding. In many cases you can describe the algorithm in the program itself.</li>
|
||||
<li> Include the source code of your program. Comment your program properly. You should have the code at your GitHub/GitLab link. You can also place the code in an appendix of your report.</li>
|
||||
<li> If possible, try to find analytic solutions, or known limits in order to test your program when developing the code.</li>
|
||||
<li> Include your results either in figure form or in a table. Remember to label your results. All tables and figures should have relevant captions and labels on the axes.</li>
|
||||
<li> Try to evaluate the reliabilty and numerical stability/precision of your results. If possible, include a qualitative and/or quantitative discussion of the numerical stability, eventual loss of precision etc.</li>
|
||||
<li> Try to give an interpretation of you results in your answers to the problems.</li>
|
||||
<li> Critique: if possible include your comments and reflections about the exercise, whether you felt you learnt something, ideas for improvements and other thoughts you've made when solving the exercise. We wish to keep this course at the interactive level and your comments can help us improve it.</li>
|
||||
<li> Try to establish a practice where you log your work at the computerlab. You may find such a logbook very handy at later stages in your work, especially when you don't properly remember what a previous test version of your program did. Here you could also record the time spent on solving the exercise, various algorithms you may have tested or other topics which you feel worthy of mentioning.</li>
|
||||
</ul>
|
||||
<h2 id="format-for-electronic-delivery-of-report-and-programs" class="anchor">Format for electronic delivery of report and programs </h2>
|
||||
|
||||
<p>The preferred format for the report is a PDF file. You can also use DOC or postscript formats or as an ipython notebook file. As programming language we prefer that you choose between C/C++, Fortran2008, Julia or Python. The following prescription should be followed when preparing the report:</p>
|
||||
|
||||
<ul>
|
||||
<li> Use Canvas to hand in your projects, log in at <a href="https://www.uio.no/english/services/it/education/canvas/" target="_self"><tt>https://www.uio.no/english/services/it/education/canvas/</tt></a> with your normal UiO username and password.</li>
|
||||
<li> Upload <b>only</b> the report file or the link to your GitHub/GitLab or similar typo of repos! For the source code file(s) you have developed please provide us with your link to your GitHub/GitLab or similar domain. The report file should include all of your discussions and a list of the codes you have developed. Do not include library files which are available at the course homepage, unless you have made specific changes to them.</li>
|
||||
<li> In your GitHub/GitLab or similar repository, please include a folder which contains selected results. These can be in the form of output from your code for a selected set of runs and input parameters.</li>
|
||||
</ul>
|
||||
<p>Finally,
|
||||
we encourage you to collaborate. Optimal working groups consist of
|
||||
2-3 students. You can then hand in a common report.
|
||||
</p>
|
||||
<h2 id="software-and-needed-installations" class="anchor">Software and needed installations </h2>
|
||||
|
||||
<p>If you have Python installed (we recommend Python3) and you feel pretty familiar with installing different packages,
|
||||
we recommend that you install the following Python packages via <b>pip</b> as
|
||||
</p>
|
||||
<ol>
|
||||
<li> pip install numpy scipy matplotlib ipython scikit-learn tensorflow sympy pandas pillow</li>
|
||||
</ol>
|
||||
<p>For Python3, replace <b>pip</b> with <b>pip3</b>.</p>
|
||||
|
||||
<p>See below for a discussion of <b>tensorflow</b> and <b>scikit-learn</b>. </p>
|
||||
|
||||
<p>For OSX users we recommend also, after having installed Xcode, to install <b>brew</b>. Brew allows
|
||||
for a seamless installation of additional software via for example
|
||||
</p>
|
||||
<ol>
|
||||
<li> brew install python3</li>
|
||||
</ol>
|
||||
<p>For Linux users, with its variety of distributions like for example the widely popular Ubuntu distribution
|
||||
you can use <b>pip</b> as well and simply install Python as
|
||||
</p>
|
||||
<ol>
|
||||
<li> sudo apt-get install python3 (or python for python2.7)</li>
|
||||
</ol>
|
||||
<p>etc etc. </p>
|
||||
|
||||
<p>If you don't want to install various Python packages with their dependencies separately, we recommend two widely used distrubutions which set up all relevant dependencies for Python, namely</p>
|
||||
<ol>
|
||||
<li> <a href="https://docs.anaconda.com/" target="_self">Anaconda</a> Anaconda is an open source distribution of the Python and R programming languages for large-scale data processing, predictive analytics, and scientific computing, that aims to simplify package management and deployment. Package versions are managed by the package management system <b>conda</b></li>
|
||||
<li> <a href="https://www.enthought.com/product/canopy/" target="_self">Enthought canopy</a> is a Python distribution for scientific and analytic computing distribution and analysis environment, available for free and under a commercial license.</li>
|
||||
</ol>
|
||||
<p>Popular software packages written in Python for ML are</p>
|
||||
|
||||
<ul>
|
||||
<li> <a href="http://scikit-learn.org/stable/" target="_self">Scikit-learn</a>,</li>
|
||||
<li> <a href="https://www.tensorflow.org/" target="_self">Tensorflow</a>,</li>
|
||||
<li> <a href="http://pytorch.org/" target="_self">PyTorch</a> and</li>
|
||||
<li> <a href="https://keras.io/" target="_self">Keras</a>.</li>
|
||||
</ul>
|
||||
<p>These are all freely available at their respective GitHub sites. They
|
||||
encompass communities of developers in the thousands or more. And the number
|
||||
of code developers and contributors keeps increasing.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
<ul class="pagination">
|
||||
<li><a href="._Project1-bs000.html">«</a></li>
|
||||
<li><a href="._Project1-bs000.html">1</a></li>
|
||||
<li class="active"><a href="._Project1-bs001.html">2</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
</div> <!-- end container -->
|
||||
<!-- include javascript, jQuery *first* -->
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
|
||||
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
|
||||
<!-- Bootstrap footer
|
||||
<footer>
|
||||
<a href="https://..."><img width="250" align=right src="https://..."></a>
|
||||
</footer>
|
||||
-->
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -0,0 +1,238 @@
|
||||
<!--
|
||||
HTML file automatically generated from DocOnce source
|
||||
(https://github.com/doconce/doconce/)
|
||||
doconce format html Project1.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=Project1-bs
|
||||
-->
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/doconce/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Project 1 on Machine Learning, deadline October 6 (midnight), 2025">
|
||||
<title>Project 1 on Machine Learning, deadline October 6 (midnight), 2025</title>
|
||||
<!-- Bootstrap style: bootstrap -->
|
||||
<!-- doconce format html Project1.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=Project1-bs -->
|
||||
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
|
||||
<!-- not necessary
|
||||
<link href="https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
|
||||
-->
|
||||
<style type="text/css">
|
||||
/* Add scrollbar to dropdown menus in bootstrap navigation bar */
|
||||
.dropdown-menu {
|
||||
height: auto;
|
||||
max-height: 400px;
|
||||
overflow-x: hidden;
|
||||
}
|
||||
/* Adds an invisible element before each target to offset for the navigation
|
||||
bar */
|
||||
.anchor::before {
|
||||
content:"";
|
||||
display:block;
|
||||
height:50px; /* fixed header height for style bootstrap */
|
||||
margin:-50px 0 0; /* negative fixed header height */
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Preamble: Note on writing reports, using reference material, '
|
||||
'AI and other tools',
|
||||
2,
|
||||
None,
|
||||
'preamble-note-on-writing-reports-using-reference-material-ai-and-other-tools'),
|
||||
('Regression analysis and resampling methods',
|
||||
2,
|
||||
None,
|
||||
'regression-analysis-and-resampling-methods'),
|
||||
('Part a : Ordinary Least Square (OLS) for the Runge function',
|
||||
3,
|
||||
None,
|
||||
'part-a-ordinary-least-square-ols-for-the-runge-function'),
|
||||
('Part b: Adding Ridge regression for the Runge function',
|
||||
3,
|
||||
None,
|
||||
'part-b-adding-ridge-regression-for-the-runge-function'),
|
||||
('Part c: Writing your own gradient descent code',
|
||||
3,
|
||||
None,
|
||||
'part-c-writing-your-own-gradient-descent-code'),
|
||||
('Part d: Including momentum and more advanced ways to update '
|
||||
'the learning the rate',
|
||||
3,
|
||||
None,
|
||||
'part-d-including-momentum-and-more-advanced-ways-to-update-the-learning-the-rate'),
|
||||
('Part e: Writing our own code for Lasso regression',
|
||||
3,
|
||||
None,
|
||||
'part-e-writing-our-own-code-for-lasso-regression'),
|
||||
('Part f: Stochastic gradient descent',
|
||||
3,
|
||||
None,
|
||||
'part-f-stochastic-gradient-descent'),
|
||||
('Part g: Bias-variance trade-off and resampling techniques',
|
||||
3,
|
||||
None,
|
||||
'part-g-bias-variance-trade-off-and-resampling-techniques'),
|
||||
('Part h): Cross-validation as resampling techniques, adding '
|
||||
'more complexity',
|
||||
3,
|
||||
None,
|
||||
'part-h-cross-validation-as-resampling-techniques-adding-more-complexity'),
|
||||
('Background literature', 2, None, 'background-literature'),
|
||||
('Introduction to numerical projects',
|
||||
2,
|
||||
None,
|
||||
'introduction-to-numerical-projects'),
|
||||
('Format for electronic delivery of report and programs',
|
||||
2,
|
||||
None,
|
||||
'format-for-electronic-delivery-of-report-and-programs'),
|
||||
('Software and needed installations',
|
||||
2,
|
||||
None,
|
||||
'software-and-needed-installations')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
|
||||
|
||||
|
||||
<script type="text/x-mathjax-config">
|
||||
MathJax.Hub.Config({
|
||||
TeX: {
|
||||
equationNumbers: { autoNumber: "none" },
|
||||
extensions: ["AMSmath.js", "AMSsymbols.js", "autobold.js", "color.js"]
|
||||
}
|
||||
});
|
||||
</script>
|
||||
<script type="text/javascript" async
|
||||
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
|
||||
</script>
|
||||
|
||||
|
||||
<!-- Bootstrap navigation bar -->
|
||||
<div class="navbar navbar-default navbar-fixed-top">
|
||||
<div class="navbar-header">
|
||||
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="Project1-bs.html">Project 1 on Machine Learning, deadline October 6 (midnight), 2025</a>
|
||||
</div>
|
||||
<div class="navbar-collapse collapse navbar-responsive-collapse">
|
||||
<ul class="nav navbar-nav navbar-right">
|
||||
<li class="dropdown">
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="#preamble-note-on-writing-reports-using-reference-material-ai-and-other-tools" style="font-size: 80%;"><b>Preamble: Note on writing reports, using reference material, AI and other tools</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._Project1-bs001.html#regression-analysis-and-resampling-methods" style="font-size: 80%;"><b>Regression analysis and resampling methods</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._Project1-bs001.html#part-a-ordinary-least-square-ols-for-the-runge-function" style="font-size: 80%;"> Part a : Ordinary Least Square (OLS) for the Runge function</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._Project1-bs001.html#part-b-adding-ridge-regression-for-the-runge-function" style="font-size: 80%;"> Part b: Adding Ridge regression for the Runge function</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._Project1-bs001.html#part-c-writing-your-own-gradient-descent-code" style="font-size: 80%;"> Part c: Writing your own gradient descent code</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._Project1-bs001.html#part-d-including-momentum-and-more-advanced-ways-to-update-the-learning-the-rate" style="font-size: 80%;"> Part d: Including momentum and more advanced ways to update the learning the rate</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._Project1-bs001.html#part-e-writing-our-own-code-for-lasso-regression" style="font-size: 80%;"> Part e: Writing our own code for Lasso regression</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._Project1-bs001.html#part-f-stochastic-gradient-descent" style="font-size: 80%;"> Part f: Stochastic gradient descent</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._Project1-bs001.html#part-g-bias-variance-trade-off-and-resampling-techniques" style="font-size: 80%;"> Part g: Bias-variance trade-off and resampling techniques</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._Project1-bs001.html#part-h-cross-validation-as-resampling-techniques-adding-more-complexity" style="font-size: 80%;"> Part h): Cross-validation as resampling techniques, adding more complexity</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._Project1-bs001.html#background-literature" style="font-size: 80%;"><b>Background literature</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._Project1-bs001.html#introduction-to-numerical-projects" style="font-size: 80%;"><b>Introduction to numerical projects</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._Project1-bs001.html#format-for-electronic-delivery-of-report-and-programs" style="font-size: 80%;"><b>Format for electronic delivery of report and programs</b></a></li>
|
||||
<!-- navigation toc: --> <li><a href="._Project1-bs001.html#software-and-needed-installations" style="font-size: 80%;"><b>Software and needed installations</b></a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div> <!-- end of navigation bar -->
|
||||
<div class="container">
|
||||
<p> </p><p> </p><p> </p> <!-- add vertical space -->
|
||||
<a name="part0000"></a>
|
||||
<!-- ------------------- main content ---------------------- -->
|
||||
<div class="jumbotron">
|
||||
<center>
|
||||
<h1>Project 1 on Machine Learning, deadline October 6 (midnight), 2025</h1>
|
||||
</center> <!-- document title -->
|
||||
|
||||
<!-- author(s): Data Analysis and Machine Learning FYS-STK3155/FYS4155 -->
|
||||
<center>
|
||||
<b>Data Analysis and Machine Learning FYS-STK3155/FYS4155</b>
|
||||
</center>
|
||||
<!-- institution -->
|
||||
<center>
|
||||
<b>University of Oslo, Norway</b>
|
||||
</center>
|
||||
<br>
|
||||
<center>
|
||||
<h4>September 2</h4>
|
||||
</center> <!-- date -->
|
||||
<br>
|
||||
|
||||
|
||||
</div> <!-- end jumbotron -->
|
||||
<h2 id="preamble-note-on-writing-reports-using-reference-material-ai-and-other-tools" class="anchor">Preamble: Note on writing reports, using reference material, AI and other tools </h2>
|
||||
|
||||
<p>We want you to answer the three different projects by handing in
|
||||
reports written like a standard scientific/technical report. The
|
||||
links at
|
||||
<a href="https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects" target="_self"><tt>https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects</tt></a>
|
||||
contain more information. There you can find examples of previous
|
||||
reports, the projects themselves, how we rade reports etc. How to
|
||||
write reports will also be discussed during the various lab
|
||||
sessions. Please do ask us if you are in doubt.
|
||||
</p>
|
||||
|
||||
<p>When using codes and material from other sources, you should refer to
|
||||
these in the bibliography of your report, indicating wherefrom you for
|
||||
example got the code, whether this is from the lecture notes,
|
||||
softwares like Scikit-Learn, TensorFlow, PyTorch or other sources. These sources
|
||||
should always be cited correctly. How to cite some
|
||||
of the libraries is often indicated from their corresponding GitHub
|
||||
sites or websites, see for example how to cite Scikit-Learn at
|
||||
<a href="https://scikit-learn.org/dev/about.html" target="_self"><tt>https://scikit-learn.org/dev/about.html</tt></a>.
|
||||
</p>
|
||||
|
||||
<p>We enocurage you to use tools like
|
||||
<a href="https://openai.com/chatgpt/" target="_self">ChatGPT</a> or similar in writing the report. If you use for example ChatGPT,
|
||||
please do cite it properly and include (if possible) your questions and answers as an addition to the report. This can
|
||||
be uploaded to for example your website, GitHub/GitLab or similar as supplemental material.
|
||||
</p>
|
||||
|
||||
<p>If you would like to study other data sets, feel free to propose other
|
||||
sets. What we have proposed here are mere suggestions from our
|
||||
side. If you opt for another data set, consider using a set which has
|
||||
been studied in the scientific literature. This makes it easier for
|
||||
you to compare and analyze your results. Comparing with existing
|
||||
results from the scientific literature is also an essential element of
|
||||
the scientific discussion. The University of California at Irvine
|
||||
with its Machine Learning repository at
|
||||
<a href="https://archive.ics.uci.edu/ml/index.php" target="_self"><tt>https://archive.ics.uci.edu/ml/index.php</tt></a> is an excellent site to
|
||||
look up for examples and
|
||||
inspiration. <a href="https://www.kaggle.com/" target="_self">Kaggle.com</a> is an equally
|
||||
interesting site. Feel free to explore these sites. When selecting
|
||||
other data sets, make sure these are sets used for regression problems
|
||||
(not classification).
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
<ul class="pagination">
|
||||
<li class="active"><a href="._Project1-bs000.html">1</a></li>
|
||||
<li><a href="._Project1-bs001.html">2</a></li>
|
||||
<li><a href="._Project1-bs001.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
</div> <!-- end container -->
|
||||
<!-- include javascript, jQuery *first* -->
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
|
||||
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
|
||||
<!-- Bootstrap footer
|
||||
<footer>
|
||||
<a href="https://..."><img width="250" align=right src="https://..."></a>
|
||||
</footer>
|
||||
-->
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -6,19 +6,21 @@ DATE: September 2
|
||||
===== Preamble: Note on writing reports, using reference material, AI and other tools =====
|
||||
|
||||
We want you to answer the three different projects by handing in
|
||||
reports written like a standard scientific/technical report. The links
|
||||
at URL:"https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects"
|
||||
Furthermore, at the same link,
|
||||
you can find examples of previous reports. How to write reports will
|
||||
also be discussed during the various lab sessions. Please do ask us if you are in doubt.
|
||||
reports written like a standard scientific/technical report. The
|
||||
links at
|
||||
URL:"https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects"
|
||||
contain more information. There you can find examples of previous
|
||||
reports, the projects themselves, how we rade reports etc. How to
|
||||
write reports will also be discussed during the various lab
|
||||
sessions. Please do ask us if you are in doubt.
|
||||
|
||||
|
||||
|
||||
When using codes and material from other sources, you should refer to
|
||||
these in the bibliography of your report, indicating wherefrom you for
|
||||
example got the code, whether this is from the lecture notes,
|
||||
softwares like Scikit-Learn, TensorFlow, PyTorch or other sources such
|
||||
AI software. These should always be cited correctly. How to cite some
|
||||
softwares like Scikit-Learn, TensorFlow, PyTorch or other sources. These sources
|
||||
should always be cited correctly. How to cite some
|
||||
of the libraries is often indicated from their corresponding GitHub
|
||||
sites or websites, see for example how to cite Scikit-Learn at
|
||||
URL:"https://scikit-learn.org/dev/about.html".
|
||||
@@ -52,7 +54,7 @@ other data sets, make sure these are sets used for regression problems
|
||||
|
||||
|
||||
The main aim of this project is to study in more detail various
|
||||
regression methods, including the Ordinary Least Squares (OLS) method.
|
||||
regression methods, including Ordinary Least Squares (OLS) reegression, Ridge regression and LASSO regression.
|
||||
In addition to the scientific part, in this course we want also to
|
||||
give you an experience in writing scientific reports.
|
||||
|
||||
@@ -71,26 +73,25 @@ f(x) = \frac{1}{1+25x^2}.
|
||||
|
||||
Our first step will be to perform an OLS regression analysis of this
|
||||
function, trying out a polynomial fit with an $x$ dependence of the
|
||||
form $[x,x^2,\dots]$. We can use a uniform distribution to set up the
|
||||
form $[x,x^2,\dots]$. You can use a uniform distribution to set up the
|
||||
arrays of values for $x \in [-1,1]$, or alternatively use a fixed step size.
|
||||
Thereafter we will repeat much of the
|
||||
same procedure using the Ridge and Lasso regression methods,
|
||||
introducing thus a dependence on the hyperparameter (penalty) $\lambda$.
|
||||
Thereafter we will repeat many of the same steps when using the Ridge and Lasso regression methods,
|
||||
introducing thereby a dependence on the hyperparameter (penalty) $\lambda$.
|
||||
|
||||
We will also include bootstrap as a resampling technique in order to
|
||||
study the so-called _bias-variance tradeoff_. After that we will
|
||||
include the cross-validation technique.
|
||||
include the so-called cross-validation technique.
|
||||
|
||||
|
||||
=== Part a : Ordinary Least Square (OLS) for the Runge function ===
|
||||
|
||||
We will generate our own dataset for a function
|
||||
We will generate our own dataset for abovementioned function
|
||||
$\mathrm{Runge}(x)$ function with $x\in [-1,1]$. You should explore also the addition
|
||||
of an added stochastic noise to this function using the normal
|
||||
distribution $N(0,1)$.
|
||||
|
||||
*Write your own code* (using for example the pseudoinverse function _pinv_ from _Numpy_ ) and perform a standard _ordinary least square regression_
|
||||
analysis using polynomials in $x$ up to order $15$. Explore the dependence on the number of data points and the polynomial degree.
|
||||
analysis using polynomials in $x$ up to order $15$ or higher. Explore the dependence on the number of data points and the polynomial degree.
|
||||
|
||||
Evaluate the mean Squared error (MSE)
|
||||
|
||||
@@ -119,12 +120,12 @@ where we have defined the mean value of $\bm{y}$ as
|
||||
\]
|
||||
!et
|
||||
|
||||
Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree 20).
|
||||
Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree 15).
|
||||
Plot also the parameters $\theta$ as you increase the order of the polynomial. Comment your results.
|
||||
|
||||
Your code has to include a scaling/centering of the data (for example by
|
||||
subtracting the mean value), and
|
||||
a split of the data in training and test data. For this exercise you can
|
||||
a split of the data in training and test data. For the scaling you can
|
||||
either write your own code or use for example the function for
|
||||
splitting training data provided by the library _Scikit-Learn_ (make
|
||||
sure you have installed it). This function is called
|
||||
@@ -149,10 +150,10 @@ On scaling, we recommend reading the following section from the scikit-learn sof
|
||||
=== Part b: Adding Ridge regression for the Runge function ===
|
||||
|
||||
Write your own code for the Ridge method as done in the previous
|
||||
exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the exercise from week 36 is something you can reuse here.
|
||||
exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the results from the exercise set from week 36 is something you can reuse here.
|
||||
|
||||
Perform the same analysis as you did in the previous exercise but now for different values of $\lambda$. Compare and
|
||||
analyze your results with those obtained in part a) with the ordinary least squares method. Study the
|
||||
analyze your results with those obtained in part a) with the OLS method. Study the
|
||||
dependence on $\lambda$.
|
||||
|
||||
|
||||
@@ -174,16 +175,16 @@ We keep our focus on OLS and Ridge regression and update our code for
|
||||
the gradient descent method by including _momentum_, _ADAgrad_,
|
||||
_RMSprop_ and _ADAM_ as methods fro iteratively updating your learning
|
||||
rate. Discuss the results and compare the different methods applied to
|
||||
the one-dimensional Runge function.
|
||||
the one-dimensional Runge function. The lecture notes from week 37 contain several examples on how to implement these methods.
|
||||
|
||||
|
||||
=== Part e: Writing our own code for Lasso regression ===
|
||||
|
||||
LASSO regression (see lecture slides from week 36 and week 37)
|
||||
represents our first encounter with a machine learning method which
|
||||
cannot be solved through analytical expressions. Use the gradient
|
||||
cannot be solved through analytical expressions (as in OLS and Ridge regression). Use the gradient
|
||||
descent methods you developed in parts c) and d) to solve the LASSO
|
||||
optimization problem. You can compare your results using
|
||||
optimization problem. You can compare your results with
|
||||
the functionalities of _Scikit-Learn_.
|
||||
|
||||
Discuss (critically) your results for the Runge function from OLS,
|
||||
@@ -192,14 +193,16 @@ approaches.
|
||||
|
||||
=== Part f: Stochastic gradient descent ===
|
||||
|
||||
Our last gradient step is to include stochastic gradient descent using the
|
||||
same methods to update the learning rates as in parts c-e).
|
||||
Compare and discuss your results with and without stochastic gradient and give a critical assessment of the various methods.
|
||||
Our last gradient step is to include stochastic gradient descent using
|
||||
the same methods to update the learning rates as in parts c-e).
|
||||
Compare and discuss your results with and without stochastic gradient
|
||||
and give a critical assessment of the various methods.
|
||||
|
||||
=== Part g: Bias-variance trade-off and resampling techniques ===
|
||||
|
||||
Our aim here is to study the bias-variance trade-off by implementing the _bootstrap_ resampling technique.
|
||||
_We will only use the simpler ordinary least squares here_.
|
||||
Our aim here is to study the bias-variance trade-off by implementing
|
||||
the _bootstrap_ resampling technique. _We will only use the simpler
|
||||
ordinary least squares here_.
|
||||
|
||||
|
||||
With a code which does OLS and includes resampling techniques,
|
||||
@@ -208,11 +211,14 @@ continuous predictions such as regression. However, many of the
|
||||
intuitions and ideas discussed here also carry over to classification
|
||||
tasks and basically all Machine Learning algorithms.
|
||||
|
||||
Before you perform an analysis of the bias-variance trade-off on your test data, make
|
||||
first a figure similar to Fig. 2.11 of Hastie, Tibshirani, and
|
||||
Friedman. Figure 2.11 of this reference displays only the test and training MSEs. The test MSE can be used to
|
||||
indicate possible regions of low/high bias and variance. You will most likely not get an
|
||||
equally smooth curve!
|
||||
Before you perform an analysis of the bias-variance trade-off on your
|
||||
test data, make first a figure similar to Fig. 2.11 of Hastie,
|
||||
Tibshirani, and Friedman. Figure 2.11 of this reference displays only
|
||||
the test and training MSEs. The test MSE can be used to indicate
|
||||
possible regions of low/high bias and variance. You will most likely
|
||||
not get an equally smooth curve! You may also need to increase the
|
||||
polynomial order and play around with the number of data points as
|
||||
well (see also the exercise set from week 35).
|
||||
|
||||
With this result we move on to the bias-variance trade-off analysis.
|
||||
|
||||
@@ -220,7 +226,7 @@ Consider a
|
||||
dataset $\mathcal{L}$ consisting of the data
|
||||
$\mathbf{X}_\mathcal{L}=\{(y_j, \boldsymbol{x}_j), j=0\ldots n-1\}$.
|
||||
|
||||
As in part d), we assume that the true data is generated from a noisy model
|
||||
We assume that the true data is generated from a noisy model
|
||||
|
||||
!bt
|
||||
\[
|
||||
@@ -233,28 +239,32 @@ deviation $\sigma^2$.
|
||||
|
||||
In our derivation of the ordinary least squares method we defined then
|
||||
an approximation to the function $f$ in terms of the parameters
|
||||
$\bm{\beta}$ and the design matrix $\bm{X}$ which embody our model,
|
||||
that is $\bm{\tilde{y}}=\bm{X}\bm{\beta}$.
|
||||
$\bm{\theta}$ and the design matrix $\bm{X}$ which embody our model,
|
||||
that is $\bm{\tilde{y}}=\bm{X}\bm{\theta}$.
|
||||
|
||||
The parameters $\bm{\beta}$ are in turn found by optimizing the mean
|
||||
The parameters $\bm{\theta}$ are in turn found by optimizing the mean
|
||||
squared error via the so-called cost function
|
||||
|
||||
!bt
|
||||
\[
|
||||
C(\bm{X},\bm{\beta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\bm{y}-\bm{\tilde{y}})^2\right].
|
||||
C(\bm{X},\bm{\theta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\bm{y}-\bm{\tilde{y}})^2\right].
|
||||
\]
|
||||
!et
|
||||
Here the expected value $\mathbb{E}$ is the sample value.
|
||||
|
||||
Show that you can rewrite this in terms of a term which contains the variance of the model itself (the so-called variance term), a
|
||||
term which measures the deviation from the true data and the mean value of the model (the bias term) and finally the variance of the noise.
|
||||
Show that you can rewrite this in terms of a term which contains the
|
||||
variance of the model itself (the so-called variance term), a term
|
||||
which measures the deviation from the true data and the mean value of
|
||||
the model (the bias term) and finally the variance of the noise.
|
||||
|
||||
|
||||
That is, show that
|
||||
!bt
|
||||
\[
|
||||
\mathbb{E}\left[(\bm{y}-\bm{\tilde{y}})^2\right]=\mathrm{Bias}[\tilde{y}]+\mathrm{var}[\tilde{y}]+\sigma^2,
|
||||
\]
|
||||
!et
|
||||
with
|
||||
with (we approximate $f(\bm{x})\approx \bm{y}$)
|
||||
!bt
|
||||
\[
|
||||
\mathrm{Bias}[\tilde{y}]=\mathbb{E}\left[\left(\bm{y}-\mathbb{E}\left[\bm{\tilde{y}}\right]\right)^2\right],
|
||||
@@ -266,8 +276,14 @@ and
|
||||
\mathrm{var}[\tilde{y}]=\mathbb{E}\left[\left(\tilde{\bm{y}}-\mathbb{E}\left[\bm{\tilde{y}}\right]\right)^2\right]=\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\bm{\tilde{y}}\right])^2.
|
||||
\]
|
||||
!et
|
||||
The answer to this exercise should be included in the theory part of the report. This exercise is also part of the weekly exercises of week 38.
|
||||
Explain what the terms mean and discuss their interpretations.
|
||||
|
||||
|
||||
_Important note_: Since the function $f(x)$ is unknown, in order to be able to evalute the bias, we replace $f(\bm{x})$ in the expression for the bias with $\bm{y}$.
|
||||
|
||||
The answer to this exercise should be included in the theory part of
|
||||
the report. This exercise is also part of the weekly exercises of
|
||||
week 38. Explain what the terms mean and discuss their
|
||||
interpretations.
|
||||
|
||||
Perform then a bias-variance analysis of the Runge function by
|
||||
studying the MSE value as function of the complexity of your model.
|
||||
@@ -285,14 +301,17 @@ You can follow the code example in the jupyter-book at URL:"https://compphysics.
|
||||
The aim here is to implement another widely popular
|
||||
resampling technique, the so-called cross-validation method.
|
||||
|
||||
Implement the $k$-fold cross-validation algorithm (feel free to use the functionality of _Scikit-Learn_ or write your own code) and evaluate again the MSE function resulting
|
||||
from the test folds.
|
||||
Implement the $k$-fold cross-validation algorithm (feel free to use
|
||||
the functionality of _Scikit-Learn_ or write your own code) and
|
||||
evaluate again the MSE function resulting from the test folds.
|
||||
|
||||
|
||||
|
||||
Compare the MSE you get from your cross-validation code with the one
|
||||
you got from your _bootstrap_ code. Comment your results. Try $5-10$
|
||||
folds.
|
||||
you got from your _bootstrap_ code from the previous exercise. Comment and interpret your results.
|
||||
|
||||
In addition to using the ordinary least squares method, you should include both Ridge and Lasso regression in the analysis.
|
||||
In addition to using the ordinary least squares method, you should
|
||||
include both Ridge and Lasso regression in the final analysis.
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,588 @@
|
||||
<!--
|
||||
HTML file automatically generated from DocOnce source
|
||||
(https://github.com/doconce/doconce/)
|
||||
doconce format html Project1.do.txt --pygments_html_style=default --html_style=bloodish --html_links_in_new_window --html_output=Project1
|
||||
-->
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="generator" content="DocOnce: https://github.com/doconce/doconce/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="Project 1 on Machine Learning, deadline October 6 (midnight), 2025">
|
||||
<title>Project 1 on Machine Learning, deadline October 6 (midnight), 2025</title>
|
||||
<style type="text/css">
|
||||
/* bloodish style */
|
||||
body {
|
||||
font-family: Helvetica, Verdana, Arial, Sans-serif;
|
||||
color: #404040;
|
||||
background: #ffffff;
|
||||
}
|
||||
h1 { font-size: 1.8em; color: #8A0808; }
|
||||
h2 { font-size: 1.6em; color: #8A0808; }
|
||||
h3 { font-size: 1.4em; color: #8A0808; }
|
||||
h4 { font-size: 1.2em; color: #8A0808; }
|
||||
a { color: #8A0808; text-decoration:none; }
|
||||
tt { font-family: "Courier New", Courier; }
|
||||
p { text-indent: 0px; }
|
||||
hr { border: 0; width: 80%; border-bottom: 1px solid #aaa}
|
||||
p.caption { width: 80%; font-style: normal; text-align: left; }
|
||||
hr.figure { border: 0; width: 80%; border-bottom: 1px solid #aaa; }div.highlight {
|
||||
border: 1px solid #cfcfcf;
|
||||
border-radius: 2px;
|
||||
line-height: 1.21429em;
|
||||
}
|
||||
div.cell {
|
||||
width: 100%;
|
||||
padding: 5px 5px 5px 0;
|
||||
margin: 0;
|
||||
outline: none;
|
||||
}
|
||||
div.input {
|
||||
page-break-inside: avoid;
|
||||
box-orient: horizontal;
|
||||
box-align: stretch;
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
align-items: stretch;
|
||||
}
|
||||
div.inner_cell {
|
||||
box-orient: vertical;
|
||||
box-align: stretch;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: stretch;
|
||||
box-flex: 1;
|
||||
flex: 1;
|
||||
}
|
||||
div.input_area {
|
||||
border: 1px solid #cfcfcf;
|
||||
border-radius: 4px;
|
||||
background: #f7f7f7;
|
||||
line-height: 1.21429em;
|
||||
}
|
||||
div.input_area > div.highlight {
|
||||
margin: .4em;
|
||||
border: none;
|
||||
padding: 0;
|
||||
background-color: transparent;
|
||||
}
|
||||
div.output_wrapper {
|
||||
position: relative;
|
||||
box-orient: vertical;
|
||||
box-align: stretch;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: stretch;
|
||||
}
|
||||
.output {
|
||||
box-orient: vertical;
|
||||
box-align: stretch;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: stretch;
|
||||
}
|
||||
div.output_area {
|
||||
padding: 0;
|
||||
page-break-inside: avoid;
|
||||
box-orient: horizontal;
|
||||
box-align: stretch;
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
align-items: stretch;
|
||||
}
|
||||
div.output_subarea {
|
||||
padding: .4em .4em 0 .4em;
|
||||
box-flex: 1;
|
||||
flex: 1;
|
||||
}
|
||||
div.output_text {
|
||||
text-align: left;
|
||||
color: #000;
|
||||
line-height: 1.21429em;
|
||||
}
|
||||
div { text-align: justify; text-justify: inter-word; }
|
||||
.tab {
|
||||
padding-left: 1.5em;
|
||||
}
|
||||
div.toc p,a {
|
||||
line-height: 1.3;
|
||||
margin-top: 1.1;
|
||||
margin-bottom: 1.1;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Preamble: Note on writing reports, using reference material, '
|
||||
'AI and other tools',
|
||||
2,
|
||||
None,
|
||||
'preamble-note-on-writing-reports-using-reference-material-ai-and-other-tools'),
|
||||
('Regression analysis and resampling methods',
|
||||
2,
|
||||
None,
|
||||
'regression-analysis-and-resampling-methods'),
|
||||
('Part a : Ordinary Least Square (OLS) for the Runge function',
|
||||
3,
|
||||
None,
|
||||
'part-a-ordinary-least-square-ols-for-the-runge-function'),
|
||||
('Part b: Adding Ridge regression for the Runge function',
|
||||
3,
|
||||
None,
|
||||
'part-b-adding-ridge-regression-for-the-runge-function'),
|
||||
('Part c: Writing your own gradient descent code',
|
||||
3,
|
||||
None,
|
||||
'part-c-writing-your-own-gradient-descent-code'),
|
||||
('Part d: Including momentum and more advanced ways to update '
|
||||
'the learning the rate',
|
||||
3,
|
||||
None,
|
||||
'part-d-including-momentum-and-more-advanced-ways-to-update-the-learning-the-rate'),
|
||||
('Part e: Writing our own code for Lasso regression',
|
||||
3,
|
||||
None,
|
||||
'part-e-writing-our-own-code-for-lasso-regression'),
|
||||
('Part f: Stochastic gradient descent',
|
||||
3,
|
||||
None,
|
||||
'part-f-stochastic-gradient-descent'),
|
||||
('Part g: Bias-variance trade-off and resampling techniques',
|
||||
3,
|
||||
None,
|
||||
'part-g-bias-variance-trade-off-and-resampling-techniques'),
|
||||
('Part h): Cross-validation as resampling techniques, adding '
|
||||
'more complexity',
|
||||
3,
|
||||
None,
|
||||
'part-h-cross-validation-as-resampling-techniques-adding-more-complexity'),
|
||||
('Background literature', 2, None, 'background-literature'),
|
||||
('Introduction to numerical projects',
|
||||
2,
|
||||
None,
|
||||
'introduction-to-numerical-projects'),
|
||||
('Format for electronic delivery of report and programs',
|
||||
2,
|
||||
None,
|
||||
'format-for-electronic-delivery-of-report-and-programs'),
|
||||
('Software and needed installations',
|
||||
2,
|
||||
None,
|
||||
'software-and-needed-installations')]}
|
||||
end of tocinfo -->
|
||||
|
||||
<body>
|
||||
|
||||
|
||||
|
||||
<script type="text/x-mathjax-config">
|
||||
MathJax.Hub.Config({
|
||||
TeX: {
|
||||
equationNumbers: { autoNumber: "AMS" },
|
||||
extensions: ["AMSmath.js", "AMSsymbols.js", "autobold.js", "color.js"]
|
||||
}
|
||||
});
|
||||
</script>
|
||||
<script type="text/javascript" async
|
||||
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
|
||||
</script>
|
||||
|
||||
|
||||
<!-- ------------------- main content ---------------------- -->
|
||||
<center>
|
||||
<h1>Project 1 on Machine Learning, deadline October 6 (midnight), 2025</h1>
|
||||
</center> <!-- document title -->
|
||||
|
||||
<!-- author(s): Data Analysis and Machine Learning FYS-STK3155/FYS4155 -->
|
||||
<center>
|
||||
<b>Data Analysis and Machine Learning FYS-STK3155/FYS4155</b>
|
||||
</center>
|
||||
<!-- institution -->
|
||||
<center>
|
||||
<b>University of Oslo, Norway</b>
|
||||
</center>
|
||||
<br>
|
||||
<center>
|
||||
<h4>September 2</h4>
|
||||
</center> <!-- date -->
|
||||
<br>
|
||||
<h2 id="preamble-note-on-writing-reports-using-reference-material-ai-and-other-tools">Preamble: Note on writing reports, using reference material, AI and other tools </h2>
|
||||
|
||||
<p>We want you to answer the three different projects by handing in
|
||||
reports written like a standard scientific/technical report. The
|
||||
links at
|
||||
<a href="https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects" target="_blank"><tt>https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects</tt></a>
|
||||
contain more information. There you can find examples of previous
|
||||
reports, the projects themselves, how we rade reports etc. How to
|
||||
write reports will also be discussed during the various lab
|
||||
sessions. Please do ask us if you are in doubt.
|
||||
</p>
|
||||
|
||||
<p>When using codes and material from other sources, you should refer to
|
||||
these in the bibliography of your report, indicating wherefrom you for
|
||||
example got the code, whether this is from the lecture notes,
|
||||
softwares like Scikit-Learn, TensorFlow, PyTorch or other sources. These sources
|
||||
should always be cited correctly. How to cite some
|
||||
of the libraries is often indicated from their corresponding GitHub
|
||||
sites or websites, see for example how to cite Scikit-Learn at
|
||||
<a href="https://scikit-learn.org/dev/about.html" target="_blank"><tt>https://scikit-learn.org/dev/about.html</tt></a>.
|
||||
</p>
|
||||
|
||||
<p>We enocurage you to use tools like
|
||||
<a href="https://openai.com/chatgpt/" target="_blank">ChatGPT</a> or similar in writing the report. If you use for example ChatGPT,
|
||||
please do cite it properly and include (if possible) your questions and answers as an addition to the report. This can
|
||||
be uploaded to for example your website, GitHub/GitLab or similar as supplemental material.
|
||||
</p>
|
||||
|
||||
<p>If you would like to study other data sets, feel free to propose other
|
||||
sets. What we have proposed here are mere suggestions from our
|
||||
side. If you opt for another data set, consider using a set which has
|
||||
been studied in the scientific literature. This makes it easier for
|
||||
you to compare and analyze your results. Comparing with existing
|
||||
results from the scientific literature is also an essential element of
|
||||
the scientific discussion. The University of California at Irvine
|
||||
with its Machine Learning repository at
|
||||
<a href="https://archive.ics.uci.edu/ml/index.php" target="_blank"><tt>https://archive.ics.uci.edu/ml/index.php</tt></a> is an excellent site to
|
||||
look up for examples and
|
||||
inspiration. <a href="https://www.kaggle.com/" target="_blank">Kaggle.com</a> is an equally
|
||||
interesting site. Feel free to explore these sites. When selecting
|
||||
other data sets, make sure these are sets used for regression problems
|
||||
(not classification).
|
||||
</p>
|
||||
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
<h2 id="regression-analysis-and-resampling-methods">Regression analysis and resampling methods </h2>
|
||||
|
||||
<p>The main aim of this project is to study in more detail various
|
||||
regression methods, including Ordinary Least Squares (OLS) reegression, Ridge regression and LASSO regression.
|
||||
In addition to the scientific part, in this course we want also to
|
||||
give you an experience in writing scientific reports.
|
||||
</p>
|
||||
|
||||
<p>We will study how to fit polynomials to specific
|
||||
one-dimensional functions (feel free to replace the suggested function with more complicated ones).
|
||||
</p>
|
||||
|
||||
<p>We will use Runge's function (see <a href="https://en.wikipedia.org/wiki/Runge%27s_phenomenon" target="_blank"><tt>https://en.wikipedia.org/wiki/Runge%27s_phenomenon</tt></a> for a discussion). The one-dimensional function we will study is</p>
|
||||
$$
|
||||
f(x) = \frac{1}{1+25x^2}.
|
||||
$$
|
||||
|
||||
<p>Our first step will be to perform an OLS regression analysis of this
|
||||
function, trying out a polynomial fit with an \( x \) dependence of the
|
||||
form \( [x,x^2,\dots] \). You can use a uniform distribution to set up the
|
||||
arrays of values for \( x \in [-1,1] \), or alternatively use a fixed step size.
|
||||
Thereafter we will repeat many of the same steps when using the Ridge and Lasso regression methods,
|
||||
introducing thereby a dependence on the hyperparameter (penalty) \( \lambda \).
|
||||
</p>
|
||||
|
||||
<p>We will also include bootstrap as a resampling technique in order to
|
||||
study the so-called <b>bias-variance tradeoff</b>. After that we will
|
||||
include the so-called cross-validation technique.
|
||||
</p>
|
||||
<h3 id="part-a-ordinary-least-square-ols-for-the-runge-function">Part a : Ordinary Least Square (OLS) for the Runge function </h3>
|
||||
|
||||
<p>We will generate our own dataset for abovementioned function
|
||||
\( \mathrm{Runge}(x) \) function with \( x\in [-1,1] \). You should explore also the addition
|
||||
of an added stochastic noise to this function using the normal
|
||||
distribution \( N(0,1) \).
|
||||
</p>
|
||||
|
||||
<p><em>Write your own code</em> (using for example the pseudoinverse function <b>pinv</b> from <b>Numpy</b> ) and perform a standard <b>ordinary least square regression</b>
|
||||
analysis using polynomials in \( x \) up to order \( 15 \) or higher. Explore the dependence on the number of data points and the polynomial degree.
|
||||
</p>
|
||||
|
||||
<p>Evaluate the mean Squared error (MSE)</p>
|
||||
|
||||
$$ MSE(\boldsymbol{y},\tilde{\boldsymbol{y}}) = \frac{1}{n}
|
||||
\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2,
|
||||
$$
|
||||
|
||||
<p>and the \( R^2 \) score function. If \( \tilde{\boldsymbol{y}}_i \) is the predicted
|
||||
value of the \( i-th \) sample and \( y_i \) is the corresponding true value,
|
||||
then the score \( R^2 \) is defined as
|
||||
</p>
|
||||
|
||||
$$
|
||||
R^2(\boldsymbol{y}, \tilde{\boldsymbol{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2},
|
||||
$$
|
||||
|
||||
<p>where we have defined the mean value of \( \boldsymbol{y} \) as</p>
|
||||
|
||||
$$
|
||||
\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i.
|
||||
$$
|
||||
|
||||
<p>Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree 15).
|
||||
Plot also the parameters \( \theta \) as you increase the order of the polynomial. Comment your results.
|
||||
</p>
|
||||
|
||||
<p>Your code has to include a scaling/centering of the data (for example by
|
||||
subtracting the mean value), and
|
||||
a split of the data in training and test data. For the scaling you can
|
||||
either write your own code or use for example the function for
|
||||
splitting training data provided by the library <b>Scikit-Learn</b> (make
|
||||
sure you have installed it). This function is called
|
||||
\( train\_test\_split \). <b>You should present a critical discussion of why and how you have scaled or not scaled the data</b>.
|
||||
</p>
|
||||
|
||||
<p>It is normal in essentially all Machine Learning studies to split the
|
||||
data in a training set and a test set (eventually also an additional
|
||||
validation set). There
|
||||
is no explicit recipe for how much data should be included as training
|
||||
data and say test data. An accepted rule of thumb is to use
|
||||
approximately \( 2/3 \) to \( 4/5 \) of the data as training data.
|
||||
</p>
|
||||
|
||||
<p>You can easily reuse the solutions to your exercises from week 35.
|
||||
See also the lecture slides from week 35 and week 36.
|
||||
</p>
|
||||
|
||||
<p>On scaling, we recommend reading the following section from the scikit-learn software description, see <a href="https://scikit-learn.org/stable/auto_examples/preprocessing/plot_all_scaling.html#plot-all-scaling-standard-scaler-section" target="_blank"><tt>https://scikit-learn.org/stable/auto_examples/preprocessing/plot_all_scaling.html#plot-all-scaling-standard-scaler-section</tt></a>.</p>
|
||||
<h3 id="part-b-adding-ridge-regression-for-the-runge-function">Part b: Adding Ridge regression for the Runge function </h3>
|
||||
|
||||
<p>Write your own code for the Ridge method as done in the previous
|
||||
exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the results from the exercise set from week 36 is something you can reuse here.
|
||||
</p>
|
||||
|
||||
<p>Perform the same analysis as you did in the previous exercise but now for different values of \( \lambda \). Compare and
|
||||
analyze your results with those obtained in part a) with the OLS method. Study the
|
||||
dependence on \( \lambda \).
|
||||
</p>
|
||||
<h3 id="part-c-writing-your-own-gradient-descent-code">Part c: Writing your own gradient descent code </h3>
|
||||
|
||||
<p>Replace now the analytical expressions for the optimal parameters
|
||||
\( \boldsymbol{\theta} \) with your own gradient descent code. In this exercise we
|
||||
focus only on the simplest gradient descent approach with a fixed
|
||||
learning rate (see the exercises from week 37 and the lecture notes
|
||||
from week 36).
|
||||
</p>
|
||||
|
||||
<p>Study and compare your results from parts a) and b) with your gradient
|
||||
descent approch. Discuss in particular the role of the learning rate.
|
||||
</p>
|
||||
<h3 id="part-d-including-momentum-and-more-advanced-ways-to-update-the-learning-the-rate">Part d: Including momentum and more advanced ways to update the learning the rate </h3>
|
||||
|
||||
<p>We keep our focus on OLS and Ridge regression and update our code for
|
||||
the gradient descent method by including <b>momentum</b>, <b>ADAgrad</b>,
|
||||
<b>RMSprop</b> and <b>ADAM</b> as methods fro iteratively updating your learning
|
||||
rate. Discuss the results and compare the different methods applied to
|
||||
the one-dimensional Runge function. The lecture notes from week 37 contain several examples on how to implement these methods.
|
||||
</p>
|
||||
<h3 id="part-e-writing-our-own-code-for-lasso-regression">Part e: Writing our own code for Lasso regression </h3>
|
||||
|
||||
<p>LASSO regression (see lecture slides from week 36 and week 37)
|
||||
represents our first encounter with a machine learning method which
|
||||
cannot be solved through analytical expressions (as in OLS and Ridge regression). Use the gradient
|
||||
descent methods you developed in parts c) and d) to solve the LASSO
|
||||
optimization problem. You can compare your results with
|
||||
the functionalities of <b>Scikit-Learn</b>.
|
||||
</p>
|
||||
|
||||
<p>Discuss (critically) your results for the Runge function from OLS,
|
||||
Ridge and LASSO regression using the various gradient descent
|
||||
approaches.
|
||||
</p>
|
||||
<h3 id="part-f-stochastic-gradient-descent">Part f: Stochastic gradient descent </h3>
|
||||
|
||||
<p>Our last gradient step is to include stochastic gradient descent using
|
||||
the same methods to update the learning rates as in parts c-e).
|
||||
Compare and discuss your results with and without stochastic gradient
|
||||
and give a critical assessment of the various methods.
|
||||
</p>
|
||||
<h3 id="part-g-bias-variance-trade-off-and-resampling-techniques">Part g: Bias-variance trade-off and resampling techniques </h3>
|
||||
|
||||
<p>Our aim here is to study the bias-variance trade-off by implementing
|
||||
the <b>bootstrap</b> resampling technique. <b>We will only use the simpler
|
||||
ordinary least squares here</b>.
|
||||
</p>
|
||||
|
||||
<p>With a code which does OLS and includes resampling techniques,
|
||||
we will now discuss the bias-variance trade-off in the context of
|
||||
continuous predictions such as regression. However, many of the
|
||||
intuitions and ideas discussed here also carry over to classification
|
||||
tasks and basically all Machine Learning algorithms.
|
||||
</p>
|
||||
|
||||
<p>Before you perform an analysis of the bias-variance trade-off on your
|
||||
test data, make first a figure similar to Fig. 2.11 of Hastie,
|
||||
Tibshirani, and Friedman. Figure 2.11 of this reference displays only
|
||||
the test and training MSEs. The test MSE can be used to indicate
|
||||
possible regions of low/high bias and variance. You will most likely
|
||||
not get an equally smooth curve! You may also need to increase the
|
||||
polynomial order and play around with the number of data points as
|
||||
well (see also the exercise set from week 35).
|
||||
</p>
|
||||
|
||||
<p>With this result we move on to the bias-variance trade-off analysis.</p>
|
||||
|
||||
<p>Consider a
|
||||
dataset \( \mathcal{L} \) consisting of the data
|
||||
\( \mathbf{X}_\mathcal{L}=\{(y_j, \boldsymbol{x}_j), j=0\ldots n-1\} \).
|
||||
</p>
|
||||
|
||||
<p>We assume that the true data is generated from a noisy model</p>
|
||||
|
||||
$$
|
||||
\boldsymbol{y}=f(\boldsymbol{x}) + \boldsymbol{\epsilon}.
|
||||
$$
|
||||
|
||||
<p>Here \( \epsilon \) is normally distributed with mean zero and standard
|
||||
deviation \( \sigma^2 \).
|
||||
</p>
|
||||
|
||||
<p>In our derivation of the ordinary least squares method we defined then
|
||||
an approximation to the function \( f \) in terms of the parameters
|
||||
\( \boldsymbol{\theta} \) and the design matrix \( \boldsymbol{X} \) which embody our model,
|
||||
that is \( \boldsymbol{\tilde{y}}=\boldsymbol{X}\boldsymbol{\theta} \).
|
||||
</p>
|
||||
|
||||
<p>The parameters \( \boldsymbol{\theta} \) are in turn found by optimizing the mean
|
||||
squared error via the so-called cost function
|
||||
</p>
|
||||
|
||||
$$
|
||||
C(\boldsymbol{X},\boldsymbol{\theta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right].
|
||||
$$
|
||||
|
||||
<p>Here the expected value \( \mathbb{E} \) is the sample value. </p>
|
||||
|
||||
<p>Show that you can rewrite this in terms of a term which contains the
|
||||
variance of the model itself (the so-called variance term), a term
|
||||
which measures the deviation from the true data and the mean value of
|
||||
the model (the bias term) and finally the variance of the noise.
|
||||
</p>
|
||||
|
||||
<p>That is, show that</p>
|
||||
$$
|
||||
\mathbb{E}\left[(\boldsymbol{y}-\boldsymbol{\tilde{y}})^2\right]=\mathrm{Bias}[\tilde{y}]+\mathrm{var}[\tilde{y}]+\sigma^2,
|
||||
$$
|
||||
|
||||
<p>with (we approximate \( f(\boldsymbol{x})\approx \boldsymbol{y} \)) </p>
|
||||
$$
|
||||
\mathrm{Bias}[\tilde{y}]=\mathbb{E}\left[\left(\boldsymbol{y}-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right]\right)^2\right],
|
||||
$$
|
||||
|
||||
<p>and </p>
|
||||
$$
|
||||
\mathrm{var}[\tilde{y}]=\mathbb{E}\left[\left(\tilde{\boldsymbol{y}}-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right]\right)^2\right]=\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\boldsymbol{\tilde{y}}\right])^2.
|
||||
$$
|
||||
|
||||
<p><b>Important note</b>: Since the function \( f(x) \) is unknown, in order to be able to evalute the bias, we replace \( f(\boldsymbol{x}) \) in the expression for the bias with \( \boldsymbol{y} \). </p>
|
||||
|
||||
<p>The answer to this exercise should be included in the theory part of
|
||||
the report. This exercise is also part of the weekly exercises of
|
||||
week 38. Explain what the terms mean and discuss their
|
||||
interpretations.
|
||||
</p>
|
||||
|
||||
<p>Perform then a bias-variance analysis of the Runge function by
|
||||
studying the MSE value as function of the complexity of your model.
|
||||
</p>
|
||||
|
||||
<p>Discuss the bias and variance trade-off as function
|
||||
of your model complexity (the degree of the polynomial) and the number
|
||||
of data points, and possibly also your training and test data using the <b>bootstrap</b> resampling method.
|
||||
You can follow the code example in the jupyter-book at <a href="https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/chapter3.html#the-bias-variance-tradeoff" target="_blank"><tt>https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/chapter3.html#the-bias-variance-tradeoff</tt></a>.
|
||||
</p>
|
||||
<h3 id="part-h-cross-validation-as-resampling-techniques-adding-more-complexity">Part h): Cross-validation as resampling techniques, adding more complexity </h3>
|
||||
|
||||
<p>The aim here is to implement another widely popular
|
||||
resampling technique, the so-called cross-validation method.
|
||||
</p>
|
||||
|
||||
<p>Implement the \( k \)-fold cross-validation algorithm (feel free to use
|
||||
the functionality of <b>Scikit-Learn</b> or write your own code) and
|
||||
evaluate again the MSE function resulting from the test folds.
|
||||
</p>
|
||||
|
||||
<p>Compare the MSE you get from your cross-validation code with the one
|
||||
you got from your <b>bootstrap</b> code from the previous exercise. Comment and interpret your results.
|
||||
</p>
|
||||
|
||||
<p>In addition to using the ordinary least squares method, you should
|
||||
include both Ridge and Lasso regression in the final analysis.
|
||||
</p>
|
||||
<h2 id="background-literature">Background literature </h2>
|
||||
|
||||
<ol>
|
||||
<li> For a discussion and derivation of the variances and mean squared errors using linear regression, see the <a href="https://arxiv.org/abs/1509.09169" target="_blank">Lecture notes on ridge regression by Wessel N. van Wieringen</a></li>
|
||||
<li> The textbook of <a href="https://www.springer.com/gp/book/9780387848570" target="_blank">Trevor Hastie, Robert Tibshirani, Jerome H. Friedman, The Elements of Statistical Learning, Springer</a>, chapters 3 and 7 are the most relevant ones for the analysis here.</li>
|
||||
</ol>
|
||||
<h2 id="introduction-to-numerical-projects">Introduction to numerical projects </h2>
|
||||
|
||||
<p>Here follows a brief recipe and recommendation on how to answer the various questions when preparing your answers. </p>
|
||||
|
||||
<ul>
|
||||
<li> Give a short description of the nature of the problem and the eventual numerical methods you have used.</li>
|
||||
<li> Describe the algorithm you have used and/or developed. Here you may find it convenient to use pseudocoding. In many cases you can describe the algorithm in the program itself.</li>
|
||||
<li> Include the source code of your program. Comment your program properly. You should have the code at your GitHub/GitLab link. You can also place the code in an appendix of your report.</li>
|
||||
<li> If possible, try to find analytic solutions, or known limits in order to test your program when developing the code.</li>
|
||||
<li> Include your results either in figure form or in a table. Remember to label your results. All tables and figures should have relevant captions and labels on the axes.</li>
|
||||
<li> Try to evaluate the reliabilty and numerical stability/precision of your results. If possible, include a qualitative and/or quantitative discussion of the numerical stability, eventual loss of precision etc.</li>
|
||||
<li> Try to give an interpretation of you results in your answers to the problems.</li>
|
||||
<li> Critique: if possible include your comments and reflections about the exercise, whether you felt you learnt something, ideas for improvements and other thoughts you've made when solving the exercise. We wish to keep this course at the interactive level and your comments can help us improve it.</li>
|
||||
<li> Try to establish a practice where you log your work at the computerlab. You may find such a logbook very handy at later stages in your work, especially when you don't properly remember what a previous test version of your program did. Here you could also record the time spent on solving the exercise, various algorithms you may have tested or other topics which you feel worthy of mentioning.</li>
|
||||
</ul>
|
||||
<h2 id="format-for-electronic-delivery-of-report-and-programs">Format for electronic delivery of report and programs </h2>
|
||||
|
||||
<p>The preferred format for the report is a PDF file. You can also use DOC or postscript formats or as an ipython notebook file. As programming language we prefer that you choose between C/C++, Fortran2008, Julia or Python. The following prescription should be followed when preparing the report:</p>
|
||||
|
||||
<ul>
|
||||
<li> Use Canvas to hand in your projects, log in at <a href="https://www.uio.no/english/services/it/education/canvas/" target="_blank"><tt>https://www.uio.no/english/services/it/education/canvas/</tt></a> with your normal UiO username and password.</li>
|
||||
<li> Upload <b>only</b> the report file or the link to your GitHub/GitLab or similar typo of repos! For the source code file(s) you have developed please provide us with your link to your GitHub/GitLab or similar domain. The report file should include all of your discussions and a list of the codes you have developed. Do not include library files which are available at the course homepage, unless you have made specific changes to them.</li>
|
||||
<li> In your GitHub/GitLab or similar repository, please include a folder which contains selected results. These can be in the form of output from your code for a selected set of runs and input parameters.</li>
|
||||
</ul>
|
||||
<p>Finally,
|
||||
we encourage you to collaborate. Optimal working groups consist of
|
||||
2-3 students. You can then hand in a common report.
|
||||
</p>
|
||||
<h2 id="software-and-needed-installations">Software and needed installations </h2>
|
||||
|
||||
<p>If you have Python installed (we recommend Python3) and you feel pretty familiar with installing different packages,
|
||||
we recommend that you install the following Python packages via <b>pip</b> as
|
||||
</p>
|
||||
<ol>
|
||||
<li> pip install numpy scipy matplotlib ipython scikit-learn tensorflow sympy pandas pillow</li>
|
||||
</ol>
|
||||
<p>For Python3, replace <b>pip</b> with <b>pip3</b>.</p>
|
||||
|
||||
<p>See below for a discussion of <b>tensorflow</b> and <b>scikit-learn</b>. </p>
|
||||
|
||||
<p>For OSX users we recommend also, after having installed Xcode, to install <b>brew</b>. Brew allows
|
||||
for a seamless installation of additional software via for example
|
||||
</p>
|
||||
<ol>
|
||||
<li> brew install python3</li>
|
||||
</ol>
|
||||
<p>For Linux users, with its variety of distributions like for example the widely popular Ubuntu distribution
|
||||
you can use <b>pip</b> as well and simply install Python as
|
||||
</p>
|
||||
<ol>
|
||||
<li> sudo apt-get install python3 (or python for python2.7)</li>
|
||||
</ol>
|
||||
<p>etc etc. </p>
|
||||
|
||||
<p>If you don't want to install various Python packages with their dependencies separately, we recommend two widely used distrubutions which set up all relevant dependencies for Python, namely</p>
|
||||
<ol>
|
||||
<li> <a href="https://docs.anaconda.com/" target="_blank">Anaconda</a> Anaconda is an open source distribution of the Python and R programming languages for large-scale data processing, predictive analytics, and scientific computing, that aims to simplify package management and deployment. Package versions are managed by the package management system <b>conda</b></li>
|
||||
<li> <a href="https://www.enthought.com/product/canopy/" target="_blank">Enthought canopy</a> is a Python distribution for scientific and analytic computing distribution and analysis environment, available for free and under a commercial license.</li>
|
||||
</ol>
|
||||
<p>Popular software packages written in Python for ML are</p>
|
||||
|
||||
<ul>
|
||||
<li> <a href="http://scikit-learn.org/stable/" target="_blank">Scikit-learn</a>,</li>
|
||||
<li> <a href="https://www.tensorflow.org/" target="_blank">Tensorflow</a>,</li>
|
||||
<li> <a href="http://pytorch.org/" target="_blank">PyTorch</a> and</li>
|
||||
<li> <a href="https://keras.io/" target="_blank">Keras</a>.</li>
|
||||
</ul>
|
||||
<p>These are all freely available at their respective GitHub sites. They
|
||||
encompass communities of developers in the thousands or more. And the number
|
||||
of code developers and contributors keeps increasing.
|
||||
</p>
|
||||
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -0,0 +1,641 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b209e219",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"<!-- HTML file automatically generated from DocOnce source (https://github.com/doconce/doconce/)\n",
|
||||
"doconce format html Project1.do.txt -->\n",
|
||||
"<!-- dom:TITLE: Project 1 on Machine Learning, deadline October 6 (midnight), 2025 -->"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "6fa4c4bc",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"# Project 1 on Machine Learning, deadline October 6 (midnight), 2025\n",
|
||||
"**Data Analysis and Machine Learning FYS-STK3155/FYS4155**, University of Oslo, Norway\n",
|
||||
"\n",
|
||||
"Date: **September 2**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "735b16c4",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"## Preamble: Note on writing reports, using reference material, AI and other tools\n",
|
||||
"\n",
|
||||
"We want you to answer the three different projects by handing in\n",
|
||||
"reports written like a standard scientific/technical report. The\n",
|
||||
"links at\n",
|
||||
"<https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects>\n",
|
||||
"contain more information. There you can find examples of previous\n",
|
||||
"reports, the projects themselves, how we rade reports etc. How to\n",
|
||||
"write reports will also be discussed during the various lab\n",
|
||||
"sessions. Please do ask us if you are in doubt.\n",
|
||||
"\n",
|
||||
"When using codes and material from other sources, you should refer to\n",
|
||||
"these in the bibliography of your report, indicating wherefrom you for\n",
|
||||
"example got the code, whether this is from the lecture notes,\n",
|
||||
"softwares like Scikit-Learn, TensorFlow, PyTorch or other sources. These sources\n",
|
||||
"should always be cited correctly. How to cite some\n",
|
||||
"of the libraries is often indicated from their corresponding GitHub\n",
|
||||
"sites or websites, see for example how to cite Scikit-Learn at\n",
|
||||
"<https://scikit-learn.org/dev/about.html>.\n",
|
||||
"\n",
|
||||
"We enocurage you to use tools like\n",
|
||||
"[ChatGPT](https://openai.com/chatgpt/) or similar in writing the report. If you use for example ChatGPT,\n",
|
||||
"please do cite it properly and include (if possible) your questions and answers as an addition to the report. This can\n",
|
||||
"be uploaded to for example your website, GitHub/GitLab or similar as supplemental material.\n",
|
||||
"\n",
|
||||
"If you would like to study other data sets, feel free to propose other\n",
|
||||
"sets. What we have proposed here are mere suggestions from our\n",
|
||||
"side. If you opt for another data set, consider using a set which has\n",
|
||||
"been studied in the scientific literature. This makes it easier for\n",
|
||||
"you to compare and analyze your results. Comparing with existing\n",
|
||||
"results from the scientific literature is also an essential element of\n",
|
||||
"the scientific discussion. The University of California at Irvine\n",
|
||||
"with its Machine Learning repository at\n",
|
||||
"<https://archive.ics.uci.edu/ml/index.php> is an excellent site to\n",
|
||||
"look up for examples and\n",
|
||||
"inspiration. [Kaggle.com](https://www.kaggle.com/) is an equally\n",
|
||||
"interesting site. Feel free to explore these sites. When selecting\n",
|
||||
"other data sets, make sure these are sets used for regression problems\n",
|
||||
"(not classification)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "0b7956ca",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"## Regression analysis and resampling methods\n",
|
||||
"\n",
|
||||
"The main aim of this project is to study in more detail various\n",
|
||||
"regression methods, including Ordinary Least Squares (OLS) reegression, Ridge regression and LASSO regression.\n",
|
||||
"In addition to the scientific part, in this course we want also to\n",
|
||||
"give you an experience in writing scientific reports.\n",
|
||||
"\n",
|
||||
"We will study how to fit polynomials to specific\n",
|
||||
"one-dimensional functions (feel free to replace the suggested function with more complicated ones).\n",
|
||||
"\n",
|
||||
"We will use Runge's function (see <https://en.wikipedia.org/wiki/Runge%27s_phenomenon> for a discussion). The one-dimensional function we will study is"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "28ba3d22",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"f(x) = \\frac{1}{1+25x^2}.\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "9a3e10ba",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"Our first step will be to perform an OLS regression analysis of this\n",
|
||||
"function, trying out a polynomial fit with an $x$ dependence of the\n",
|
||||
"form $[x,x^2,\\dots]$. You can use a uniform distribution to set up the\n",
|
||||
"arrays of values for $x \\in [-1,1]$, or alternatively use a fixed step size.\n",
|
||||
"Thereafter we will repeat many of the same steps when using the Ridge and Lasso regression methods,\n",
|
||||
"introducing thereby a dependence on the hyperparameter (penalty) $\\lambda$.\n",
|
||||
"\n",
|
||||
"We will also include bootstrap as a resampling technique in order to\n",
|
||||
"study the so-called **bias-variance tradeoff**. After that we will\n",
|
||||
"include the so-called cross-validation technique."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "8aa547a5",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"### Part a : Ordinary Least Square (OLS) for the Runge function\n",
|
||||
"\n",
|
||||
"We will generate our own dataset for abovementioned function\n",
|
||||
"$\\mathrm{Runge}(x)$ function with $x\\in [-1,1]$. You should explore also the addition\n",
|
||||
"of an added stochastic noise to this function using the normal\n",
|
||||
"distribution $N(0,1)$.\n",
|
||||
"\n",
|
||||
"*Write your own code* (using for example the pseudoinverse function **pinv** from **Numpy** ) and perform a standard **ordinary least square regression**\n",
|
||||
"analysis using polynomials in $x$ up to order $15$ or higher. Explore the dependence on the number of data points and the polynomial degree.\n",
|
||||
"\n",
|
||||
"Evaluate the mean Squared error (MSE)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "68fbf03d",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"MSE(\\boldsymbol{y},\\tilde{\\boldsymbol{y}}) = \\frac{1}{n}\n",
|
||||
"\\sum_{i=0}^{n-1}(y_i-\\tilde{y}_i)^2,\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b49509bc",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"and the $R^2$ score function. If $\\tilde{\\boldsymbol{y}}_i$ is the predicted\n",
|
||||
"value of the $i-th$ sample and $y_i$ is the corresponding true value,\n",
|
||||
"then the score $R^2$ is defined as"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "0fa4ffc6",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"R^2(\\boldsymbol{y}, \\tilde{\\boldsymbol{y}}) = 1 - \\frac{\\sum_{i=0}^{n - 1} (y_i - \\tilde{y}_i)^2}{\\sum_{i=0}^{n - 1} (y_i - \\bar{y})^2},\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "ce462b32",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"where we have defined the mean value of $\\boldsymbol{y}$ as"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "a5fbef36",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\bar{y} = \\frac{1}{n} \\sum_{i=0}^{n - 1} y_i.\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "a6afe9cb",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree 15).\n",
|
||||
"Plot also the parameters $\\theta$ as you increase the order of the polynomial. Comment your results.\n",
|
||||
"\n",
|
||||
"Your code has to include a scaling/centering of the data (for example by\n",
|
||||
"subtracting the mean value), and\n",
|
||||
"a split of the data in training and test data. For the scaling you can\n",
|
||||
"either write your own code or use for example the function for\n",
|
||||
"splitting training data provided by the library **Scikit-Learn** (make\n",
|
||||
"sure you have installed it). This function is called\n",
|
||||
"$train\\_test\\_split$. **You should present a critical discussion of why and how you have scaled or not scaled the data**.\n",
|
||||
"\n",
|
||||
"It is normal in essentially all Machine Learning studies to split the\n",
|
||||
"data in a training set and a test set (eventually also an additional\n",
|
||||
"validation set). There\n",
|
||||
"is no explicit recipe for how much data should be included as training\n",
|
||||
"data and say test data. An accepted rule of thumb is to use\n",
|
||||
"approximately $2/3$ to $4/5$ of the data as training data.\n",
|
||||
"\n",
|
||||
"You can easily reuse the solutions to your exercises from week 35.\n",
|
||||
"See also the lecture slides from week 35 and week 36.\n",
|
||||
"\n",
|
||||
"On scaling, we recommend reading the following section from the scikit-learn software description, see <https://scikit-learn.org/stable/auto_examples/preprocessing/plot_all_scaling.html#plot-all-scaling-standard-scaler-section>."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "3be10f68",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"### Part b: Adding Ridge regression for the Runge function\n",
|
||||
"\n",
|
||||
"Write your own code for the Ridge method as done in the previous\n",
|
||||
"exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the results from the exercise set from week 36 is something you can reuse here.\n",
|
||||
"\n",
|
||||
"Perform the same analysis as you did in the previous exercise but now for different values of $\\lambda$. Compare and\n",
|
||||
"analyze your results with those obtained in part a) with the OLS method. Study the\n",
|
||||
"dependence on $\\lambda$."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "caa7909c",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"### Part c: Writing your own gradient descent code\n",
|
||||
"\n",
|
||||
"Replace now the analytical expressions for the optimal parameters\n",
|
||||
"$\\boldsymbol{\\theta}$ with your own gradient descent code. In this exercise we\n",
|
||||
"focus only on the simplest gradient descent approach with a fixed\n",
|
||||
"learning rate (see the exercises from week 37 and the lecture notes\n",
|
||||
"from week 36).\n",
|
||||
"\n",
|
||||
"Study and compare your results from parts a) and b) with your gradient\n",
|
||||
"descent approch. Discuss in particular the role of the learning rate."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "3aac4df1",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"### Part d: Including momentum and more advanced ways to update the learning the rate\n",
|
||||
"\n",
|
||||
"We keep our focus on OLS and Ridge regression and update our code for\n",
|
||||
"the gradient descent method by including **momentum**, **ADAgrad**,\n",
|
||||
"**RMSprop** and **ADAM** as methods fro iteratively updating your learning\n",
|
||||
"rate. Discuss the results and compare the different methods applied to\n",
|
||||
"the one-dimensional Runge function. The lecture notes from week 37 contain several examples on how to implement these methods."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d0862a53",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"### Part e: Writing our own code for Lasso regression\n",
|
||||
"\n",
|
||||
"LASSO regression (see lecture slides from week 36 and week 37)\n",
|
||||
"represents our first encounter with a machine learning method which\n",
|
||||
"cannot be solved through analytical expressions (as in OLS and Ridge regression). Use the gradient\n",
|
||||
"descent methods you developed in parts c) and d) to solve the LASSO\n",
|
||||
"optimization problem. You can compare your results with \n",
|
||||
"the functionalities of **Scikit-Learn**.\n",
|
||||
"\n",
|
||||
"Discuss (critically) your results for the Runge function from OLS,\n",
|
||||
"Ridge and LASSO regression using the various gradient descent\n",
|
||||
"approaches."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "9170032e",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"### Part f: Stochastic gradient descent\n",
|
||||
"\n",
|
||||
"Our last gradient step is to include stochastic gradient descent using\n",
|
||||
"the same methods to update the learning rates as in parts c-e).\n",
|
||||
"Compare and discuss your results with and without stochastic gradient\n",
|
||||
"and give a critical assessment of the various methods."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "bacd1035",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"### Part g: Bias-variance trade-off and resampling techniques\n",
|
||||
"\n",
|
||||
"Our aim here is to study the bias-variance trade-off by implementing\n",
|
||||
"the **bootstrap** resampling technique. **We will only use the simpler\n",
|
||||
"ordinary least squares here**.\n",
|
||||
"\n",
|
||||
"With a code which does OLS and includes resampling techniques, \n",
|
||||
"we will now discuss the bias-variance trade-off in the context of\n",
|
||||
"continuous predictions such as regression. However, many of the\n",
|
||||
"intuitions and ideas discussed here also carry over to classification\n",
|
||||
"tasks and basically all Machine Learning algorithms. \n",
|
||||
"\n",
|
||||
"Before you perform an analysis of the bias-variance trade-off on your\n",
|
||||
"test data, make first a figure similar to Fig. 2.11 of Hastie,\n",
|
||||
"Tibshirani, and Friedman. Figure 2.11 of this reference displays only\n",
|
||||
"the test and training MSEs. The test MSE can be used to indicate\n",
|
||||
"possible regions of low/high bias and variance. You will most likely\n",
|
||||
"not get an equally smooth curve! You may also need to increase the\n",
|
||||
"polynomial order and play around with the number of data points as\n",
|
||||
"well (see also the exercise set from week 35).\n",
|
||||
"\n",
|
||||
"With this result we move on to the bias-variance trade-off analysis.\n",
|
||||
"\n",
|
||||
"Consider a\n",
|
||||
"dataset $\\mathcal{L}$ consisting of the data\n",
|
||||
"$\\mathbf{X}_\\mathcal{L}=\\{(y_j, \\boldsymbol{x}_j), j=0\\ldots n-1\\}$.\n",
|
||||
"\n",
|
||||
"We assume that the true data is generated from a noisy model"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b871ec69",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\boldsymbol{y}=f(\\boldsymbol{x}) + \\boldsymbol{\\epsilon}.\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b47c19bc",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"Here $\\epsilon$ is normally distributed with mean zero and standard\n",
|
||||
"deviation $\\sigma^2$.\n",
|
||||
"\n",
|
||||
"In our derivation of the ordinary least squares method we defined then\n",
|
||||
"an approximation to the function $f$ in terms of the parameters\n",
|
||||
"$\\boldsymbol{\\theta}$ and the design matrix $\\boldsymbol{X}$ which embody our model,\n",
|
||||
"that is $\\boldsymbol{\\tilde{y}}=\\boldsymbol{X}\\boldsymbol{\\theta}$.\n",
|
||||
"\n",
|
||||
"The parameters $\\boldsymbol{\\theta}$ are in turn found by optimizing the mean\n",
|
||||
"squared error via the so-called cost function"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "6db622c2",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"C(\\boldsymbol{X},\\boldsymbol{\\theta}) =\\frac{1}{n}\\sum_{i=0}^{n-1}(y_i-\\tilde{y}_i)^2=\\mathbb{E}\\left[(\\boldsymbol{y}-\\boldsymbol{\\tilde{y}})^2\\right].\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "5a7eb70d",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"Here the expected value $\\mathbb{E}$ is the sample value. \n",
|
||||
"\n",
|
||||
"Show that you can rewrite this in terms of a term which contains the\n",
|
||||
"variance of the model itself (the so-called variance term), a term\n",
|
||||
"which measures the deviation from the true data and the mean value of\n",
|
||||
"the model (the bias term) and finally the variance of the noise.\n",
|
||||
"\n",
|
||||
"That is, show that"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d50292fe",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\mathbb{E}\\left[(\\boldsymbol{y}-\\boldsymbol{\\tilde{y}})^2\\right]=\\mathrm{Bias}[\\tilde{y}]+\\mathrm{var}[\\tilde{y}]+\\sigma^2,\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "50fa641f",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"with (we approximate $f(\\boldsymbol{x})\\approx \\boldsymbol{y}$)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "2bd429c9",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\mathrm{Bias}[\\tilde{y}]=\\mathbb{E}\\left[\\left(\\boldsymbol{y}-\\mathbb{E}\\left[\\boldsymbol{\\tilde{y}}\\right]\\right)^2\\right],\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "737c2819",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"and"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "41ef92ef",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\mathrm{var}[\\tilde{y}]=\\mathbb{E}\\left[\\left(\\tilde{\\boldsymbol{y}}-\\mathbb{E}\\left[\\boldsymbol{\\tilde{y}}\\right]\\right)^2\\right]=\\frac{1}{n}\\sum_i(\\tilde{y}_i-\\mathbb{E}\\left[\\boldsymbol{\\tilde{y}}\\right])^2.\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b948dab0",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"**Important note**: Since the function $f(x)$ is unknown, in order to be able to evalute the bias, we replace $f(\\boldsymbol{x})$ in the expression for the bias with $\\boldsymbol{y}$. \n",
|
||||
"\n",
|
||||
"The answer to this exercise should be included in the theory part of\n",
|
||||
"the report. This exercise is also part of the weekly exercises of\n",
|
||||
"week 38. Explain what the terms mean and discuss their\n",
|
||||
"interpretations.\n",
|
||||
"\n",
|
||||
"Perform then a bias-variance analysis of the Runge function by\n",
|
||||
"studying the MSE value as function of the complexity of your model.\n",
|
||||
"\n",
|
||||
"Discuss the bias and variance trade-off as function\n",
|
||||
"of your model complexity (the degree of the polynomial) and the number\n",
|
||||
"of data points, and possibly also your training and test data using the **bootstrap** resampling method.\n",
|
||||
"You can follow the code example in the jupyter-book at <https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/chapter3.html#the-bias-variance-tradeoff>."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "6a0548bf",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"### Part h): Cross-validation as resampling techniques, adding more complexity\n",
|
||||
"\n",
|
||||
"The aim here is to implement another widely popular\n",
|
||||
"resampling technique, the so-called cross-validation method. \n",
|
||||
"\n",
|
||||
"Implement the $k$-fold cross-validation algorithm (feel free to use\n",
|
||||
"the functionality of **Scikit-Learn** or write your own code) and\n",
|
||||
"evaluate again the MSE function resulting from the test folds.\n",
|
||||
"\n",
|
||||
"Compare the MSE you get from your cross-validation code with the one\n",
|
||||
"you got from your **bootstrap** code from the previous exercise. Comment and interpret your results. \n",
|
||||
"\n",
|
||||
"In addition to using the ordinary least squares method, you should\n",
|
||||
"include both Ridge and Lasso regression in the final analysis."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "df9845cb",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"## Background literature\n",
|
||||
"\n",
|
||||
"1. For a discussion and derivation of the variances and mean squared errors using linear regression, see the [Lecture notes on ridge regression by Wessel N. van Wieringen](https://arxiv.org/abs/1509.09169)\n",
|
||||
"\n",
|
||||
"2. The textbook of [Trevor Hastie, Robert Tibshirani, Jerome H. Friedman, The Elements of Statistical Learning, Springer](https://www.springer.com/gp/book/9780387848570), chapters 3 and 7 are the most relevant ones for the analysis here."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b9e04791",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"## Introduction to numerical projects\n",
|
||||
"\n",
|
||||
"Here follows a brief recipe and recommendation on how to answer the various questions when preparing your answers. \n",
|
||||
"\n",
|
||||
" * Give a short description of the nature of the problem and the eventual numerical methods you have used.\n",
|
||||
"\n",
|
||||
" * Describe the algorithm you have used and/or developed. Here you may find it convenient to use pseudocoding. In many cases you can describe the algorithm in the program itself.\n",
|
||||
"\n",
|
||||
" * Include the source code of your program. Comment your program properly. You should have the code at your GitHub/GitLab link. You can also place the code in an appendix of your report.\n",
|
||||
"\n",
|
||||
" * If possible, try to find analytic solutions, or known limits in order to test your program when developing the code.\n",
|
||||
"\n",
|
||||
" * Include your results either in figure form or in a table. Remember to label your results. All tables and figures should have relevant captions and labels on the axes.\n",
|
||||
"\n",
|
||||
" * Try to evaluate the reliabilty and numerical stability/precision of your results. If possible, include a qualitative and/or quantitative discussion of the numerical stability, eventual loss of precision etc.\n",
|
||||
"\n",
|
||||
" * Try to give an interpretation of you results in your answers to the problems.\n",
|
||||
"\n",
|
||||
" * Critique: if possible include your comments and reflections about the exercise, whether you felt you learnt something, ideas for improvements and other thoughts you've made when solving the exercise. We wish to keep this course at the interactive level and your comments can help us improve it.\n",
|
||||
"\n",
|
||||
" * Try to establish a practice where you log your work at the computerlab. You may find such a logbook very handy at later stages in your work, especially when you don't properly remember what a previous test version of your program did. Here you could also record the time spent on solving the exercise, various algorithms you may have tested or other topics which you feel worthy of mentioning."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "3fab6237",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"## Format for electronic delivery of report and programs\n",
|
||||
"\n",
|
||||
"The preferred format for the report is a PDF file. You can also use DOC or postscript formats or as an ipython notebook file. As programming language we prefer that you choose between C/C++, Fortran2008, Julia or Python. The following prescription should be followed when preparing the report:\n",
|
||||
"\n",
|
||||
" * Use Canvas to hand in your projects, log in at <https://www.uio.no/english/services/it/education/canvas/> with your normal UiO username and password.\n",
|
||||
"\n",
|
||||
" * Upload **only** the report file or the link to your GitHub/GitLab or similar typo of repos! For the source code file(s) you have developed please provide us with your link to your GitHub/GitLab or similar domain. The report file should include all of your discussions and a list of the codes you have developed. Do not include library files which are available at the course homepage, unless you have made specific changes to them.\n",
|
||||
"\n",
|
||||
" * In your GitHub/GitLab or similar repository, please include a folder which contains selected results. These can be in the form of output from your code for a selected set of runs and input parameters.\n",
|
||||
"\n",
|
||||
"Finally, \n",
|
||||
"we encourage you to collaborate. Optimal working groups consist of \n",
|
||||
"2-3 students. You can then hand in a common report."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "3388eb60",
|
||||
"metadata": {
|
||||
"editable": true
|
||||
},
|
||||
"source": [
|
||||
"## Software and needed installations\n",
|
||||
"\n",
|
||||
"If you have Python installed (we recommend Python3) and you feel pretty familiar with installing different packages, \n",
|
||||
"we recommend that you install the following Python packages via **pip** as\n",
|
||||
"1. pip install numpy scipy matplotlib ipython scikit-learn tensorflow sympy pandas pillow\n",
|
||||
"\n",
|
||||
"For Python3, replace **pip** with **pip3**.\n",
|
||||
"\n",
|
||||
"See below for a discussion of **tensorflow** and **scikit-learn**. \n",
|
||||
"\n",
|
||||
"For OSX users we recommend also, after having installed Xcode, to install **brew**. Brew allows \n",
|
||||
"for a seamless installation of additional software via for example\n",
|
||||
"1. brew install python3\n",
|
||||
"\n",
|
||||
"For Linux users, with its variety of distributions like for example the widely popular Ubuntu distribution\n",
|
||||
"you can use **pip** as well and simply install Python as \n",
|
||||
"1. sudo apt-get install python3 (or python for python2.7)\n",
|
||||
"\n",
|
||||
"etc etc. \n",
|
||||
"\n",
|
||||
"If you don't want to install various Python packages with their dependencies separately, we recommend two widely used distrubutions which set up all relevant dependencies for Python, namely\n",
|
||||
"1. [Anaconda](https://docs.anaconda.com/) Anaconda is an open source distribution of the Python and R programming languages for large-scale data processing, predictive analytics, and scientific computing, that aims to simplify package management and deployment. Package versions are managed by the package management system **conda**\n",
|
||||
"\n",
|
||||
"2. [Enthought canopy](https://www.enthought.com/product/canopy/) is a Python distribution for scientific and analytic computing distribution and analysis environment, available for free and under a commercial license.\n",
|
||||
"\n",
|
||||
"Popular software packages written in Python for ML are\n",
|
||||
"\n",
|
||||
"* [Scikit-learn](http://scikit-learn.org/stable/), \n",
|
||||
"\n",
|
||||
"* [Tensorflow](https://www.tensorflow.org/),\n",
|
||||
"\n",
|
||||
"* [PyTorch](http://pytorch.org/) and \n",
|
||||
"\n",
|
||||
"* [Keras](https://keras.io/).\n",
|
||||
"\n",
|
||||
"These are all freely available at their respective GitHub sites. They \n",
|
||||
"encompass communities of developers in the thousands or more. And the number\n",
|
||||
"of code developers and contributors keeps increasing."
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -0,0 +1,504 @@
|
||||
%%
|
||||
%% Automatically generated file from DocOnce source
|
||||
%% (https://github.com/doconce/doconce/)
|
||||
%% doconce format latex Project1.do.txt --print_latex_style=trac --latex_admon=paragraph
|
||||
%%
|
||||
% #ifdef PTEX2TEX_EXPLANATION
|
||||
%%
|
||||
%% The file follows the ptex2tex extended LaTeX format, see
|
||||
%% ptex2tex: https://code.google.com/p/ptex2tex/
|
||||
%%
|
||||
%% Run
|
||||
%% ptex2tex myfile
|
||||
%% or
|
||||
%% doconce ptex2tex myfile
|
||||
%%
|
||||
%% to turn myfile.p.tex into an ordinary LaTeX file myfile.tex.
|
||||
%% (The ptex2tex program: https://code.google.com/p/ptex2tex)
|
||||
%% Many preprocess options can be added to ptex2tex or doconce ptex2tex
|
||||
%%
|
||||
%% ptex2tex -DMINTED myfile
|
||||
%% doconce ptex2tex myfile envir=minted
|
||||
%%
|
||||
%% ptex2tex will typeset code environments according to a global or local
|
||||
%% .ptex2tex.cfg configure file. doconce ptex2tex will typeset code
|
||||
%% according to options on the command line (just type doconce ptex2tex to
|
||||
%% see examples). If doconce ptex2tex has envir=minted, it enables the
|
||||
%% minted style without needing -DMINTED.
|
||||
% #endif
|
||||
|
||||
% #define PREAMBLE
|
||||
|
||||
% #ifdef PREAMBLE
|
||||
%-------------------- begin preamble ----------------------
|
||||
|
||||
\documentclass[%
|
||||
oneside, % oneside: electronic viewing, twoside: printing
|
||||
final, % draft: marks overfull hboxes, figures with paths
|
||||
10pt]{article}
|
||||
|
||||
\listfiles % print all files needed to compile this document
|
||||
|
||||
\usepackage{relsize,makeidx,color,setspace,amsmath,amsfonts,amssymb}
|
||||
\usepackage[table]{xcolor}
|
||||
\usepackage{bm,ltablex,microtype}
|
||||
|
||||
\usepackage[pdftex]{graphicx}
|
||||
|
||||
\usepackage[T1]{fontenc}
|
||||
%\usepackage[latin1]{inputenc}
|
||||
\usepackage{ucs}
|
||||
\usepackage[utf8x]{inputenc}
|
||||
|
||||
\usepackage{lmodern} % Latin Modern fonts derived from Computer Modern
|
||||
|
||||
% Hyperlinks in PDF:
|
||||
\definecolor{linkcolor}{rgb}{0,0,0.4}
|
||||
\usepackage{hyperref}
|
||||
\hypersetup{
|
||||
breaklinks=true,
|
||||
colorlinks=true,
|
||||
linkcolor=linkcolor,
|
||||
urlcolor=linkcolor,
|
||||
citecolor=black,
|
||||
filecolor=black,
|
||||
%filecolor=blue,
|
||||
pdfmenubar=true,
|
||||
pdftoolbar=true,
|
||||
bookmarksdepth=3 % Uncomment (and tweak) for PDF bookmarks with more levels than the TOC
|
||||
}
|
||||
%\hyperbaseurl{} % hyperlinks are relative to this root
|
||||
|
||||
\setcounter{tocdepth}{2} % levels in table of contents
|
||||
|
||||
% prevent orhpans and widows
|
||||
\clubpenalty = 10000
|
||||
\widowpenalty = 10000
|
||||
|
||||
% --- end of standard preamble for documents ---
|
||||
|
||||
|
||||
% insert custom LaTeX commands...
|
||||
|
||||
\raggedbottom
|
||||
\makeindex
|
||||
\usepackage[totoc]{idxlayout} % for index in the toc
|
||||
\usepackage[nottoc]{tocbibind} % for references/bibliography in the toc
|
||||
|
||||
%-------------------- end preamble ----------------------
|
||||
|
||||
\begin{document}
|
||||
|
||||
% matching end for #ifdef PREAMBLE
|
||||
% #endif
|
||||
|
||||
\newcommand{\exercisesection}[1]{\subsection*{#1}}
|
||||
|
||||
|
||||
% ------------------- main content ----------------------
|
||||
|
||||
|
||||
|
||||
% ----------------- title -------------------------
|
||||
|
||||
\thispagestyle{empty}
|
||||
|
||||
\begin{center}
|
||||
{\LARGE\bf
|
||||
\begin{spacing}{1.25}
|
||||
Project 1 on Machine Learning, deadline October 6 (midnight), 2025
|
||||
\end{spacing}
|
||||
}
|
||||
\end{center}
|
||||
|
||||
% ----------------- author(s) -------------------------
|
||||
|
||||
\begin{center}
|
||||
{\bf Data Analysis and Machine Learning FYS-STK3155/FYS4155}
|
||||
\end{center}
|
||||
|
||||
\begin{center}
|
||||
% List of all institutions:
|
||||
\centerline{{\small University of Oslo, Norway}}
|
||||
\end{center}
|
||||
|
||||
% ----------------- end author(s) -------------------------
|
||||
|
||||
% --- begin date ---
|
||||
\begin{center}
|
||||
September 2
|
||||
\end{center}
|
||||
% --- end date ---
|
||||
|
||||
\vspace{1cm}
|
||||
|
||||
|
||||
\subsection{Preamble: Note on writing reports, using reference material, AI and other tools}
|
||||
|
||||
We want you to answer the three different projects by handing in
|
||||
reports written like a standard scientific/technical report. The
|
||||
links at
|
||||
\href{{https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects}}{\nolinkurl{https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects}}
|
||||
contain more information. There you can find examples of previous
|
||||
reports, the projects themselves, how we rade reports etc. How to
|
||||
write reports will also be discussed during the various lab
|
||||
sessions. Please do ask us if you are in doubt.
|
||||
|
||||
When using codes and material from other sources, you should refer to
|
||||
these in the bibliography of your report, indicating wherefrom you for
|
||||
example got the code, whether this is from the lecture notes,
|
||||
softwares like Scikit-Learn, TensorFlow, PyTorch or other sources. These sources
|
||||
should always be cited correctly. How to cite some
|
||||
of the libraries is often indicated from their corresponding GitHub
|
||||
sites or websites, see for example how to cite Scikit-Learn at
|
||||
\href{{https://scikit-learn.org/dev/about.html}}{\nolinkurl{https://scikit-learn.org/dev/about.html}}.
|
||||
|
||||
We enocurage you to use tools like
|
||||
\href{{https://openai.com/chatgpt/}}{ChatGPT} or similar in writing the report. If you use for example ChatGPT,
|
||||
please do cite it properly and include (if possible) your questions and answers as an addition to the report. This can
|
||||
be uploaded to for example your website, GitHub/GitLab or similar as supplemental material.
|
||||
|
||||
If you would like to study other data sets, feel free to propose other
|
||||
sets. What we have proposed here are mere suggestions from our
|
||||
side. If you opt for another data set, consider using a set which has
|
||||
been studied in the scientific literature. This makes it easier for
|
||||
you to compare and analyze your results. Comparing with existing
|
||||
results from the scientific literature is also an essential element of
|
||||
the scientific discussion. The University of California at Irvine
|
||||
with its Machine Learning repository at
|
||||
\href{{https://archive.ics.uci.edu/ml/index.php}}{\nolinkurl{https://archive.ics.uci.edu/ml/index.php}} is an excellent site to
|
||||
look up for examples and
|
||||
inspiration. \href{{https://www.kaggle.com/}}{Kaggle.com} is an equally
|
||||
interesting site. Feel free to explore these sites. When selecting
|
||||
other data sets, make sure these are sets used for regression problems
|
||||
(not classification).
|
||||
|
||||
% !split
|
||||
\subsection{Regression analysis and resampling methods}
|
||||
|
||||
The main aim of this project is to study in more detail various
|
||||
regression methods, including Ordinary Least Squares (OLS) reegression, Ridge regression and LASSO regression.
|
||||
In addition to the scientific part, in this course we want also to
|
||||
give you an experience in writing scientific reports.
|
||||
|
||||
We will study how to fit polynomials to specific
|
||||
one-dimensional functions (feel free to replace the suggested function with more complicated ones).
|
||||
|
||||
We will use Runge's function (see \href{{https://en.wikipedia.org/wiki/Runge%27s_phenomenon}}{\nolinkurl{https://en.wikipedia.org/wiki/Runge\%27s_phenomenon}} for a discussion). The one-dimensional function we will study is
|
||||
\[
|
||||
f(x) = \frac{1}{1+25x^2}.
|
||||
\]
|
||||
|
||||
Our first step will be to perform an OLS regression analysis of this
|
||||
function, trying out a polynomial fit with an $x$ dependence of the
|
||||
form $[x,x^2,\dots]$. You can use a uniform distribution to set up the
|
||||
arrays of values for $x \in [-1,1]$, or alternatively use a fixed step size.
|
||||
Thereafter we will repeat many of the same steps when using the Ridge and Lasso regression methods,
|
||||
introducing thereby a dependence on the hyperparameter (penalty) $\lambda$.
|
||||
|
||||
We will also include bootstrap as a resampling technique in order to
|
||||
study the so-called \textbf{bias-variance tradeoff}. After that we will
|
||||
include the so-called cross-validation technique.
|
||||
|
||||
\paragraph{Part a : Ordinary Least Square (OLS) for the Runge function.}
|
||||
We will generate our own dataset for abovementioned function
|
||||
$\mathrm{Runge}(x)$ function with $x\in [-1,1]$. You should explore also the addition
|
||||
of an added stochastic noise to this function using the normal
|
||||
distribution $N(0,1)$.
|
||||
|
||||
\emph{Write your own code} (using for example the pseudoinverse function \textbf{pinv} from \textbf{Numpy} ) and perform a standard \textbf{ordinary least square regression}
|
||||
analysis using polynomials in $x$ up to order $15$ or higher. Explore the dependence on the number of data points and the polynomial degree.
|
||||
|
||||
Evaluate the mean Squared error (MSE)
|
||||
|
||||
\[ MSE(\bm{y},\tilde{\bm{y}}) = \frac{1}{n}
|
||||
\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2,
|
||||
\]
|
||||
|
||||
and the $R^2$ score function. If $\tilde{\bm{y}}_i$ is the predicted
|
||||
value of the $i-th$ sample and $y_i$ is the corresponding true value,
|
||||
then the score $R^2$ is defined as
|
||||
|
||||
\[
|
||||
R^2(\bm{y}, \tilde{\bm{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2},
|
||||
\]
|
||||
|
||||
where we have defined the mean value of $\bm{y}$ as
|
||||
|
||||
\[
|
||||
\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i.
|
||||
\]
|
||||
|
||||
Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree 15).
|
||||
Plot also the parameters $\theta$ as you increase the order of the polynomial. Comment your results.
|
||||
|
||||
Your code has to include a scaling/centering of the data (for example by
|
||||
subtracting the mean value), and
|
||||
a split of the data in training and test data. For the scaling you can
|
||||
either write your own code or use for example the function for
|
||||
splitting training data provided by the library \textbf{Scikit-Learn} (make
|
||||
sure you have installed it). This function is called
|
||||
$train\_test\_split$. \textbf{You should present a critical discussion of why and how you have scaled or not scaled the data}.
|
||||
|
||||
It is normal in essentially all Machine Learning studies to split the
|
||||
data in a training set and a test set (eventually also an additional
|
||||
validation set). There
|
||||
is no explicit recipe for how much data should be included as training
|
||||
data and say test data. An accepted rule of thumb is to use
|
||||
approximately $2/3$ to $4/5$ of the data as training data.
|
||||
|
||||
You can easily reuse the solutions to your exercises from week 35.
|
||||
See also the lecture slides from week 35 and week 36.
|
||||
|
||||
On scaling, we recommend reading the following section from the scikit-learn software description, see \href{{https://scikit-learn.org/stable/auto_examples/preprocessing/plot_all_scaling.html#plot-all-scaling-standard-scaler-section}}{\nolinkurl{https://scikit-learn.org/stable/auto_examples/preprocessing/plot_all_scaling.html\#plot-all-scaling-standard-scaler-section}}.
|
||||
|
||||
\paragraph{Part b: Adding Ridge regression for the Runge function.}
|
||||
Write your own code for the Ridge method as done in the previous
|
||||
exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the results from the exercise set from week 36 is something you can reuse here.
|
||||
|
||||
Perform the same analysis as you did in the previous exercise but now for different values of $\lambda$. Compare and
|
||||
analyze your results with those obtained in part a) with the OLS method. Study the
|
||||
dependence on $\lambda$.
|
||||
|
||||
\paragraph{Part c: Writing your own gradient descent code.}
|
||||
Replace now the analytical expressions for the optimal parameters
|
||||
$\bm{\theta}$ with your own gradient descent code. In this exercise we
|
||||
focus only on the simplest gradient descent approach with a fixed
|
||||
learning rate (see the exercises from week 37 and the lecture notes
|
||||
from week 36).
|
||||
|
||||
Study and compare your results from parts a) and b) with your gradient
|
||||
descent approch. Discuss in particular the role of the learning rate.
|
||||
|
||||
\paragraph{Part d: Including momentum and more advanced ways to update the learning the rate.}
|
||||
We keep our focus on OLS and Ridge regression and update our code for
|
||||
the gradient descent method by including \textbf{momentum}, \textbf{ADAgrad},
|
||||
\textbf{RMSprop} and \textbf{ADAM} as methods fro iteratively updating your learning
|
||||
rate. Discuss the results and compare the different methods applied to
|
||||
the one-dimensional Runge function. The lecture notes from week 37 contain several examples on how to implement these methods.
|
||||
|
||||
\paragraph{Part e: Writing our own code for Lasso regression.}
|
||||
LASSO regression (see lecture slides from week 36 and week 37)
|
||||
represents our first encounter with a machine learning method which
|
||||
cannot be solved through analytical expressions (as in OLS and Ridge regression). Use the gradient
|
||||
descent methods you developed in parts c) and d) to solve the LASSO
|
||||
optimization problem. You can compare your results with
|
||||
the functionalities of \textbf{Scikit-Learn}.
|
||||
|
||||
Discuss (critically) your results for the Runge function from OLS,
|
||||
Ridge and LASSO regression using the various gradient descent
|
||||
approaches.
|
||||
|
||||
\paragraph{Part f: Stochastic gradient descent.}
|
||||
Our last gradient step is to include stochastic gradient descent using
|
||||
the same methods to update the learning rates as in parts c-e).
|
||||
Compare and discuss your results with and without stochastic gradient
|
||||
and give a critical assessment of the various methods.
|
||||
|
||||
\paragraph{Part g: Bias-variance trade-off and resampling techniques.}
|
||||
Our aim here is to study the bias-variance trade-off by implementing
|
||||
the \textbf{bootstrap} resampling technique. \textbf{We will only use the simpler
|
||||
ordinary least squares here}.
|
||||
|
||||
With a code which does OLS and includes resampling techniques,
|
||||
we will now discuss the bias-variance trade-off in the context of
|
||||
continuous predictions such as regression. However, many of the
|
||||
intuitions and ideas discussed here also carry over to classification
|
||||
tasks and basically all Machine Learning algorithms.
|
||||
|
||||
Before you perform an analysis of the bias-variance trade-off on your
|
||||
test data, make first a figure similar to Fig.~2.11 of Hastie,
|
||||
Tibshirani, and Friedman. Figure 2.11 of this reference displays only
|
||||
the test and training MSEs. The test MSE can be used to indicate
|
||||
possible regions of low/high bias and variance. You will most likely
|
||||
not get an equally smooth curve! You may also need to increase the
|
||||
polynomial order and play around with the number of data points as
|
||||
well (see also the exercise set from week 35).
|
||||
|
||||
With this result we move on to the bias-variance trade-off analysis.
|
||||
|
||||
Consider a
|
||||
dataset $\mathcal{L}$ consisting of the data
|
||||
$\mathbf{X}_\mathcal{L}=\{(y_j, \boldsymbol{x}_j), j=0\ldots n-1\}$.
|
||||
|
||||
We assume that the true data is generated from a noisy model
|
||||
|
||||
\[
|
||||
\bm{y}=f(\boldsymbol{x}) + \bm{\epsilon}.
|
||||
\]
|
||||
|
||||
Here $\epsilon$ is normally distributed with mean zero and standard
|
||||
deviation $\sigma^2$.
|
||||
|
||||
In our derivation of the ordinary least squares method we defined then
|
||||
an approximation to the function $f$ in terms of the parameters
|
||||
$\bm{\theta}$ and the design matrix $\bm{X}$ which embody our model,
|
||||
that is $\bm{\tilde{y}}=\bm{X}\bm{\theta}$.
|
||||
|
||||
The parameters $\bm{\theta}$ are in turn found by optimizing the mean
|
||||
squared error via the so-called cost function
|
||||
|
||||
\[
|
||||
C(\bm{X},\bm{\theta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\bm{y}-\bm{\tilde{y}})^2\right].
|
||||
\]
|
||||
Here the expected value $\mathbb{E}$ is the sample value.
|
||||
|
||||
Show that you can rewrite this in terms of a term which contains the
|
||||
variance of the model itself (the so-called variance term), a term
|
||||
which measures the deviation from the true data and the mean value of
|
||||
the model (the bias term) and finally the variance of the noise.
|
||||
|
||||
That is, show that
|
||||
\[
|
||||
\mathbb{E}\left[(\bm{y}-\bm{\tilde{y}})^2\right]=\mathrm{Bias}[\tilde{y}]+\mathrm{var}[\tilde{y}]+\sigma^2,
|
||||
\]
|
||||
with (we approximate $f(\bm{x})\approx \bm{y}$)
|
||||
\[
|
||||
\mathrm{Bias}[\tilde{y}]=\mathbb{E}\left[\left(\bm{y}-\mathbb{E}\left[\bm{\tilde{y}}\right]\right)^2\right],
|
||||
\]
|
||||
and
|
||||
\[
|
||||
\mathrm{var}[\tilde{y}]=\mathbb{E}\left[\left(\tilde{\bm{y}}-\mathbb{E}\left[\bm{\tilde{y}}\right]\right)^2\right]=\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\bm{\tilde{y}}\right])^2.
|
||||
\]
|
||||
|
||||
\textbf{Important note}: Since the function $f(x)$ is unknown, in order to be able to evalute the bias, we replace $f(\bm{x})$ in the expression for the bias with $\bm{y}$.
|
||||
|
||||
The answer to this exercise should be included in the theory part of
|
||||
the report. This exercise is also part of the weekly exercises of
|
||||
week 38. Explain what the terms mean and discuss their
|
||||
interpretations.
|
||||
|
||||
Perform then a bias-variance analysis of the Runge function by
|
||||
studying the MSE value as function of the complexity of your model.
|
||||
|
||||
Discuss the bias and variance trade-off as function
|
||||
of your model complexity (the degree of the polynomial) and the number
|
||||
of data points, and possibly also your training and test data using the \textbf{bootstrap} resampling method.
|
||||
You can follow the code example in the jupyter-book at \href{{https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/chapter3.html#the-bias-variance-tradeoff}}{\nolinkurl{https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/chapter3.html\#the-bias-variance-tradeoff}}.
|
||||
|
||||
\paragraph{Part h): Cross-validation as resampling techniques, adding more complexity.}
|
||||
The aim here is to implement another widely popular
|
||||
resampling technique, the so-called cross-validation method.
|
||||
|
||||
Implement the $k$-fold cross-validation algorithm (feel free to use
|
||||
the functionality of \textbf{Scikit-Learn} or write your own code) and
|
||||
evaluate again the MSE function resulting from the test folds.
|
||||
|
||||
Compare the MSE you get from your cross-validation code with the one
|
||||
you got from your \textbf{bootstrap} code from the previous exercise. Comment and interpret your results.
|
||||
|
||||
In addition to using the ordinary least squares method, you should
|
||||
include both Ridge and Lasso regression in the final analysis.
|
||||
|
||||
\subsection{Background literature}
|
||||
|
||||
\begin{enumerate}
|
||||
\item For a discussion and derivation of the variances and mean squared errors using linear regression, see the \href{{https://arxiv.org/abs/1509.09169}}{Lecture notes on ridge regression by Wessel N. van Wieringen}
|
||||
|
||||
\item The textbook of \href{{https://www.springer.com/gp/book/9780387848570}}{Trevor Hastie, Robert Tibshirani, Jerome H. Friedman, The Elements of Statistical Learning, Springer}, chapters 3 and 7 are the most relevant ones for the analysis here.
|
||||
\end{enumerate}
|
||||
|
||||
\noindent
|
||||
\subsection{Introduction to numerical projects}
|
||||
|
||||
Here follows a brief recipe and recommendation on how to answer the various questions when preparing your answers.
|
||||
|
||||
\begin{itemize}
|
||||
\item Give a short description of the nature of the problem and the eventual numerical methods you have used.
|
||||
|
||||
\item Describe the algorithm you have used and/or developed. Here you may find it convenient to use pseudocoding. In many cases you can describe the algorithm in the program itself.
|
||||
|
||||
\item Include the source code of your program. Comment your program properly. You should have the code at your GitHub/GitLab link. You can also place the code in an appendix of your report.
|
||||
|
||||
\item If possible, try to find analytic solutions, or known limits in order to test your program when developing the code.
|
||||
|
||||
\item Include your results either in figure form or in a table. Remember to label your results. All tables and figures should have relevant captions and labels on the axes.
|
||||
|
||||
\item Try to evaluate the reliabilty and numerical stability/precision of your results. If possible, include a qualitative and/or quantitative discussion of the numerical stability, eventual loss of precision etc.
|
||||
|
||||
\item Try to give an interpretation of you results in your answers to the problems.
|
||||
|
||||
\item Critique: if possible include your comments and reflections about the exercise, whether you felt you learnt something, ideas for improvements and other thoughts you've made when solving the exercise. We wish to keep this course at the interactive level and your comments can help us improve it.
|
||||
|
||||
\item Try to establish a practice where you log your work at the computerlab. You may find such a logbook very handy at later stages in your work, especially when you don't properly remember what a previous test version of your program did. Here you could also record the time spent on solving the exercise, various algorithms you may have tested or other topics which you feel worthy of mentioning.
|
||||
\end{itemize}
|
||||
|
||||
\noindent
|
||||
\subsection{Format for electronic delivery of report and programs}
|
||||
|
||||
The preferred format for the report is a PDF file. You can also use DOC or postscript formats or as an ipython notebook file. As programming language we prefer that you choose between C/C++, Fortran2008, Julia or Python. The following prescription should be followed when preparing the report:
|
||||
|
||||
\begin{itemize}
|
||||
\item Use Canvas to hand in your projects, log in at \href{{https://www.uio.no/english/services/it/education/canvas/}}{\nolinkurl{https://www.uio.no/english/services/it/education/canvas/}} with your normal UiO username and password.
|
||||
|
||||
\item Upload \textbf{only} the report file or the link to your GitHub/GitLab or similar typo of repos! For the source code file(s) you have developed please provide us with your link to your GitHub/GitLab or similar domain. The report file should include all of your discussions and a list of the codes you have developed. Do not include library files which are available at the course homepage, unless you have made specific changes to them.
|
||||
|
||||
\item In your GitHub/GitLab or similar repository, please include a folder which contains selected results. These can be in the form of output from your code for a selected set of runs and input parameters.
|
||||
\end{itemize}
|
||||
|
||||
\noindent
|
||||
Finally,
|
||||
we encourage you to collaborate. Optimal working groups consist of
|
||||
2-3 students. You can then hand in a common report.
|
||||
|
||||
\subsection{Software and needed installations}
|
||||
|
||||
If you have Python installed (we recommend Python3) and you feel pretty familiar with installing different packages,
|
||||
we recommend that you install the following Python packages via \textbf{pip} as
|
||||
\begin{enumerate}
|
||||
\item pip install numpy scipy matplotlib ipython scikit-learn tensorflow sympy pandas pillow
|
||||
\end{enumerate}
|
||||
|
||||
\noindent
|
||||
For Python3, replace \textbf{pip} with \textbf{pip3}.
|
||||
|
||||
See below for a discussion of \textbf{tensorflow} and \textbf{scikit-learn}.
|
||||
|
||||
For OSX users we recommend also, after having installed Xcode, to install \textbf{brew}. Brew allows
|
||||
for a seamless installation of additional software via for example
|
||||
\begin{enumerate}
|
||||
\item brew install python3
|
||||
\end{enumerate}
|
||||
|
||||
\noindent
|
||||
For Linux users, with its variety of distributions like for example the widely popular Ubuntu distribution
|
||||
you can use \textbf{pip} as well and simply install Python as
|
||||
\begin{enumerate}
|
||||
\item sudo apt-get install python3 (or python for python2.7)
|
||||
\end{enumerate}
|
||||
|
||||
\noindent
|
||||
etc etc.
|
||||
|
||||
If you don't want to install various Python packages with their dependencies separately, we recommend two widely used distrubutions which set up all relevant dependencies for Python, namely
|
||||
\begin{enumerate}
|
||||
\item \href{{https://docs.anaconda.com/}}{Anaconda} Anaconda is an open source distribution of the Python and R programming languages for large-scale data processing, predictive analytics, and scientific computing, that aims to simplify package management and deployment. Package versions are managed by the package management system \textbf{conda}
|
||||
|
||||
\item \href{{https://www.enthought.com/product/canopy/}}{Enthought canopy} is a Python distribution for scientific and analytic computing distribution and analysis environment, available for free and under a commercial license.
|
||||
\end{enumerate}
|
||||
|
||||
\noindent
|
||||
Popular software packages written in Python for ML are
|
||||
|
||||
\begin{itemize}
|
||||
\item \href{{http://scikit-learn.org/stable/}}{Scikit-learn},
|
||||
|
||||
\item \href{{https://www.tensorflow.org/}}{Tensorflow},
|
||||
|
||||
\item \href{{http://pytorch.org/}}{PyTorch} and
|
||||
|
||||
\item \href{{https://keras.io/}}{Keras}.
|
||||
\end{itemize}
|
||||
|
||||
\noindent
|
||||
These are all freely available at their respective GitHub sites. They
|
||||
encompass communities of developers in the thousands or more. And the number
|
||||
of code developers and contributors keeps increasing.
|
||||
|
||||
|
||||
% ------------------- end of main content ---------------
|
||||
|
||||
% #ifdef PREAMBLE
|
||||
\end{document}
|
||||
% #endif
|
||||
|
||||
@@ -0,0 +1,476 @@
|
||||
%%
|
||||
%% Automatically generated file from DocOnce source
|
||||
%% (https://github.com/doconce/doconce/)
|
||||
%% doconce format latex Project1.do.txt --print_latex_style=trac --latex_admon=paragraph
|
||||
%%
|
||||
|
||||
|
||||
%-------------------- begin preamble ----------------------
|
||||
|
||||
\documentclass[%
|
||||
oneside, % oneside: electronic viewing, twoside: printing
|
||||
final, % draft: marks overfull hboxes, figures with paths
|
||||
10pt]{article}
|
||||
|
||||
\listfiles % print all files needed to compile this document
|
||||
|
||||
\usepackage{relsize,makeidx,color,setspace,amsmath,amsfonts,amssymb}
|
||||
\usepackage[table]{xcolor}
|
||||
\usepackage{bm,ltablex,microtype}
|
||||
|
||||
\usepackage[pdftex]{graphicx}
|
||||
|
||||
\usepackage[T1]{fontenc}
|
||||
%\usepackage[latin1]{inputenc}
|
||||
\usepackage{ucs}
|
||||
\usepackage[utf8x]{inputenc}
|
||||
|
||||
\usepackage{lmodern} % Latin Modern fonts derived from Computer Modern
|
||||
|
||||
% Hyperlinks in PDF:
|
||||
\definecolor{linkcolor}{rgb}{0,0,0.4}
|
||||
\usepackage{hyperref}
|
||||
\hypersetup{
|
||||
breaklinks=true,
|
||||
colorlinks=true,
|
||||
linkcolor=linkcolor,
|
||||
urlcolor=linkcolor,
|
||||
citecolor=black,
|
||||
filecolor=black,
|
||||
%filecolor=blue,
|
||||
pdfmenubar=true,
|
||||
pdftoolbar=true,
|
||||
bookmarksdepth=3 % Uncomment (and tweak) for PDF bookmarks with more levels than the TOC
|
||||
}
|
||||
%\hyperbaseurl{} % hyperlinks are relative to this root
|
||||
|
||||
\setcounter{tocdepth}{2} % levels in table of contents
|
||||
|
||||
% prevent orhpans and widows
|
||||
\clubpenalty = 10000
|
||||
\widowpenalty = 10000
|
||||
|
||||
% --- end of standard preamble for documents ---
|
||||
|
||||
|
||||
% insert custom LaTeX commands...
|
||||
|
||||
\raggedbottom
|
||||
\makeindex
|
||||
\usepackage[totoc]{idxlayout} % for index in the toc
|
||||
\usepackage[nottoc]{tocbibind} % for references/bibliography in the toc
|
||||
|
||||
%-------------------- end preamble ----------------------
|
||||
|
||||
\begin{document}
|
||||
|
||||
% matching end for #ifdef PREAMBLE
|
||||
|
||||
\newcommand{\exercisesection}[1]{\subsection*{#1}}
|
||||
|
||||
|
||||
% ------------------- main content ----------------------
|
||||
|
||||
|
||||
|
||||
% ----------------- title -------------------------
|
||||
|
||||
\thispagestyle{empty}
|
||||
|
||||
\begin{center}
|
||||
{\LARGE\bf
|
||||
\begin{spacing}{1.25}
|
||||
Project 1 on Machine Learning, deadline October 6 (midnight), 2025
|
||||
\end{spacing}
|
||||
}
|
||||
\end{center}
|
||||
|
||||
% ----------------- author(s) -------------------------
|
||||
|
||||
\begin{center}
|
||||
{\bf Data Analysis and Machine Learning FYS-STK3155/FYS4155}
|
||||
\end{center}
|
||||
|
||||
\begin{center}
|
||||
% List of all institutions:
|
||||
\centerline{{\small University of Oslo, Norway}}
|
||||
\end{center}
|
||||
|
||||
% ----------------- end author(s) -------------------------
|
||||
|
||||
% --- begin date ---
|
||||
\begin{center}
|
||||
September 2
|
||||
\end{center}
|
||||
% --- end date ---
|
||||
|
||||
\vspace{1cm}
|
||||
|
||||
|
||||
\subsection*{Preamble: Note on writing reports, using reference material, AI and other tools}
|
||||
|
||||
We want you to answer the three different projects by handing in
|
||||
reports written like a standard scientific/technical report. The
|
||||
links at
|
||||
\href{{https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects}}{\nolinkurl{https://github.com/CompPhysics/MachineLearning/tree/master/doc/Projects}}
|
||||
contain more information. There you can find examples of previous
|
||||
reports, the projects themselves, how we rade reports etc. How to
|
||||
write reports will also be discussed during the various lab
|
||||
sessions. Please do ask us if you are in doubt.
|
||||
|
||||
When using codes and material from other sources, you should refer to
|
||||
these in the bibliography of your report, indicating wherefrom you for
|
||||
example got the code, whether this is from the lecture notes,
|
||||
softwares like Scikit-Learn, TensorFlow, PyTorch or other sources. These sources
|
||||
should always be cited correctly. How to cite some
|
||||
of the libraries is often indicated from their corresponding GitHub
|
||||
sites or websites, see for example how to cite Scikit-Learn at
|
||||
\href{{https://scikit-learn.org/dev/about.html}}{\nolinkurl{https://scikit-learn.org/dev/about.html}}.
|
||||
|
||||
We enocurage you to use tools like
|
||||
\href{{https://openai.com/chatgpt/}}{ChatGPT} or similar in writing the report. If you use for example ChatGPT,
|
||||
please do cite it properly and include (if possible) your questions and answers as an addition to the report. This can
|
||||
be uploaded to for example your website, GitHub/GitLab or similar as supplemental material.
|
||||
|
||||
If you would like to study other data sets, feel free to propose other
|
||||
sets. What we have proposed here are mere suggestions from our
|
||||
side. If you opt for another data set, consider using a set which has
|
||||
been studied in the scientific literature. This makes it easier for
|
||||
you to compare and analyze your results. Comparing with existing
|
||||
results from the scientific literature is also an essential element of
|
||||
the scientific discussion. The University of California at Irvine
|
||||
with its Machine Learning repository at
|
||||
\href{{https://archive.ics.uci.edu/ml/index.php}}{\nolinkurl{https://archive.ics.uci.edu/ml/index.php}} is an excellent site to
|
||||
look up for examples and
|
||||
inspiration. \href{{https://www.kaggle.com/}}{Kaggle.com} is an equally
|
||||
interesting site. Feel free to explore these sites. When selecting
|
||||
other data sets, make sure these are sets used for regression problems
|
||||
(not classification).
|
||||
|
||||
% !split
|
||||
\subsection*{Regression analysis and resampling methods}
|
||||
|
||||
The main aim of this project is to study in more detail various
|
||||
regression methods, including Ordinary Least Squares (OLS) reegression, Ridge regression and LASSO regression.
|
||||
In addition to the scientific part, in this course we want also to
|
||||
give you an experience in writing scientific reports.
|
||||
|
||||
We will study how to fit polynomials to specific
|
||||
one-dimensional functions (feel free to replace the suggested function with more complicated ones).
|
||||
|
||||
We will use Runge's function (see \href{{https://en.wikipedia.org/wiki/Runge%27s_phenomenon}}{\nolinkurl{https://en.wikipedia.org/wiki/Runge\%27s_phenomenon}} for a discussion). The one-dimensional function we will study is
|
||||
\[
|
||||
f(x) = \frac{1}{1+25x^2}.
|
||||
\]
|
||||
|
||||
Our first step will be to perform an OLS regression analysis of this
|
||||
function, trying out a polynomial fit with an $x$ dependence of the
|
||||
form $[x,x^2,\dots]$. You can use a uniform distribution to set up the
|
||||
arrays of values for $x \in [-1,1]$, or alternatively use a fixed step size.
|
||||
Thereafter we will repeat many of the same steps when using the Ridge and Lasso regression methods,
|
||||
introducing thereby a dependence on the hyperparameter (penalty) $\lambda$.
|
||||
|
||||
We will also include bootstrap as a resampling technique in order to
|
||||
study the so-called \textbf{bias-variance tradeoff}. After that we will
|
||||
include the so-called cross-validation technique.
|
||||
|
||||
\paragraph{Part a : Ordinary Least Square (OLS) for the Runge function.}
|
||||
We will generate our own dataset for abovementioned function
|
||||
$\mathrm{Runge}(x)$ function with $x\in [-1,1]$. You should explore also the addition
|
||||
of an added stochastic noise to this function using the normal
|
||||
distribution $N(0,1)$.
|
||||
|
||||
\emph{Write your own code} (using for example the pseudoinverse function \textbf{pinv} from \textbf{Numpy} ) and perform a standard \textbf{ordinary least square regression}
|
||||
analysis using polynomials in $x$ up to order $15$ or higher. Explore the dependence on the number of data points and the polynomial degree.
|
||||
|
||||
Evaluate the mean Squared error (MSE)
|
||||
|
||||
\[ MSE(\bm{y},\tilde{\bm{y}}) = \frac{1}{n}
|
||||
\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2,
|
||||
\]
|
||||
|
||||
and the $R^2$ score function. If $\tilde{\bm{y}}_i$ is the predicted
|
||||
value of the $i-th$ sample and $y_i$ is the corresponding true value,
|
||||
then the score $R^2$ is defined as
|
||||
|
||||
\[
|
||||
R^2(\bm{y}, \tilde{\bm{y}}) = 1 - \frac{\sum_{i=0}^{n - 1} (y_i - \tilde{y}_i)^2}{\sum_{i=0}^{n - 1} (y_i - \bar{y})^2},
|
||||
\]
|
||||
|
||||
where we have defined the mean value of $\bm{y}$ as
|
||||
|
||||
\[
|
||||
\bar{y} = \frac{1}{n} \sum_{i=0}^{n - 1} y_i.
|
||||
\]
|
||||
|
||||
Plot the resulting scores (MSE and R$^2$) as functions of the polynomial degree (here up to polymial degree 15).
|
||||
Plot also the parameters $\theta$ as you increase the order of the polynomial. Comment your results.
|
||||
|
||||
Your code has to include a scaling/centering of the data (for example by
|
||||
subtracting the mean value), and
|
||||
a split of the data in training and test data. For the scaling you can
|
||||
either write your own code or use for example the function for
|
||||
splitting training data provided by the library \textbf{Scikit-Learn} (make
|
||||
sure you have installed it). This function is called
|
||||
$train\_test\_split$. \textbf{You should present a critical discussion of why and how you have scaled or not scaled the data}.
|
||||
|
||||
It is normal in essentially all Machine Learning studies to split the
|
||||
data in a training set and a test set (eventually also an additional
|
||||
validation set). There
|
||||
is no explicit recipe for how much data should be included as training
|
||||
data and say test data. An accepted rule of thumb is to use
|
||||
approximately $2/3$ to $4/5$ of the data as training data.
|
||||
|
||||
You can easily reuse the solutions to your exercises from week 35.
|
||||
See also the lecture slides from week 35 and week 36.
|
||||
|
||||
On scaling, we recommend reading the following section from the scikit-learn software description, see \href{{https://scikit-learn.org/stable/auto_examples/preprocessing/plot_all_scaling.html#plot-all-scaling-standard-scaler-section}}{\nolinkurl{https://scikit-learn.org/stable/auto_examples/preprocessing/plot_all_scaling.html\#plot-all-scaling-standard-scaler-section}}.
|
||||
|
||||
\paragraph{Part b: Adding Ridge regression for the Runge function.}
|
||||
Write your own code for the Ridge method as done in the previous
|
||||
exercise. The lecture notes from week 35 and 36 contain more information. Furthermore, the results from the exercise set from week 36 is something you can reuse here.
|
||||
|
||||
Perform the same analysis as you did in the previous exercise but now for different values of $\lambda$. Compare and
|
||||
analyze your results with those obtained in part a) with the OLS method. Study the
|
||||
dependence on $\lambda$.
|
||||
|
||||
\paragraph{Part c: Writing your own gradient descent code.}
|
||||
Replace now the analytical expressions for the optimal parameters
|
||||
$\bm{\theta}$ with your own gradient descent code. In this exercise we
|
||||
focus only on the simplest gradient descent approach with a fixed
|
||||
learning rate (see the exercises from week 37 and the lecture notes
|
||||
from week 36).
|
||||
|
||||
Study and compare your results from parts a) and b) with your gradient
|
||||
descent approch. Discuss in particular the role of the learning rate.
|
||||
|
||||
\paragraph{Part d: Including momentum and more advanced ways to update the learning the rate.}
|
||||
We keep our focus on OLS and Ridge regression and update our code for
|
||||
the gradient descent method by including \textbf{momentum}, \textbf{ADAgrad},
|
||||
\textbf{RMSprop} and \textbf{ADAM} as methods fro iteratively updating your learning
|
||||
rate. Discuss the results and compare the different methods applied to
|
||||
the one-dimensional Runge function. The lecture notes from week 37 contain several examples on how to implement these methods.
|
||||
|
||||
\paragraph{Part e: Writing our own code for Lasso regression.}
|
||||
LASSO regression (see lecture slides from week 36 and week 37)
|
||||
represents our first encounter with a machine learning method which
|
||||
cannot be solved through analytical expressions (as in OLS and Ridge regression). Use the gradient
|
||||
descent methods you developed in parts c) and d) to solve the LASSO
|
||||
optimization problem. You can compare your results with
|
||||
the functionalities of \textbf{Scikit-Learn}.
|
||||
|
||||
Discuss (critically) your results for the Runge function from OLS,
|
||||
Ridge and LASSO regression using the various gradient descent
|
||||
approaches.
|
||||
|
||||
\paragraph{Part f: Stochastic gradient descent.}
|
||||
Our last gradient step is to include stochastic gradient descent using
|
||||
the same methods to update the learning rates as in parts c-e).
|
||||
Compare and discuss your results with and without stochastic gradient
|
||||
and give a critical assessment of the various methods.
|
||||
|
||||
\paragraph{Part g: Bias-variance trade-off and resampling techniques.}
|
||||
Our aim here is to study the bias-variance trade-off by implementing
|
||||
the \textbf{bootstrap} resampling technique. \textbf{We will only use the simpler
|
||||
ordinary least squares here}.
|
||||
|
||||
With a code which does OLS and includes resampling techniques,
|
||||
we will now discuss the bias-variance trade-off in the context of
|
||||
continuous predictions such as regression. However, many of the
|
||||
intuitions and ideas discussed here also carry over to classification
|
||||
tasks and basically all Machine Learning algorithms.
|
||||
|
||||
Before you perform an analysis of the bias-variance trade-off on your
|
||||
test data, make first a figure similar to Fig.~2.11 of Hastie,
|
||||
Tibshirani, and Friedman. Figure 2.11 of this reference displays only
|
||||
the test and training MSEs. The test MSE can be used to indicate
|
||||
possible regions of low/high bias and variance. You will most likely
|
||||
not get an equally smooth curve! You may also need to increase the
|
||||
polynomial order and play around with the number of data points as
|
||||
well (see also the exercise set from week 35).
|
||||
|
||||
With this result we move on to the bias-variance trade-off analysis.
|
||||
|
||||
Consider a
|
||||
dataset $\mathcal{L}$ consisting of the data
|
||||
$\mathbf{X}_\mathcal{L}=\{(y_j, \boldsymbol{x}_j), j=0\ldots n-1\}$.
|
||||
|
||||
We assume that the true data is generated from a noisy model
|
||||
|
||||
\[
|
||||
\bm{y}=f(\boldsymbol{x}) + \bm{\epsilon}.
|
||||
\]
|
||||
|
||||
Here $\epsilon$ is normally distributed with mean zero and standard
|
||||
deviation $\sigma^2$.
|
||||
|
||||
In our derivation of the ordinary least squares method we defined then
|
||||
an approximation to the function $f$ in terms of the parameters
|
||||
$\bm{\theta}$ and the design matrix $\bm{X}$ which embody our model,
|
||||
that is $\bm{\tilde{y}}=\bm{X}\bm{\theta}$.
|
||||
|
||||
The parameters $\bm{\theta}$ are in turn found by optimizing the mean
|
||||
squared error via the so-called cost function
|
||||
|
||||
\[
|
||||
C(\bm{X},\bm{\theta}) =\frac{1}{n}\sum_{i=0}^{n-1}(y_i-\tilde{y}_i)^2=\mathbb{E}\left[(\bm{y}-\bm{\tilde{y}})^2\right].
|
||||
\]
|
||||
Here the expected value $\mathbb{E}$ is the sample value.
|
||||
|
||||
Show that you can rewrite this in terms of a term which contains the
|
||||
variance of the model itself (the so-called variance term), a term
|
||||
which measures the deviation from the true data and the mean value of
|
||||
the model (the bias term) and finally the variance of the noise.
|
||||
|
||||
That is, show that
|
||||
\[
|
||||
\mathbb{E}\left[(\bm{y}-\bm{\tilde{y}})^2\right]=\mathrm{Bias}[\tilde{y}]+\mathrm{var}[\tilde{y}]+\sigma^2,
|
||||
\]
|
||||
with (we approximate $f(\bm{x})\approx \bm{y}$)
|
||||
\[
|
||||
\mathrm{Bias}[\tilde{y}]=\mathbb{E}\left[\left(\bm{y}-\mathbb{E}\left[\bm{\tilde{y}}\right]\right)^2\right],
|
||||
\]
|
||||
and
|
||||
\[
|
||||
\mathrm{var}[\tilde{y}]=\mathbb{E}\left[\left(\tilde{\bm{y}}-\mathbb{E}\left[\bm{\tilde{y}}\right]\right)^2\right]=\frac{1}{n}\sum_i(\tilde{y}_i-\mathbb{E}\left[\bm{\tilde{y}}\right])^2.
|
||||
\]
|
||||
|
||||
\textbf{Important note}: Since the function $f(x)$ is unknown, in order to be able to evalute the bias, we replace $f(\bm{x})$ in the expression for the bias with $\bm{y}$.
|
||||
|
||||
The answer to this exercise should be included in the theory part of
|
||||
the report. This exercise is also part of the weekly exercises of
|
||||
week 38. Explain what the terms mean and discuss their
|
||||
interpretations.
|
||||
|
||||
Perform then a bias-variance analysis of the Runge function by
|
||||
studying the MSE value as function of the complexity of your model.
|
||||
|
||||
Discuss the bias and variance trade-off as function
|
||||
of your model complexity (the degree of the polynomial) and the number
|
||||
of data points, and possibly also your training and test data using the \textbf{bootstrap} resampling method.
|
||||
You can follow the code example in the jupyter-book at \href{{https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/chapter3.html#the-bias-variance-tradeoff}}{\nolinkurl{https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/chapter3.html\#the-bias-variance-tradeoff}}.
|
||||
|
||||
\paragraph{Part h): Cross-validation as resampling techniques, adding more complexity.}
|
||||
The aim here is to implement another widely popular
|
||||
resampling technique, the so-called cross-validation method.
|
||||
|
||||
Implement the $k$-fold cross-validation algorithm (feel free to use
|
||||
the functionality of \textbf{Scikit-Learn} or write your own code) and
|
||||
evaluate again the MSE function resulting from the test folds.
|
||||
|
||||
Compare the MSE you get from your cross-validation code with the one
|
||||
you got from your \textbf{bootstrap} code from the previous exercise. Comment and interpret your results.
|
||||
|
||||
In addition to using the ordinary least squares method, you should
|
||||
include both Ridge and Lasso regression in the final analysis.
|
||||
|
||||
\subsection*{Background literature}
|
||||
|
||||
\begin{enumerate}
|
||||
\item For a discussion and derivation of the variances and mean squared errors using linear regression, see the \href{{https://arxiv.org/abs/1509.09169}}{Lecture notes on ridge regression by Wessel N. van Wieringen}
|
||||
|
||||
\item The textbook of \href{{https://www.springer.com/gp/book/9780387848570}}{Trevor Hastie, Robert Tibshirani, Jerome H. Friedman, The Elements of Statistical Learning, Springer}, chapters 3 and 7 are the most relevant ones for the analysis here.
|
||||
\end{enumerate}
|
||||
|
||||
\noindent
|
||||
\subsection*{Introduction to numerical projects}
|
||||
|
||||
Here follows a brief recipe and recommendation on how to answer the various questions when preparing your answers.
|
||||
|
||||
\begin{itemize}
|
||||
\item Give a short description of the nature of the problem and the eventual numerical methods you have used.
|
||||
|
||||
\item Describe the algorithm you have used and/or developed. Here you may find it convenient to use pseudocoding. In many cases you can describe the algorithm in the program itself.
|
||||
|
||||
\item Include the source code of your program. Comment your program properly. You should have the code at your GitHub/GitLab link. You can also place the code in an appendix of your report.
|
||||
|
||||
\item If possible, try to find analytic solutions, or known limits in order to test your program when developing the code.
|
||||
|
||||
\item Include your results either in figure form or in a table. Remember to label your results. All tables and figures should have relevant captions and labels on the axes.
|
||||
|
||||
\item Try to evaluate the reliabilty and numerical stability/precision of your results. If possible, include a qualitative and/or quantitative discussion of the numerical stability, eventual loss of precision etc.
|
||||
|
||||
\item Try to give an interpretation of you results in your answers to the problems.
|
||||
|
||||
\item Critique: if possible include your comments and reflections about the exercise, whether you felt you learnt something, ideas for improvements and other thoughts you've made when solving the exercise. We wish to keep this course at the interactive level and your comments can help us improve it.
|
||||
|
||||
\item Try to establish a practice where you log your work at the computerlab. You may find such a logbook very handy at later stages in your work, especially when you don't properly remember what a previous test version of your program did. Here you could also record the time spent on solving the exercise, various algorithms you may have tested or other topics which you feel worthy of mentioning.
|
||||
\end{itemize}
|
||||
|
||||
\noindent
|
||||
\subsection*{Format for electronic delivery of report and programs}
|
||||
|
||||
The preferred format for the report is a PDF file. You can also use DOC or postscript formats or as an ipython notebook file. As programming language we prefer that you choose between C/C++, Fortran2008, Julia or Python. The following prescription should be followed when preparing the report:
|
||||
|
||||
\begin{itemize}
|
||||
\item Use Canvas to hand in your projects, log in at \href{{https://www.uio.no/english/services/it/education/canvas/}}{\nolinkurl{https://www.uio.no/english/services/it/education/canvas/}} with your normal UiO username and password.
|
||||
|
||||
\item Upload \textbf{only} the report file or the link to your GitHub/GitLab or similar typo of repos! For the source code file(s) you have developed please provide us with your link to your GitHub/GitLab or similar domain. The report file should include all of your discussions and a list of the codes you have developed. Do not include library files which are available at the course homepage, unless you have made specific changes to them.
|
||||
|
||||
\item In your GitHub/GitLab or similar repository, please include a folder which contains selected results. These can be in the form of output from your code for a selected set of runs and input parameters.
|
||||
\end{itemize}
|
||||
|
||||
\noindent
|
||||
Finally,
|
||||
we encourage you to collaborate. Optimal working groups consist of
|
||||
2-3 students. You can then hand in a common report.
|
||||
|
||||
\subsection*{Software and needed installations}
|
||||
|
||||
If you have Python installed (we recommend Python3) and you feel pretty familiar with installing different packages,
|
||||
we recommend that you install the following Python packages via \textbf{pip} as
|
||||
\begin{enumerate}
|
||||
\item pip install numpy scipy matplotlib ipython scikit-learn tensorflow sympy pandas pillow
|
||||
\end{enumerate}
|
||||
|
||||
\noindent
|
||||
For Python3, replace \textbf{pip} with \textbf{pip3}.
|
||||
|
||||
See below for a discussion of \textbf{tensorflow} and \textbf{scikit-learn}.
|
||||
|
||||
For OSX users we recommend also, after having installed Xcode, to install \textbf{brew}. Brew allows
|
||||
for a seamless installation of additional software via for example
|
||||
\begin{enumerate}
|
||||
\item brew install python3
|
||||
\end{enumerate}
|
||||
|
||||
\noindent
|
||||
For Linux users, with its variety of distributions like for example the widely popular Ubuntu distribution
|
||||
you can use \textbf{pip} as well and simply install Python as
|
||||
\begin{enumerate}
|
||||
\item sudo apt-get install python3 (or python for python2.7)
|
||||
\end{enumerate}
|
||||
|
||||
\noindent
|
||||
etc etc.
|
||||
|
||||
If you don't want to install various Python packages with their dependencies separately, we recommend two widely used distrubutions which set up all relevant dependencies for Python, namely
|
||||
\begin{enumerate}
|
||||
\item \href{{https://docs.anaconda.com/}}{Anaconda} Anaconda is an open source distribution of the Python and R programming languages for large-scale data processing, predictive analytics, and scientific computing, that aims to simplify package management and deployment. Package versions are managed by the package management system \textbf{conda}
|
||||
|
||||
\item \href{{https://www.enthought.com/product/canopy/}}{Enthought canopy} is a Python distribution for scientific and analytic computing distribution and analysis environment, available for free and under a commercial license.
|
||||
\end{enumerate}
|
||||
|
||||
\noindent
|
||||
Popular software packages written in Python for ML are
|
||||
|
||||
\begin{itemize}
|
||||
\item \href{{http://scikit-learn.org/stable/}}{Scikit-learn},
|
||||
|
||||
\item \href{{https://www.tensorflow.org/}}{Tensorflow},
|
||||
|
||||
\item \href{{http://pytorch.org/}}{PyTorch} and
|
||||
|
||||
\item \href{{https://keras.io/}}{Keras}.
|
||||
\end{itemize}
|
||||
|
||||
\noindent
|
||||
These are all freely available at their respective GitHub sites. They
|
||||
encompass communities of developers in the thousands or more. And the number
|
||||
of code developers and contributors keeps increasing.
|
||||
|
||||
|
||||
% ------------------- end of main content ---------------
|
||||
|
||||
\end{document}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
This IPython notebook Project1.ipynb does not require any additional
|
||||
programs.
|
||||
Binary file not shown.
Reference in New Issue
Block a user