641 lines
41 KiB
HTML
641 lines
41 KiB
HTML
<!--
|
|
HTML file automatically generated from DocOnce source
|
|
(https://github.com/doconce/doconce/)
|
|
doconce format html Project2.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=Project2-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 2 on Machine Learning, deadline November 10 (Midnight)">
|
|
<title>Project 2 on Machine Learning, deadline November 10 (Midnight)</title>
|
|
<!-- Bootstrap style: bootstrap -->
|
|
<!-- doconce format html Project2.do.txt --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=Project2-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': [('Deliverables', 2, None, 'deliverables'),
|
|
('Preamble: Note on writing reports, using reference material, '
|
|
'AI and other tools',
|
|
3,
|
|
None,
|
|
'preamble-note-on-writing-reports-using-reference-material-ai-and-other-tools'),
|
|
('Classification and Regression, writing our own neural network '
|
|
'code',
|
|
2,
|
|
None,
|
|
'classification-and-regression-writing-our-own-neural-network-code'),
|
|
('Part a): Analytical warm-up',
|
|
3,
|
|
None,
|
|
'part-a-analytical-warm-up'),
|
|
('Reminder about the gradient machinery from project 1',
|
|
3,
|
|
None,
|
|
'reminder-about-the-gradient-machinery-from-project-1'),
|
|
('Part b): Writing your own Neural Network code',
|
|
3,
|
|
None,
|
|
'part-b-writing-your-own-neural-network-code'),
|
|
('Part c): Testing against other software libraries',
|
|
3,
|
|
None,
|
|
'part-c-testing-against-other-software-libraries'),
|
|
('Part d): Testing different activation functions and depths of '
|
|
'the neural network',
|
|
3,
|
|
None,
|
|
'part-d-testing-different-activation-functions-and-depths-of-the-neural-network'),
|
|
('Part e): Testing different norms',
|
|
3,
|
|
None,
|
|
'part-e-testing-different-norms'),
|
|
('Part f): Classification analysis using neural networks',
|
|
3,
|
|
None,
|
|
'part-f-classification-analysis-using-neural-networks'),
|
|
('Part g) Critical evaluation of the various algorithms',
|
|
3,
|
|
None,
|
|
'part-g-critical-evaluation-of-the-various-algorithms'),
|
|
('Summary of methods to implement and analyze',
|
|
2,
|
|
None,
|
|
'summary-of-methods-to-implement-and-analyze'),
|
|
('Required Analysis:', 3, None, 'required-analysis'),
|
|
('Optional (Note that you should include at least two of these '
|
|
'in the report):',
|
|
3,
|
|
None,
|
|
'optional-note-that-you-should-include-at-least-two-of-these-in-the-report'),
|
|
('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')]}
|
|
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="Project2-bs.html">Project 2 on Machine Learning, deadline November 10 (Midnight)</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="#deliverables" style="font-size: 80%;"><b>Deliverables</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="#preamble-note-on-writing-reports-using-reference-material-ai-and-other-tools" style="font-size: 80%;"> Preamble: Note on writing reports, using reference material, AI and other tools</a></li>
|
|
<!-- navigation toc: --> <li><a href="#classification-and-regression-writing-our-own-neural-network-code" style="font-size: 80%;"><b>Classification and Regression, writing our own neural network code</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="#part-a-analytical-warm-up" style="font-size: 80%;"> Part a): Analytical warm-up</a></li>
|
|
<!-- navigation toc: --> <li><a href="#reminder-about-the-gradient-machinery-from-project-1" style="font-size: 80%;"> Reminder about the gradient machinery from project 1</a></li>
|
|
<!-- navigation toc: --> <li><a href="#part-b-writing-your-own-neural-network-code" style="font-size: 80%;"> Part b): Writing your own Neural Network code</a></li>
|
|
<!-- navigation toc: --> <li><a href="#part-c-testing-against-other-software-libraries" style="font-size: 80%;"> Part c): Testing against other software libraries</a></li>
|
|
<!-- navigation toc: --> <li><a href="#part-d-testing-different-activation-functions-and-depths-of-the-neural-network" style="font-size: 80%;"> Part d): Testing different activation functions and depths of the neural network</a></li>
|
|
<!-- navigation toc: --> <li><a href="#part-e-testing-different-norms" style="font-size: 80%;"> Part e): Testing different norms</a></li>
|
|
<!-- navigation toc: --> <li><a href="#part-f-classification-analysis-using-neural-networks" style="font-size: 80%;"> Part f): Classification analysis using neural networks</a></li>
|
|
<!-- navigation toc: --> <li><a href="#part-g-critical-evaluation-of-the-various-algorithms" style="font-size: 80%;"> Part g) Critical evaluation of the various algorithms</a></li>
|
|
<!-- navigation toc: --> <li><a href="#summary-of-methods-to-implement-and-analyze" style="font-size: 80%;"><b>Summary of methods to implement and analyze</b></a></li>
|
|
<!-- navigation toc: --> <li><a href="#required-analysis" style="font-size: 80%;"> Required Analysis:</a></li>
|
|
<!-- navigation toc: --> <li><a href="#optional-note-that-you-should-include-at-least-two-of-these-in-the-report" style="font-size: 80%;"> Optional (Note that you should include at least two of these in the report):</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>
|
|
|
|
</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 2 on Machine Learning, deadline November 10 (Midnight)</h1>
|
|
</center> <!-- document title -->
|
|
|
|
<!-- author(s): <a href="http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html" target="_self">Data Analysis and Machine Learning FYS-STK3155/FYS4155</a> -->
|
|
<center>
|
|
<b><a href="http://www.uio.no/studier/emner/matnat/fys/FYS3155/index-eng.html" target="_self">Data Analysis and Machine Learning FYS-STK3155/FYS4155</a></b>
|
|
</center>
|
|
<!-- institution -->
|
|
<center>
|
|
<b>University of Oslo, Norway</b>
|
|
</center>
|
|
<br>
|
|
<center>
|
|
<h4>October 14, 2025</h4>
|
|
</center> <!-- date -->
|
|
<br>
|
|
|
|
|
|
</div> <!-- end jumbotron -->
|
|
<h2 id="deliverables" class="anchor">Deliverables </h2>
|
|
|
|
<p>First, join a group in canvas with your group partners. Pick an avaliable group for Project 2 in the <b>People</b> page.</p>
|
|
|
|
<p>In canvas, deliver as a group and include:</p>
|
|
|
|
<ul>
|
|
<li> A PDF of your report which follows the guidelines covered below and in the week 39 exercises. Additional requirements include:</li>
|
|
<ul>
|
|
<li> It should be around 5000 words, use the word counter in Overleaf for this. This often corresponds to 10-12 pages. References and appendices are excluded from the word count</li>
|
|
<li> It should include around 10-15 figures. You can include more figures in appendices and/or as supplemental material in your repository.</li>
|
|
</ul>
|
|
<li> A comment linking to your github repository (or folder in one of your github repositories) for this project. The repository must include</li>
|
|
</ul>
|
|
<p>A PDF file of the report</p>
|
|
<ul>
|
|
<li> A folder named Code, where you put python files for your functions and notebooks for reproducing your results. Remember to use a seed for generating random data and for train-test splits when generating final results.</li>
|
|
<li> A README file with the name of the group members</li>
|
|
<li> a short description of the project</li>
|
|
<li> a description of how to install the required packages to run your code from a requirements.txt file or similar (such as a plain text description) names and descriptions of the various notebooks in the Code folder and the results they produce</li>
|
|
</ul>
|
|
<h3 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 </h3>
|
|
|
|
<p>We want you to answer the three different projects by handing in
|
|
reports written like a standard scientific/technical report. The links
|
|
at
|
|
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 grade 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
|
|
https://scikit-learn.org/dev/about.html.
|
|
</p>
|
|
|
|
<p>We enocurage you to use tools like 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.
|
|
</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
|
|
https://archive.ics.uci.edu/ml/index.php is an excellent site to look
|
|
up for examples and inspiration. Kaggle.com is an equally interesting
|
|
site. Feel free to explore these sites.
|
|
</p>
|
|
<h2 id="classification-and-regression-writing-our-own-neural-network-code" class="anchor">Classification and Regression, writing our own neural network code </h2>
|
|
|
|
<p>The main aim of this project is to study both classification and
|
|
regression problems by developing our own
|
|
feed-forward neural network (FFNN) code. The exercises from week 41 and 42 (see <a href="https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/exercisesweek41.html" target="_self"><tt>https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/exercisesweek41.html</tt></a> and <a href="https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/exercisesweek42.html" target="_self"><tt>https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/exercisesweek42.html</tt></a>) as well as the lecture material from the same weeks (see <a href="https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/week41.html" target="_self"><tt>https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/week41.html</tt></a> and <a href="https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/week42.html" target="_self"><tt>https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/week42.html</tt></a>) should contain enough information for you to get started with writing your own code.
|
|
</p>
|
|
|
|
<p>We will also reuse our codes on gradient descent methods from project 1.</p>
|
|
|
|
<p>The data sets that we propose here are (the default sets)</p>
|
|
|
|
<ul>
|
|
<li> 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</li>
|
|
<ul>
|
|
<li> The simple one-dimensional function Runge function from project 1, that is \( f(x) = \frac{1}{1+25x^2} \). We recommend using a simpler function when developing your neural network code for regression problems. Feel however free to discuss and study other functions, such as the two-dimensional Runge function \( f(x,y)=\left[(10x - 5)^2 + (10y - 5)^2 + 1 \right]^{-1} \), or even more complicated two-dimensional functions (see the supplementary material of <a href="https://www.nature.com/articles/s41467-025-61362-4" target="_self"><tt>https://www.nature.com/articles/s41467-025-61362-4</tt></a> for an extensive list of two-dimensional functions).</li>
|
|
</ul>
|
|
<li> Classification.</li>
|
|
<ul>
|
|
<li> We will consider a multiclass classification problem given by the full MNIST data set. The full data set is at <a href="https://www.kaggle.com/datasets/hojjatk/mnist-dataset" target="_self"><tt>https://www.kaggle.com/datasets/hojjatk/mnist-dataset</tt></a>.</li>
|
|
</ul>
|
|
</ul>
|
|
<p>We will start with a regression problem and we will reuse our codes on gradient descent methods from project 1.</p>
|
|
<h3 id="part-a-analytical-warm-up" class="anchor">Part a): Analytical warm-up </h3>
|
|
|
|
<p>When using our gradient machinery from project 1, we will need the expressions for the cost/loss functions and their respective
|
|
gradients. The functions whose gradients we need are:
|
|
</p>
|
|
<ol>
|
|
<li> The mean-squared error (MSE) with and without the \( L_1 \) and \( L_2 \) norms (regression problems)</li>
|
|
<li> The binary cross entropy (aka log loss) for binary classification problems with and without \( L_1 \) and \( L_2 \) norms</li>
|
|
<li> The multiclass cross entropy cost/loss function (aka Softmax cross entropy or just Softmax loss function)</li>
|
|
</ol>
|
|
<p>Set up these three cost/loss functions and their respective derivatives and explain the various terms. In this project you will however only use the MSE and the Softmax cross entropy.</p>
|
|
|
|
<p>We will test three activation functions for our neural network setup, these are the </p>
|
|
<ol>
|
|
<li> The Sigmoid (aka <b>logit</b>) function,</li>
|
|
<li> the RELU function and</li>
|
|
<li> the Leaky RELU function</li>
|
|
</ol>
|
|
<p>Set up their expressions and their first derivatives.
|
|
You may consult the lecture notes (with codes and more) from week 42 at <a href="https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/week42.html" target="_self"><tt>https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/week42.html</tt></a>.
|
|
</p>
|
|
<h3 id="reminder-about-the-gradient-machinery-from-project-1" class="anchor">Reminder about the gradient machinery from project 1 </h3>
|
|
|
|
<p>In the setup of a neural network code you will need your gradient descent codes from
|
|
project 1. For neural networks we will recommend using stochastic
|
|
gradient descent with either the RMSprop or the ADAM algorithms for
|
|
updating the learning rates. But you should feel free to try plain gradient descent as well.
|
|
</p>
|
|
|
|
<p>We recommend reading chapter 8 on optimization from the textbook of
|
|
Goodfellow, Bengio and Courville at
|
|
<a href="https://www.deeplearningbook.org/" target="_self"><tt>https://www.deeplearningbook.org/</tt></a>. This chapter contains many
|
|
useful insights and discussions on the optimization part of machine
|
|
learning. A useful reference on the back progagation algorithm is
|
|
Nielsen's book at <a href="http://neuralnetworksanddeeplearning.com/" target="_self"><tt>http://neuralnetworksanddeeplearning.com/</tt></a>.
|
|
</p>
|
|
|
|
<p>You will find the Python <a href="https://seaborn.pydata.org/generated/seaborn.heatmap.html" target="_self">Seaborn
|
|
package</a>
|
|
useful when plotting the results as function of the learning rate
|
|
\( \eta \) and the hyper-parameter \( \lambda \) .
|
|
</p>
|
|
<h3 id="part-b-writing-your-own-neural-network-code" class="anchor">Part b): Writing your own Neural Network code </h3>
|
|
|
|
<p>Your aim now, and this is the central part of this project, is to
|
|
write your own FFNN code implementing the back
|
|
propagation algorithm discussed in the lecture slides from week 41 at <a href="https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/week41.html" target="_self"><tt>https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/week41.html</tt></a> and week 42 at <a href="https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/week42.html" target="_self"><tt>https://compphysics.github.io/MachineLearning/doc/LectureNotes/_build/html/week42.html</tt></a>.
|
|
</p>
|
|
|
|
<p>We will focus on a regression problem first, using the one-dimensional Runge function</p>
|
|
$$
|
|
f(x) = \frac{1}{1+25x^2},
|
|
$$
|
|
|
|
<p>from project 1.</p>
|
|
|
|
<p>Use only the mean-squared error as cost function (no regularization terms) and
|
|
write an FFNN code for a regression problem with a flexible number of hidden
|
|
layers and nodes using only the Sigmoid function as activation function for
|
|
the hidden layers. Initialize the weights using a normal
|
|
distribution. How would you initialize the biases? And which
|
|
activation function would you select for the final output layer?
|
|
And how would you set up your design/feature matrix? Hint: does it have to represent a polynomial approximation as you did in project 1?
|
|
</p>
|
|
|
|
<p>Train your network and compare the results with those from your OLS
|
|
regression code from project 1 using the one-dimensional Runge
|
|
function. When comparing your neural network code with the OLS
|
|
results from project 1, use the same data sets which gave you the best
|
|
MSE score. Moreover, use the polynomial order from project 1 that gave you the
|
|
best result. Compare these results with your neural network with one
|
|
and two hidden layers using \( 50 \) and \( 100 \) hidden nodes, respectively.
|
|
</p>
|
|
|
|
<p>Comment your results and give a critical discussion of the results
|
|
obtained with the OLS code from project 1 and your own neural network
|
|
code. Make an analysis of the learning rates employed to find the
|
|
optimal MSE score. Test both stochastic gradient descent
|
|
with RMSprop and ADAM and plain gradient descent with different
|
|
learning rates.
|
|
</p>
|
|
|
|
<p>You should, as you did in project 1, scale your data.</p>
|
|
<h3 id="part-c-testing-against-other-software-libraries" class="anchor">Part c): Testing against other software libraries </h3>
|
|
|
|
<p>You should test your results against a similar code using <b>Scikit-Learn</b> (see the examples in the above lecture notes from weeks 41 and 42) or <b>tensorflow/keras</b> or <b>Pytorch</b> (for Pytorch, see Raschka et al.'s text chapters 12 and 13). </p>
|
|
|
|
<p>Furthermore, you should also test that your derivatives are correctly
|
|
calculated using automatic differentiation, using for example the
|
|
<b>Autograd</b> library or the <b>JAX</b> library. It is optional to implement
|
|
these libraries for the present project. In this project they serve as
|
|
useful tests of our derivatives.
|
|
</p>
|
|
<h3 id="part-d-testing-different-activation-functions-and-depths-of-the-neural-network" class="anchor">Part d): Testing different activation functions and depths of the neural network </h3>
|
|
|
|
<p>You should also test different activation functions for the hidden
|
|
layers. Try out the Sigmoid, the RELU and the Leaky RELU functions and
|
|
discuss your results. Test your results as functions of the number of hidden layers and nodes. Do you see signs of overfitting?
|
|
It is optional in this project to perform a bias-variance trade-off analysis.
|
|
</p>
|
|
<h3 id="part-e-testing-different-norms" class="anchor">Part e): Testing different norms </h3>
|
|
|
|
<p>Finally, still using the one-dimensional Runge function, add now the
|
|
hyperparameters \( \lambda \) with the \( L_2 \) and \( L_1 \) norms. Find the
|
|
optimal results for the hyperparameters \( \lambda \) and the learning
|
|
rates \( \eta \) and neural network architecture and compare the \( L_2 \) results with Ridge regression from
|
|
project 1 and the \( L_1 \) results with the Lasso calculations of project 1.
|
|
Use again the same data sets and the best results from project 1 in your comparisons.
|
|
</p>
|
|
<h3 id="part-f-classification-analysis-using-neural-networks" class="anchor">Part f): Classification analysis using neural networks </h3>
|
|
|
|
<p>With a well-written code it should now be easy to change the
|
|
activation function for the output layer.
|
|
</p>
|
|
|
|
<p>Here we will change the cost function for our neural network code
|
|
developed in parts b), d) and e) in order to perform a classification
|
|
analysis. The classification problem we will study is the multiclass
|
|
MNIST problem, see the description of the full data set at
|
|
<a href="https://www.kaggle.com/datasets/hojjatk/mnist-dataset" target="_self"><tt>https://www.kaggle.com/datasets/hojjatk/mnist-dataset</tt></a>. We will use the Softmax cross entropy function discussed in a).
|
|
The MNIST data set discussed in the lecture notes from week 42 is a downscaled variant of the full dataset.
|
|
</p>
|
|
|
|
<p>Feel free to suggest other data sets. If you find the classic MNIST data set somewhat limited, feel free to try the
|
|
MNIST-Fashion data set at for example <a href="https://www.kaggle.com/datasets/zalando-research/fashionmnist" target="_self"><tt>https://www.kaggle.com/datasets/zalando-research/fashionmnist</tt></a>.
|
|
</p>
|
|
|
|
<p>To set up the data set, the following python programs may be useful</p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
|
<div class="cell border-box-sizing code_cell rendered">
|
|
<div class="input">
|
|
<div class="inner_cell">
|
|
<div class="input_area">
|
|
<div class="highlight" style="background: #f8f8f8">
|
|
<pre style="line-height: 125%;"><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.datasets</span> <span style="color: #008000; font-weight: bold">import</span> fetch_openml
|
|
|
|
<span style="color: #408080; font-style: italic"># Fetch the MNIST dataset</span>
|
|
mnist <span style="color: #666666">=</span> fetch_openml(<span style="color: #BA2121">'mnist_784'</span>, version<span style="color: #666666">=1</span>, as_frame<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">False</span>, parser<span style="color: #666666">=</span><span style="color: #BA2121">'auto'</span>)
|
|
|
|
<span style="color: #408080; font-style: italic"># Extract data (features) and target (labels)</span>
|
|
X <span style="color: #666666">=</span> mnist<span style="color: #666666">.</span>data
|
|
y <span style="color: #666666">=</span> mnist<span style="color: #666666">.</span>target
|
|
</pre>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="output_wrapper">
|
|
<div class="output">
|
|
<div class="output_area">
|
|
<div class="output_subarea output_stream output_stdout output_text">
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<p>You should consider scaling the data. The Pixel values in MNIST range from 0 to 255. Scaling them to a 0-1 range can improve the performance of some models. That is, you could implement the following scaling</p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
|
<div class="cell border-box-sizing code_cell rendered">
|
|
<div class="input">
|
|
<div class="inner_cell">
|
|
<div class="input_area">
|
|
<div class="highlight" style="background: #f8f8f8">
|
|
<pre style="line-height: 125%;">X <span style="color: #666666">=</span> X <span style="color: #666666">/</span> <span style="color: #666666">255.0</span>
|
|
</pre>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="output_wrapper">
|
|
<div class="output">
|
|
<div class="output_area">
|
|
<div class="output_subarea output_stream output_stdout output_text">
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<p>And then perform the standard train-test splitting</p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
|
<div class="cell border-box-sizing code_cell rendered">
|
|
<div class="input">
|
|
<div class="inner_cell">
|
|
<div class="input_area">
|
|
<div class="highlight" style="background: #f8f8f8">
|
|
<pre style="line-height: 125%;"><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.model_selection</span> <span style="color: #008000; font-weight: bold">import</span> train_test_split
|
|
X_train, X_test, y_train, y_test <span style="color: #666666">=</span> train_test_split(X, y, test_size<span style="color: #666666">=0.2</span>, random_state<span style="color: #666666">=42</span>)
|
|
</pre>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="output_wrapper">
|
|
<div class="output">
|
|
<div class="output_area">
|
|
<div class="output_subarea output_stream output_stdout output_text">
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<p>To measure the performance of our classification problem we will use the
|
|
so-called <em>accuracy</em> score. The accuracy is as you would expect just
|
|
the number of correctly guessed targets \( t_i \) divided by the total
|
|
number of targets, that is
|
|
</p>
|
|
|
|
$$
|
|
\text{Accuracy} = \frac{\sum_{i=1}^n I(t_i = y_i)}{n} ,
|
|
$$
|
|
|
|
<p>where \( I \) is the indicator function, \( 1 \) if \( t_i = y_i \) and \( 0 \)
|
|
otherwise if we have a binary classification problem. Here \( t_i \)
|
|
represents the target and \( y_i \) the outputs of your FFNN code and \( n \) is simply the number of targets \( t_i \).
|
|
</p>
|
|
|
|
<p>Discuss your results and give a critical analysis of the various parameters, including hyper-parameters like the learning rates and the regularization parameter \( \lambda \), various activation functions, number of hidden layers and nodes and activation functions. </p>
|
|
|
|
<p>Again, we strongly recommend that you compare your own neural Network
|
|
code for classification and pertinent results against a similar code using <b>Scikit-Learn</b> or <b>tensorflow/keras</b> or <b>pytorch</b>.
|
|
</p>
|
|
|
|
<p>If you have time, you can use the functionality of <b>scikit-learn</b> and compare your neural network results with those from Logistic regression. This is optional.
|
|
The weblink here <a href="https://medium.com/ai-in-plain-english/comparison-between-logistic-regression-and-neural-networks-in-classifying-digits-dc5e85cd93c3" target="_self"><tt>https://medium.com/ai-in-plain-english/comparison-between-logistic-regression-and-neural-networks-in-classifying-digits-dc5e85cd93c3</tt></a>compares logistic regression and FFNN using the so-called MNIST data set. You may find several useful hints and ideas from this article. Your neural network code can implement the equivalent of logistic regression by simply setting the number of hidden layers to zero and keeping just the input and the output layers.
|
|
</p>
|
|
|
|
<p>If you wish to compare with say Logisti Regression from <b>scikit-learn</b>, the following code uses the above data set</p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
|
<div class="cell border-box-sizing code_cell rendered">
|
|
<div class="input">
|
|
<div class="inner_cell">
|
|
<div class="input_area">
|
|
<div class="highlight" style="background: #f8f8f8">
|
|
<pre style="line-height: 125%;"><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> LogisticRegression
|
|
<span style="color: #408080; font-style: italic"># Initialize the model</span>
|
|
model <span style="color: #666666">=</span> LogisticRegression(solver<span style="color: #666666">=</span><span style="color: #BA2121">'saga'</span>, multi_class<span style="color: #666666">=</span><span style="color: #BA2121">'multinomial'</span>, max_iter<span style="color: #666666">=1000</span>, random_state<span style="color: #666666">=42</span>)
|
|
<span style="color: #408080; font-style: italic"># Train the model</span>
|
|
model<span style="color: #666666">.</span>fit(X_train, y_train)
|
|
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.metrics</span> <span style="color: #008000; font-weight: bold">import</span> accuracy_score
|
|
<span style="color: #408080; font-style: italic"># Make predictions on the test set</span>
|
|
y_pred <span style="color: #666666">=</span> model<span style="color: #666666">.</span>predict(X_test)
|
|
<span style="color: #408080; font-style: italic"># Calculate accuracy</span>
|
|
accuracy <span style="color: #666666">=</span> accuracy_score(y_test, y_pred)
|
|
<span style="color: #008000">print</span>(<span style="color: #BA2121">f"Model Accuracy: </span><span style="color: #BB6688; font-weight: bold">{</span>accuracy<span style="color: #BB6688; font-weight: bold">:</span><span style="color: #BA2121">.4f</span><span style="color: #BB6688; font-weight: bold">}</span><span style="color: #BA2121">"</span>)
|
|
</pre>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="output_wrapper">
|
|
<div class="output">
|
|
<div class="output_area">
|
|
<div class="output_subarea output_stream output_stdout output_text">
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<h3 id="part-g-critical-evaluation-of-the-various-algorithms" class="anchor">Part g) Critical evaluation of the various algorithms </h3>
|
|
|
|
<p>After all these glorious calculations, you should now summarize the
|
|
various algorithms and come with a critical evaluation of their pros
|
|
and cons. Which algorithm works best for the regression case and which
|
|
is best for the classification case. These codes can also be part of
|
|
your final project 3, but now applied to other data sets.
|
|
</p>
|
|
<h2 id="summary-of-methods-to-implement-and-analyze" class="anchor">Summary of methods to implement and analyze </h2>
|
|
|
|
<b>Required Implementation:</b>
|
|
<ol>
|
|
<li> Reuse the regression code and results from project 1, these will act as a benchmark for seeing how suited a neural network is for this regression task.</li>
|
|
<li> Implement a neural network with</li>
|
|
<ul>
|
|
<li> A flexible number of layers</li>
|
|
<li> A flexible number of nodes in each layer</li>
|
|
<li> A changeable activation function in each layer (Sigmoid, ReLU, LeakyReLU, as well as Linear and Softmax)</li>
|
|
<li> A changeable cost function, which will be set to MSE for regression and cross-entropy for multiple-classification</li>
|
|
<li> An optional L1 or L2 norm of the weights and biases in the cost function (only used for computing gradients, not interpretable metrics)</li>
|
|
</ul>
|
|
<li> Implement the back-propagation algorithm to compute the gradient of your neural network</li>
|
|
<li> Reuse the implementation of Plain and Stochastic Gradient Descent from Project 1 (and adapt the code to work with the your neural network)</li>
|
|
<ul>
|
|
<li> With no optimization algorithm</li>
|
|
<li> With RMS Prop</li>
|
|
<li> With ADAM</li>
|
|
</ul>
|
|
<li> Implement scaling and train-test splitting of your data, preferably using sklearn</li>
|
|
<li> Implement and compute metrics like the MSE and Accuracy</li>
|
|
</ol>
|
|
<h3 id="required-analysis" class="anchor">Required Analysis: </h3>
|
|
<ol>
|
|
<li> Briefly show and argue for the advantages and disadvantages of the methods from Project 1.</li>
|
|
<li> Explore and show the impact of changing the number of layers, nodes per layer, choice of activation function, and inclusion of L1 and L2 norms. Present only the most interesting results from this exploration. 2D Heatmaps will be good for this: Start with finding a well performing set of hyper-parameters, then change two at a time in a range that shows good and bad performance.</li>
|
|
<li> Show and argue for the advantages and disadvantages of using a neural network for regression on your data</li>
|
|
<li> Show and argue for the advantages and disadvantages of using a neural network for classification on your data</li>
|
|
<li> Show and argue for the advantages and disadvantages of the different gradient methods and learning rates when training the neural network</li>
|
|
</ol>
|
|
<h3 id="optional-note-that-you-should-include-at-least-two-of-these-in-the-report" class="anchor">Optional (Note that you should include at least two of these in the report): </h3>
|
|
<ol>
|
|
<li> Implement Logistic Regression as simple classification model case (equivalent to a Neural Network with just the output layer)</li>
|
|
<li> Compute the gradient of the neural network with autograd, to show that it gives the same result as your hand-written backpropagation.</li>
|
|
<li> Compare your results with results from using a machine-learning library like pytorch (https://docs.pytorch.org/tutorials/beginner/basics/buildmodel_tutorial.html)</li>
|
|
<li> Use a more complex classification dataset instead, like the fashion MNIST (see <a href="https://www.kaggle.com/datasets/zalando-research/fashionmnist" target="_self"><tt>https://www.kaggle.com/datasets/zalando-research/fashionmnist</tt></a>)</li>
|
|
<li> Use a more complex regression dataset instead, like the two-dimensional Runge function \( f(x,y)=\left[(10x - 5)^2 + (10y - 5)^2 + 1 \right]^{-1} \), or even more complicated two-dimensional functions (see the supplementary material of <a href="https://www.nature.com/articles/s41467-025-61362-4" target="_self"><tt>https://www.nature.com/articles/s41467-025-61362-4</tt></a> for an extensive list of two-dimensional functions).</li>
|
|
<li> Compute and interpret a confusion matrix of your best classification model (see <a href="https://www.researchgate.net/figure/Confusion-matrix-of-MNIST-and-F-MNIST-embeddings_fig5_349758607" target="_self"><tt>https://www.researchgate.net/figure/Confusion-matrix-of-MNIST-and-F-MNIST-embeddings_fig5_349758607</tt></a>)</li>
|
|
</ol>
|
|
<h2 id="background-literature" class="anchor">Background literature </h2>
|
|
|
|
<ol>
|
|
<li> The text of Michael Nielsen is highly recommended, see Nielsen's book at <a href="http://neuralnetworksanddeeplearning.com/" target="_self"><tt>http://neuralnetworksanddeeplearning.com/</tt></a>. It is an excellent read.</li>
|
|
<li> Goodfellow, Bengio and Courville, Deep Learning at <a href="https://www.deeplearningbook.org/" target="_self"><tt>https://www.deeplearningbook.org/</tt></a>. Here we recommend chapters 6, 7 and 8</li>
|
|
<li> Raschka et al. at <a href="https://sebastianraschka.com/blog/2022/ml-pytorch-book.html" target="_self"><tt>https://sebastianraschka.com/blog/2022/ml-pytorch-book.html</tt></a>. Here we recommend chapters 11, 12 and 13.</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 write a report for each
|
|
project.
|
|
</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.</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 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>
|
|
|
|
<p>
|
|
<!-- navigation buttons at the bottom of the page -->
|
|
<ul class="pagination">
|
|
<li class="active"><a href="._Project2-bs000.html">1</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>
|
|
-->
|
|
<center style="font-size:80%">
|
|
<!-- copyright --> © 1999-2025, "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
|
|
</center>
|
|
</body>
|
|
</html>
|
|
|