This commit is contained in:
Morten Hjorth-Jensen
2024-09-10 12:06:15 +02:00
parent 4a3985783f
commit 4e5c11dfa3
143 changed files with 2073 additions and 6207 deletions
@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "6c8c59f8",
"id": "34471c23",
"metadata": {
"editable": true
},
@@ -14,7 +14,7 @@
},
{
"cell_type": "markdown",
"id": "24af4cf5",
"id": "947e566c",
"metadata": {
"editable": true
},
@@ -27,7 +27,7 @@
},
{
"cell_type": "markdown",
"id": "2971d68f",
"id": "91b68c62",
"metadata": {
"editable": true
},
@@ -60,7 +60,7 @@
},
{
"cell_type": "markdown",
"id": "eba3be6a",
"id": "6161e2ec",
"metadata": {
"editable": true
},
@@ -89,7 +89,7 @@
},
{
"cell_type": "markdown",
"id": "50da25e5",
"id": "d598eabc",
"metadata": {
"editable": true
},
@@ -111,7 +111,7 @@
},
{
"cell_type": "markdown",
"id": "7757fb0c",
"id": "6f94bf24",
"metadata": {
"editable": true
},
@@ -126,7 +126,7 @@
},
{
"cell_type": "markdown",
"id": "83fbdb79",
"id": "2d87bd3d",
"metadata": {
"editable": true
},
@@ -159,7 +159,7 @@
{
"cell_type": "code",
"execution_count": 1,
"id": "39b900ad",
"id": "3ea47a48",
"metadata": {
"collapsed": false,
"editable": true
@@ -176,8 +176,7 @@
"from random import random, seed\n",
"\n",
"fig = plt.figure()\n",
"ax = fig.gca(projection='3d')\n",
"\n",
"ax = fig.add_subplot(projection = '3d')\n",
"# Make data.\n",
"x = np.arange(0, 1, 0.05)\n",
"y = np.arange(0, 1, 0.05)\n",
@@ -211,7 +210,7 @@
},
{
"cell_type": "markdown",
"id": "ef99fea4",
"id": "681ae810",
"metadata": {
"editable": true
},
@@ -221,7 +220,7 @@
},
{
"cell_type": "markdown",
"id": "b1a7f51d",
"id": "0bf63b42",
"metadata": {
"editable": true
},
@@ -243,7 +242,7 @@
},
{
"cell_type": "markdown",
"id": "d4bd58e3",
"id": "bda22453",
"metadata": {
"editable": true
},
@@ -256,7 +255,7 @@
},
{
"cell_type": "markdown",
"id": "32f767b4",
"id": "7cc65393",
"metadata": {
"editable": true
},
@@ -268,7 +267,7 @@
},
{
"cell_type": "markdown",
"id": "bae0d9d6",
"id": "0e5859cb",
"metadata": {
"editable": true
},
@@ -280,7 +279,7 @@
},
{
"cell_type": "markdown",
"id": "8f0b2ec9",
"id": "5969702e",
"metadata": {
"editable": true
},
@@ -290,7 +289,7 @@
},
{
"cell_type": "markdown",
"id": "8a455920",
"id": "6fc7cf78",
"metadata": {
"editable": true
},
@@ -302,7 +301,7 @@
},
{
"cell_type": "markdown",
"id": "3f7ffe3e",
"id": "fad83915",
"metadata": {
"editable": true
},
@@ -333,7 +332,7 @@
},
{
"cell_type": "markdown",
"id": "cf4d70a4",
"id": "5ca41535",
"metadata": {
"editable": true
},
@@ -351,7 +350,7 @@
},
{
"cell_type": "markdown",
"id": "8cc60702",
"id": "eafba188",
"metadata": {
"editable": true
},
@@ -368,7 +367,7 @@
},
{
"cell_type": "markdown",
"id": "bd9d1dd3",
"id": "ca22f9c3",
"metadata": {
"editable": true
},
@@ -384,7 +383,7 @@
},
{
"cell_type": "markdown",
"id": "03fab7b5",
"id": "62e8987d",
"metadata": {
"editable": true
},
@@ -396,7 +395,7 @@
},
{
"cell_type": "markdown",
"id": "009b7fb9",
"id": "3a837289",
"metadata": {
"editable": true
},
@@ -407,7 +406,7 @@
},
{
"cell_type": "markdown",
"id": "5bf0a0d5",
"id": "c4103004",
"metadata": {
"editable": true
},
@@ -419,7 +418,7 @@
},
{
"cell_type": "markdown",
"id": "52c48acb",
"id": "8802447b",
"metadata": {
"editable": true
},
@@ -431,7 +430,7 @@
},
{
"cell_type": "markdown",
"id": "3158357a",
"id": "b129e460",
"metadata": {
"editable": true
},
@@ -443,7 +442,7 @@
},
{
"cell_type": "markdown",
"id": "021253bc",
"id": "91bb15a6",
"metadata": {
"editable": true
},
@@ -454,7 +453,7 @@
},
{
"cell_type": "markdown",
"id": "9e89d5fe",
"id": "d6ac051f",
"metadata": {
"editable": true
},
@@ -466,7 +465,7 @@
},
{
"cell_type": "markdown",
"id": "5f79916c",
"id": "6d2b1477",
"metadata": {
"editable": true
},
@@ -479,7 +478,7 @@
},
{
"cell_type": "markdown",
"id": "a6e62eff",
"id": "ffb13255",
"metadata": {
"editable": true
},
@@ -491,7 +490,7 @@
},
{
"cell_type": "markdown",
"id": "7e833f14",
"id": "031020e1",
"metadata": {
"editable": true
},
@@ -501,7 +500,7 @@
},
{
"cell_type": "markdown",
"id": "14ef5a97",
"id": "ba8af75f",
"metadata": {
"editable": true
},
@@ -513,7 +512,7 @@
},
{
"cell_type": "markdown",
"id": "a9443b1d",
"id": "c5c4d7e6",
"metadata": {
"editable": true
},
@@ -524,7 +523,7 @@
},
{
"cell_type": "markdown",
"id": "ff0c2a46",
"id": "62633d34",
"metadata": {
"editable": true
},
@@ -557,7 +556,7 @@
},
{
"cell_type": "markdown",
"id": "4c8ea78a",
"id": "18f1b5b0",
"metadata": {
"editable": true
},
@@ -569,7 +568,7 @@
},
{
"cell_type": "markdown",
"id": "1d119b3e",
"id": "76dc98a9",
"metadata": {
"editable": true
},
@@ -588,7 +587,7 @@
},
{
"cell_type": "markdown",
"id": "b9782b21",
"id": "ea4f7a95",
"metadata": {
"editable": true
},
@@ -600,7 +599,7 @@
},
{
"cell_type": "markdown",
"id": "457bd0ae",
"id": "21225b69",
"metadata": {
"editable": true
},
@@ -614,7 +613,7 @@
},
{
"cell_type": "markdown",
"id": "fbc011e0",
"id": "c259efb1",
"metadata": {
"editable": true
},
@@ -626,7 +625,7 @@
},
{
"cell_type": "markdown",
"id": "5bb40600",
"id": "2bf21344",
"metadata": {
"editable": true
},
@@ -636,7 +635,7 @@
},
{
"cell_type": "markdown",
"id": "e5aebe0a",
"id": "2b5dd2b9",
"metadata": {
"editable": true
},
@@ -648,7 +647,7 @@
},
{
"cell_type": "markdown",
"id": "6f243211",
"id": "90c065aa",
"metadata": {
"editable": true
},
@@ -658,7 +657,7 @@
},
{
"cell_type": "markdown",
"id": "850e1403",
"id": "c29e34b2",
"metadata": {
"editable": true
},
@@ -670,7 +669,7 @@
},
{
"cell_type": "markdown",
"id": "86066fab",
"id": "f928062b",
"metadata": {
"editable": true
},
@@ -689,7 +688,7 @@
},
{
"cell_type": "markdown",
"id": "aedb0de8",
"id": "ba0fe639",
"metadata": {
"editable": true
},
@@ -712,7 +711,7 @@
},
{
"cell_type": "markdown",
"id": "09e42708",
"id": "82f076b3",
"metadata": {
"editable": true
},
@@ -740,7 +739,7 @@
{
"cell_type": "code",
"execution_count": 2,
"id": "a7412176",
"id": "b5f7adac",
"metadata": {
"collapsed": false,
"editable": true
@@ -752,7 +751,7 @@
},
{
"cell_type": "markdown",
"id": "2462a733",
"id": "5e55285e",
"metadata": {
"editable": true
},
@@ -764,7 +763,7 @@
{
"cell_type": "code",
"execution_count": 3,
"id": "e299ff99",
"id": "a66f3a48",
"metadata": {
"collapsed": false,
"editable": true
@@ -790,7 +789,7 @@
},
{
"cell_type": "markdown",
"id": "58bfbdc9",
"id": "1015d4e5",
"metadata": {
"editable": true
},
@@ -815,7 +814,7 @@
},
{
"cell_type": "markdown",
"id": "5c69b9d7",
"id": "23b093c7",
"metadata": {
"editable": true
},
@@ -829,7 +828,7 @@
},
{
"cell_type": "markdown",
"id": "a92b1a41",
"id": "fe5834fa",
"metadata": {
"editable": true
},
@@ -859,7 +858,7 @@
},
{
"cell_type": "markdown",
"id": "3da35987",
"id": "a238d8fe",
"metadata": {
"editable": true
},
@@ -881,7 +880,7 @@
},
{
"cell_type": "markdown",
"id": "c03bf204",
"id": "e2cae8ee",
"metadata": {
"editable": true
},
@@ -1,348 +0,0 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "515c9474",
"metadata": {
"editable": true
},
"source": [
"<!-- HTML file automatically generated from DocOnce source (https://github.com/doconce/doconce/)\n",
"doconce format html Project2.do.txt -->\n",
"<!-- dom:TITLE: Project 2 on Machine Learning, deadline November 17 (Midnight) -->"
]
},
{
"cell_type": "markdown",
"id": "890dcb04",
"metadata": {
"editable": true
},
"source": [
"# Project 2 on Machine Learning, deadline November 17 (Midnight)\n",
"**[Data Analysis and Machine Learning FYS-STK3155/FYS4155](http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html)**, Department of Physics, University of Oslo, Norway\n",
"\n",
"Date: **Nov 13, 2023**\n",
"\n",
"Copyright 1999-2023, [Data Analysis and Machine Learning FYS-STK3155/FYS4155](http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html). Released under CC Attribution-NonCommercial 4.0 license"
]
},
{
"cell_type": "markdown",
"id": "8cbf512f",
"metadata": {
"editable": true
},
"source": [
"## Classification and Regression, from linear and logistic regression to neural networks\n",
"\n",
"The main aim of this project is to study both classification and\n",
"regression problems by developing our own feed-forward neural network\n",
"(FFNN) code. We can reuse the regression algorithms studied in project\n",
"1. We will also include logistic regression for classification\n",
"problems and write our own FFNN code for studying both regression and\n",
"classification problems. The codes developed in project 1, including\n",
"bootstrap **and/or** cross-validation as well as the computation of the\n",
"mean-squared error and/or the $R2$ or the accuracy score\n",
"(classification problems) functions can also be utilized in the\n",
"present analysis.\n",
"\n",
"The data sets that we propose here are (the default sets)\n",
"\n",
"* Regression (fitting a continuous function). In this part you will need to bring back your results from project 1 and compare these with what you get from your Neural Network code to be developed here. The data sets could be\n",
"\n",
"a. A simple one-dimensional function or the Franke function or the terrain data from project 1, or data sets your propose. It could be a simpler function than the Franke function. We recommend testing a simpler function (see below). But if you wish to try more complex function, feel free to do so.\n",
"\n",
"* Classification. Here you will also need to develop a Logistic regression code that you will use to compare with the Neural Network code. The data set we propose are the so-called [Wisconsin Breat Cancer Data](https://www.kaggle.com/uciml/breast-cancer-wisconsin-data) data set of images representing various features of tumors. These are discussed intensively in the lecture notes, see for example the slides from [week 41](https://compphysics.github.io/MachineLearning/doc/pub/week41/html/week41.html). A longer explanation with links to the scientific literature can be found at the [Machine Learning repository of the University of California at Irvine](https://archive.ics.uci.edu/ml/datasets/Breast+Cancer+Wisconsin+%28Diagnostic%29). Feel free to consult this site and the pertinent literature.\n",
"\n",
"You can find more information about this at the [Scikit-Learn site](https://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_breast_cancer.html) or at the [University of California at Irvine](https://archive.ics.uci.edu/ml/datasets/breast+cancer+wisconsin+(original)). \n",
"\n",
"However, if you would like to study other data sets, feel free to\n",
"propose other sets. What we list 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.\n",
"\n",
"We will start with a regression problem and we will reuse our codes from project 1 starting with writing our own Stochastic Gradient Descent (SGD) code."
]
},
{
"cell_type": "markdown",
"id": "696379a0",
"metadata": {
"editable": true
},
"source": [
"### Part a): Write your own Stochastic Gradient Descent code, first step\n",
"\n",
"In order to get started, we will now replace in our standard ordinary\n",
"least squares (OLS) and Ridge regression codes (from project 1) the\n",
"matrix inversion algorithm with our own gradient descent (GD) and SGD\n",
"codes. You can use the Franke function or the terrain data from\n",
"project 1. **However, we recommend using a simpler function like**\n",
"$f(x)=a_0+a_1x+a_2x^2$ or higher-order one-dimensional polynomials.\n",
"You can obviously test your final codes against for example the Franke\n",
"function.\n",
"\n",
"You should include in your analysis of the GD and SGD codes the following elements\n",
"1. A plain gradient descent with a fixed learning rate (you will need to tune it) using the analytical expression for the gradient.\n",
"\n",
"2. Add momentum to the plain GD code and compare convergence with a fixed learning rate (you may need to tune the learning rate). Keep using the analytical expression for the gradient.\n",
"\n",
"3. Repeat these steps for stochastic gradient descent with mini batches and a given number of epochs. Use a tunable learning rate as discussed in the lectures from weeks 39 and 40. Discuss the results as functions of the various parameters (size of batches, number of epochs etc). Use the analytical gradient.\n",
"\n",
"4. Implement the Adagrad method in order to tune the learning rate. Do this with and without momentum for plain gradient descent and SGD.\n",
"\n",
"5. Add RMSprop and Adam to your library of methods for tuning the learning rate.\n",
"\n",
"The lecture notes from [weeks 39 and 40contain more\n",
"details](https://compphysics.github.io/MachineLearning/doc/pub/week39/html/week39.html) and code examples. Feel free to use these examples.\n",
"1. Replace thereafter your analytical gradient with either **Autograd** or **JAX**\n",
"\n",
"In summary, you should \n",
"perform an analysis of the results for OLS and Ridge regression as\n",
"function of the chosen learning rates, the number of mini-batches and\n",
"epochs as well as algorithm for scaling the learning rate. You can\n",
"also compare your own results with those that can be obtained using\n",
"for example **Scikit-Learn**'s various SGD options. Discuss your\n",
"results. For Ridge regression you need now to study the results as functions of the hyper-parameter $\\lambda$ and \n",
"the learning rate $\\eta$. Discuss your results.\n",
"\n",
"You will need your SGD code for the setup of the Neural Network and\n",
"Logistic Regression codes. You will find the Python [Seaborn\n",
"package](https://seaborn.pydata.org/generated/seaborn.heatmap.html)\n",
"useful when plotting the results as function of the learning rate\n",
"$\\eta$ and the hyper-parameter $\\lambda$ when you use Ridge\n",
"regression.\n",
"\n",
"We recommend reading chapter 8 on optimization from the textbook of [Goodfellow, Bengio and Courville](https://www.deeplearningbook.org/). This chapter contains many useful insights and discussions on the optimization part of machine learning."
]
},
{
"cell_type": "markdown",
"id": "81d48303",
"metadata": {
"editable": true
},
"source": [
"### Part b): Writing your own Neural Network code\n",
"\n",
"Your aim now, and this is the central part of this project, is to\n",
"write your own Feed Forward Neural Network code implementing the back\n",
"propagation algorithm discussed in the lecture slides from [week 40](https://compphysics.github.io/MachineLearning/doc/pub/week41/html/week40.html) and\n",
"[week 41](https://compphysics.github.io/MachineLearning/doc/pub/week41/html/week41.html).\n",
"\n",
"We will focus on a regression problem first and study either the simple second-order polynomial from part a) or the \n",
"Franke function or terrain data (or both or other data sets) from\n",
"project 1.\n",
"\n",
"Discuss again your choice of cost function.\n",
"\n",
"Write an FFNN code for regression with a flexible number of hidden\n",
"layers and nodes using the Sigmoid function as activation function for\n",
"the hidden layers. Initialize the weights using a normal\n",
"distribution. How would you initialize the biases? And which\n",
"activation function would you select for the final output layer?\n",
"\n",
"Train your network and compare the results with those from your OLS and Ridge Regression codes from project 1 if you use the Franke function or the terrain data.\n",
"You should test your results against a similar code using **Scikit-Learn** (see the examples in the above lecture notes from week 41) or **tensorflow/keras**. \n",
"\n",
"Comment your results and give a critical discussion of the results\n",
"obtained with the Linear Regression code and your own Neural Network\n",
"code. \n",
"Make an analysis of the regularization parameters and the learning rates employed to find the optimal MSE and $R2$ scores.\n",
"\n",
"A useful reference on the back progagation algorithm is [Nielsen's\n",
"book](http://neuralnetworksanddeeplearning.com/). It is an excellent\n",
"read."
]
},
{
"cell_type": "markdown",
"id": "db6aebb0",
"metadata": {
"editable": true
},
"source": [
"### Part c): Testing different activation functions\n",
"\n",
"You should now also test different activation functions for the hidden layers. Try out the Sigmoid, the RELU and the Leaky RELU functions and discuss your results. You may also study the way you initialize your weights and biases."
]
},
{
"cell_type": "markdown",
"id": "18314d90",
"metadata": {
"editable": true
},
"source": [
"### Part d): Classification analysis using neural networks\n",
"\n",
"With a well-written code it should now be easy to change the\n",
"activation function for the output layer.\n",
"\n",
"Here we will change the cost function for our neural network code\n",
"developed in parts b) and c) in order to perform a classification analysis. \n",
"\n",
"We will here study the Wisconsin Breast Cancer data set. This is a typical binary classification problem with just one single output, either True or Fale, $0$ or $1$ etc.\n",
"You find more information about this at the [Scikit-Learn\n",
"site](https://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_breast_cancer.html) or at the [University of California\n",
"at Irvine](https://archive.ics.uci.edu/ml/datasets/breast+cancer+wisconsin+(original)). \n",
"\n",
"To measure the performance of our classification problem we use the\n",
"so-called *accuracy* score. The accuracy is as you would expect just\n",
"the number of correctly guessed targets $t_i$ divided by the total\n",
"number of targets, that is"
]
},
{
"cell_type": "markdown",
"id": "dc3021aa",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"\\text{Accuracy} = \\frac{\\sum_{i=1}^n I(t_i = y_i)}{n} ,\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "3c3c5b42",
"metadata": {
"editable": true
},
"source": [
"where $I$ is the indicator function, $1$ if $t_i = y_i$ and $0$\n",
"otherwise if we have a binary classification problem. Here $t_i$\n",
"represents the target and $y_i$ the outputs of your FFNN code and $n$ is simply the number of targets $t_i$.\n",
"\n",
"Discuss your results and give a critical analysis of the various parameters, including hyper-parameters like the learning rates and the regularization parameter $\\lambda$ (as you did in Ridge Regression), various activation functions, number of hidden layers and nodes and activation functions. \n",
"\n",
"As stated in the introduction, it can also be useful to study other\n",
"datasets. \n",
"\n",
"Again, we strongly recommend that you compare your own neural Network\n",
"code for classification and pertinent results against a similar code using **Scikit-Learn** or **tensorflow/keras** or **pytorch**."
]
},
{
"cell_type": "markdown",
"id": "1a493d07",
"metadata": {
"editable": true
},
"source": [
"### Part e): Write your Logistic Regression code, final step\n",
"\n",
"Finally, we want to compare the FFNN code we have developed with\n",
"Logistic regression, that is we wish to compare our neural network\n",
"classification results with the results we can obtain with another\n",
"method.\n",
"\n",
"Define your cost function and the design matrix before you start writing your code.\n",
"Write thereafter a Logistic regression code using your SGD algorithm. You can also use standard gradient descent in this case, with a learning rate as hyper-parameter.\n",
"Study the results as functions of the chosen learning rates.\n",
"Add also an $l_2$ regularization parameter $\\lambda$. Compare your results with those from your FFNN code as well as those obtained using **Scikit-Learn**'s logistic regression functionality.\n",
"\n",
"The weblink here <https://medium.com/ai-in-plain-english/comparison-between-logistic-regression-and-neural-networks-in-classifying-digits-dc5e85cd93c3>compares logistic regression and FFNN using the so-called MNIST data set. You may find several useful hints and ideas from this article."
]
},
{
"cell_type": "markdown",
"id": "649a5380",
"metadata": {
"editable": true
},
"source": [
"### Part f) Critical evaluation of the various algorithms\n",
"\n",
"After all these glorious calculations, you should now summarize the\n",
"various algorithms and come with a critical evaluation of their pros\n",
"and cons. Which algorithm works best for the regression case and which\n",
"is best for the classification case. These codes can also be part of\n",
"your final project 3, but now applied to other data sets."
]
},
{
"cell_type": "markdown",
"id": "804df082",
"metadata": {
"editable": true
},
"source": [
"## Background literature\n",
"\n",
"1. The text of Michael Nielsen is highly recommended, see [Nielsen's book](http://neuralnetworksanddeeplearning.com/). It is an excellent read.\n",
"\n",
"2. [Mehta et al, arXiv 1803.08823](https://arxiv.org/abs/1803.08823), *A high-bias, low-variance introduction to Machine Learning for physicists*, ArXiv:1803.08823.\n",
"\n",
"c. [Goodfellow, Bengio and Courville](https://www.deeplearningbook.org/), *Deep Learning*."
]
},
{
"cell_type": "markdown",
"id": "2661bc83",
"metadata": {
"editable": true
},
"source": [
"## Introduction to numerical projects\n",
"\n",
"Here follows a brief recipe and recommendation on how to write a report for each\n",
"project.\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.\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": "e651a157",
"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 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."
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
@@ -1,546 +0,0 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "32bbc99a",
"metadata": {
"editable": true
},
"source": [
"<!-- HTML file automatically generated from DocOnce source (https://github.com/doconce/doconce/)\n",
"doconce format html Project3.do.txt -->\n",
"<!-- dom:TITLE: Project 3 on Machine Learning, deadline December 18 (midnight), 2023 -->"
]
},
{
"cell_type": "markdown",
"id": "21397747",
"metadata": {
"editable": true
},
"source": [
"# Project 3 on Machine Learning, deadline December 18 (midnight), 2023\n",
"**[Data Analysis and Machine Learning FYS-STK3155/FYS4155](http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html)**, Department of Physics, University of Oslo, Norway\n",
"\n",
"Date: **Nov 13, 2023**\n",
"\n",
"Copyright 1999-2023, [Data Analysis and Machine Learning FYS-STK3155/FYS4155](http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html). Released under CC Attribution-NonCommercial 4.0 license"
]
},
{
"cell_type": "markdown",
"id": "a13debcc",
"metadata": {
"editable": true
},
"source": [
"# Paths for project 3"
]
},
{
"cell_type": "markdown",
"id": "9d1f1220",
"metadata": {
"editable": true
},
"source": [
"## Defining the data sets to analyze yourself\n",
"\n",
"For project 3, you can propose own data sets that relate to your research interests or just use existing data sets from say\n",
"1. [Kaggle](https://www.kaggle.com/datasets) \n",
"\n",
"2. The [University of California at Irvine (UCI) with its machine learning repository](https://archive.ics.uci.edu/ml/index.php).\n",
"\n",
"3. Or other sources.\n",
"\n",
"The approach to the analysis of these new data sets should follow to a large extent what you did in projects 1 and 2. That is:\n",
"1. Whether you end up with a regression or a classification problem, you should employ at least two of the methods we have discussed among **linear regression (including Ridge and Lasso)**, **Logistic Regression**, **Neural Networks**, **Convolution Neural Networks**, **Recurrent Neural Networks**, and **Decision Trees, Random Forests, Bagging and Boosting**.\n",
"\n",
"Feel also free to use support vector machines, $k$-means and principal components analysis, although the latter have not been covered during the lectures. This material can be found in the lecture notes.\n",
"\n",
"You could for example explore all of the approaches from decision trees, via bagging and voting classifiers, to random forests, boosting and finally XGboost. If you wish to venture into **convolutional neural networks** or **recurrent neural networks**, or extensions of neural networkds, feel free to do so. You can also study unsupervised methods, although we in this course have mainly paid attention to supervised learning. \n",
"\n",
"For Boosting, feel also free to write your own codes.\n",
"\n",
"1. For project 3, you should feel free to use your own codes from projects 1 and 2, eventually write your own for Decision trees/random forests/bagging/boosting' or use the available functionality of **Scikit-Learn**, **Tensorflow**, PyTorch etc. \n",
"\n",
"2. The estimates you used and tested in projects 1 and 2 should also be included, that is the $R2$-score, **MSE**, confusion matrix, accuracy score, information gain, ROC and Cumulative gains curves and other, cross-validation and/or bootstrap if these are relevant.\n",
"\n",
"3. Similarly, feel free to explore various activations functions in deep learning and various approachs to stochastic gradient descent approaches.\n",
"\n",
"4. If possible, you should link the data sets with exisiting research and analyses thereof. Scientific articles which have used Machine Learning algorithms to analyze the data are highly welcome. Perhaps you can improve previous analyses and even publish a new article? \n",
"\n",
"5. A critical assessment of the methods with ditto perspectives and recommendations is also something you need to include.\n",
"\n",
"All in all, the report should follow the same pattern as the two previous ones, with abstract, introduction, methods, code, results, conclusions etc..\n",
"\n",
"We propose also an alternative to the above. This is a project on using machine learning methods (neural networks mainly) to the solution of ordinary differential equations and partial differential equations, with a final twist on how to diagonalize a symmetric matrix with neural networks.\n",
"\n",
"This is a field with large scientific interest, spanning from studies of turbulence in fluid mechanics and meteorology to the solution of quantum mechanical systems. As reading background you can use the slides [from week 43](https://compphysics.github.io/MachineLearning/doc/pub/week43/html/week42.html) and/or the textbook by [Yadav et al](https://www.springer.com/gp/book/9789401798150)."
]
},
{
"cell_type": "markdown",
"id": "6416060c",
"metadata": {
"editable": true
},
"source": [
"## The basic structure of your project\n",
"\n",
"Here follows a set up on how to structure your report and analyze the data you have opted for."
]
},
{
"cell_type": "markdown",
"id": "18827262",
"metadata": {
"editable": true
},
"source": [
"### Part a)\n",
"\n",
"The first part deals with structuring and reading the data, much along the same lines as done in projects 1 and 2. Explain how the data are produced and place them in a proper context."
]
},
{
"cell_type": "markdown",
"id": "bcee53f4",
"metadata": {
"editable": true
},
"source": [
"### Part b)\n",
"\n",
"You need to include at least two central algorithms, or as an alternative explore methods from decisions tree to bagging, random forests and boosting. Explain the basics of the methods you have chosen to work with. This would be your theory part."
]
},
{
"cell_type": "markdown",
"id": "83ec8275",
"metadata": {
"editable": true
},
"source": [
"### Part c)\n",
"\n",
"Then describe your algorithm and its implementation and tests you have performed."
]
},
{
"cell_type": "markdown",
"id": "2be62c8e",
"metadata": {
"editable": true
},
"source": [
"### Part d)\n",
"\n",
"Then presents your results and findings, link with existing literature and more."
]
},
{
"cell_type": "markdown",
"id": "385e0b16",
"metadata": {
"editable": true
},
"source": [
"### Part e)\n",
"\n",
"Finally, here you should present a critical assessment of the methods you have studied and link your results with the existing literature."
]
},
{
"cell_type": "markdown",
"id": "fbf49165",
"metadata": {
"editable": true
},
"source": [
"## Solving partial differential equations with neural networks\n",
"\n",
"For this variant of project 3, we will assume that you have some\n",
"background in the solution of partial differential equations using\n",
"finite difference schemes. We will study the solution of the diffusion\n",
"equation in one dimension using a standard explicit scheme and neural\n",
"networks to solve the same equations.\n",
"\n",
"For the explicit scheme, you can study for example chapter 10 of the lecture notes in [Computational Physics, FYS3150/4150](https://github.com/CompPhysics/ComputationalPhysics/blob/master/doc/Lectures/lectures2015.pdf) or alternative sources from courses like [MAT-MEK4270](https://www.uio.no/studier/emner/matnat/math/MAT-MEK4270/index.html). For the solution of ordinary and partial differential equations using neural networks, the lectures by [included in the lectures of week 43](https://compphysics.github.io/MachineLearning/doc/pub/week42/html/week43.html) at this course are highly recommended.\n",
"\n",
"For the machine learning part you can use your own code from project 2 or the functionality of for example **Tensorflow/Keras**, **PyTorch** or other libraries such [Physics informed machine learning](https://maziarraissi.github.io/PINNs/)."
]
},
{
"cell_type": "markdown",
"id": "c97df4f9",
"metadata": {
"editable": true
},
"source": [
"### Alternative differential equations\n",
"\n",
"Note that you can replace the one-dimensional diffusion equation discussed below with other sets of either ordinary differential equations or partial differential equations.\n",
"Please discuss such a change with us at the lab."
]
},
{
"cell_type": "markdown",
"id": "ecde0a0e",
"metadata": {
"editable": true
},
"source": [
"### Part a), setting up the problem\n",
"\n",
"The physical problem can be that of the temperature gradient in a rod of length $L=1$ at $x=0$ and $x=1$.\n",
"We are looking at a one-dimensional\n",
"problem"
]
},
{
"cell_type": "markdown",
"id": "56429d7e",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"\\frac{\\partial^2 u(x,t)}{\\partial x^2} =\\frac{\\partial u(x,t)}{\\partial t}, t> 0, x\\in [0,L]\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "c2e49662",
"metadata": {
"editable": true
},
"source": [
"or"
]
},
{
"cell_type": "markdown",
"id": "fd661d63",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"u_{xx} = u_t,\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "a1d77183",
"metadata": {
"editable": true
},
"source": [
"with initial conditions, i.e., the conditions at $t=0$,"
]
},
{
"cell_type": "markdown",
"id": "73d187ef",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"u(x,0)= \\sin{(\\pi x)} \\hspace{0.5cm} 0 < x < L,\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "b9b49da0",
"metadata": {
"editable": true
},
"source": [
"with $L=1$ the length of the $x$-region of interest. The \n",
"boundary conditions are"
]
},
{
"cell_type": "markdown",
"id": "aee685e3",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"u(0,t)= 0 \\hspace{0.5cm} t \\ge 0,\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "96ec18ae",
"metadata": {
"editable": true
},
"source": [
"and"
]
},
{
"cell_type": "markdown",
"id": "57c542b7",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"u(L,t)= 0 \\hspace{0.5cm} t \\ge 0.\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "b6e8e863",
"metadata": {
"editable": true
},
"source": [
"The function $u(x,t)$ can be the temperature gradient of a rod.\n",
"As time increases, the velocity approaches a linear variation with $x$. \n",
"\n",
"We will limit ourselves to the so-called explicit forward Euler algorithm with discretized versions of time given by a forward formula and a centered difference in space resulting in"
]
},
{
"cell_type": "markdown",
"id": "76ae9476",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"u_t\\approx \\frac{u(x,t+\\Delta t)-u(x,t)}{\\Delta t}=\\frac{u(x_i,t_j+\\Delta t)-u(x_i,t_j)}{\\Delta t}\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "95e239a3",
"metadata": {
"editable": true
},
"source": [
"and"
]
},
{
"cell_type": "markdown",
"id": "63fb1417",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"u_{xx}\\approx \\frac{u(x+\\Delta x,t)-2u(x,t)+u(x-\\Delta x,t)}{\\Delta x^2},\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "e1c77709",
"metadata": {
"editable": true
},
"source": [
"or"
]
},
{
"cell_type": "markdown",
"id": "2631e4bd",
"metadata": {
"editable": true
},
"source": [
"$$\n",
"u_{xx}\\approx \\frac{u(x_i+\\Delta x,t_j)-2u(x_i,t_j)+u(x_i-\\Delta x,t_j)}{\\Delta x^2}.\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "0fa54ca3",
"metadata": {
"editable": true
},
"source": [
"Write down the algorithm and the equations you need to implement.\n",
"Find also the analytical solution to the problem."
]
},
{
"cell_type": "markdown",
"id": "7579152d",
"metadata": {
"editable": true
},
"source": [
"### Part b)\n",
"\n",
"Implement the explicit scheme algorithm and perform tests of the solution \n",
"for $\\Delta x=1/10$, $\\Delta x=1/100$ using $\\Delta t$ as dictated by the stability limit of the explicit scheme. The stability criterion for the explicit scheme requires that $\\Delta t/\\Delta x^2 \\leq 1/2$. \n",
"\n",
"Study the solutions at two time points $t_1$ and $t_2$ where $u(x,t_1)$ is smooth but still significantly curved\n",
"and $u(x,t_2)$ is almost linear, close to the stationary state."
]
},
{
"cell_type": "markdown",
"id": "e8ceb962",
"metadata": {
"editable": true
},
"source": [
"### Part c) Neural networks\n",
"\n",
"Study now the lecture notes on solving ODEs and PDEs with neural\n",
"network and use either your own code from project 2 or the\n",
"functionality of tensorflow/keras to solve the same equation as in\n",
"part b). Discuss your results and compare them with the standard\n",
"explicit scheme. Include also the analytical solution and compare with\n",
"that."
]
},
{
"cell_type": "markdown",
"id": "fe5c3f2f",
"metadata": {
"editable": true
},
"source": [
"### Part d) Neural network complexity\n",
"\n",
"Here we study the stability of the results of the results as functions of the number of hidden nodes, layers and activation functions for the hidden layers.\n",
"Increase the number of hidden nodes and layers in order to see if this improves your results. Try also different activation functions for the hidden layers, such as the **tanh**, **ReLU**, and other activation functions. \n",
"Discuss your results."
]
},
{
"cell_type": "markdown",
"id": "3b2adf3a",
"metadata": {
"editable": true
},
"source": [
"### Part e)\n",
"\n",
"Finally, present a critical assessment of the methods you have studied\n",
"and discuss the potential for the solving differential equations with machine learning methods."
]
},
{
"cell_type": "markdown",
"id": "c2ed7243",
"metadata": {
"editable": true
},
"source": [
"## Introduction to numerical projects\n",
"\n",
"Here follows a brief recipe and recommendation on how to write a report for each\n",
"project.\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.\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": "e9f450e7",
"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 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": "4ec24e55",
"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
}