|
| 1 | +TITLE: Project 1 |
| 2 | +AUTHOR: "FYS5429/9429":"https://www.uio.no/studier/emner/matnat/fys/FYS5429/index-eng.html", Advanced machine learning and data analysis for the physical sciences, University of Oslo, Norway |
| 3 | +DATE: Spring semester 2025, deadline March 21 |
| 4 | + |
| 5 | + |
| 6 | +======= Possible paths for project 1 ======= |
| 7 | + |
| 8 | +We discuss here several paths as well as data sets for the first project (or as parts of a larger project) |
| 9 | +Tentative deadline March 22. We would also like to propose that people who have formed groups present eventual projects on January 30. |
| 10 | + |
| 11 | +===== Paths for the projects ===== |
| 12 | + |
| 13 | +o The computational path: Here we propose a path where you develop your own code for a convolutional or eventually recurrent neural network and apply this to data selects of your own selection. The code should be object oriented and flexible allowing for eventual extensions by including different Loss/Cost functions and other functionalities. Feel free to select data sets from those suggested below here. This code can also be extended upon by adding for example autoencoders. You can compare your own codes with implementations using TensorFlow(Keras)/PyTorch or other libraries. An alternative is to develop a code for an RNN inclusing long-term-short-memory (LSTM). This is often the starting point for large-language models and so-called transformers. Large language models are hovever not discussed during the lectures. |
| 14 | + |
| 15 | +o The differential equation path: Here we propose a set of differential equations (ordinary and/or partial) to be solved first using neural networks (using either your own code or TensorFlow/Pytorch or similar libraries). Thereafter we plan to extend the set of methods for solving these equations to recurrent neural networks and autoencoders (AE). This project can also be extended into including "Physics informed machine learning":"https://github.com/maziarraissi/PINNs". Here we can discuss neural networks that are trained to solve supervised learning tasks while respecting any given law of physics described by general nonlinear partial differential equations. |
| 16 | + |
| 17 | +o The application path: Here you can use the most relevant method(s) (say convolutional neural networks for images) and apply this(these) to data sets relevant for your own research. |
| 18 | + |
| 19 | +o The Gaussian processes/Bayesian statistics path: "Kernel regression (Gaussian processes) and Bayesian statistics":"https://jenfb.github.io/bkmr/overview.html" are popular tools in the machine learning literature. The main idea behind these approaches is to flexibly model the relationship between a large number of variables and a particular outcome (dependent variable). This can form a second part of a project where for example standard Kernel regression methods are used on a specific data set. Alternatively, participants can opt to work on a large project relevant for their own research using gaussian processes and/or Bayesian machine Learning. |
| 20 | + |
| 21 | +o Other possibilities. |
| 22 | + |
| 23 | +===== Defining the data sets to analyze yourself ===== |
| 24 | + |
| 25 | +You can propose own data sets that relate to your research interests or just use existing data sets from say |
| 26 | +o "Kaggle":"https://www.kaggle.com/datasets" |
| 27 | +o The "University of California at Irvine (UCI) with its machine learning repository":"https://archive.ics.uci.edu/ml/index.php". |
| 28 | +o For the differential equation problems, you can generate your own datasets, as described below. |
| 29 | + |
| 30 | +o If possible, you should link the data sets with existing 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? |
| 31 | + |
| 32 | +o A critical assessment of the methods with ditto perspectives and recommendations is also something you need to include. For those of you familiar with report writing, the layouts discussed in for example "FYS-STK4155":"https://github.com/CompPhysics/MachineLearning/blob/master/doc/Projects/EvaluationGrading/EvaluationForm.md". |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +===== Solving differential equations with neural networks ===== |
| 37 | + |
| 38 | +Here we describe the possible differential equations we can study |
| 39 | +first with neural networks and thereafter with recurrent neural |
| 40 | +networks and/or Autoenconders. |
| 41 | + |
| 42 | +The differential equations are given by the so-called "Lorenz attractor model":"https://encyclopediaofmath.org/index.php?title=Lorenz_attractor", and read |
| 43 | + |
| 44 | +!bt |
| 45 | +\[ |
| 46 | +\frac{dx}{dt}=\sigma\left(y-x\right), |
| 47 | +\] |
| 48 | +!et |
| 49 | +where $\sigma =10$ is a constant |
| 50 | +!bt |
| 51 | +\[ |
| 52 | +\frac{dy}{dt}= x\left(\rho-z\right)-y, |
| 53 | +\] |
| 54 | +!et |
| 55 | +with $\rho=28$ and |
| 56 | +!bt |
| 57 | +\[ |
| 58 | +\frac{dz}{dt}=xy-\beta z |
| 59 | +\] |
| 60 | +!et |
| 61 | + |
| 62 | +with $\beta=8/3$ as our final constant. |
| 63 | + |
| 64 | +The following function is a |
| 65 | +simple function which sets up the solution using the ordinary |
| 66 | +differential library which follows _NumPy_. Here we have fixed the |
| 67 | +time sted $\Delta t=0.01$ and the final time $t_f=8$. |
| 68 | + |
| 69 | +The program sets $100$ random initial values and produces inputs and outputs for a neural network calculations. |
| 70 | +The inputs are given by the values of the array $\bm{x}$ (which contains $x,y,z$ as functions of time) for the time step $\bm{x}_t$. |
| 71 | +The other array defined by $\bm{x}_{t+1}$ contains the outputs (or targets) which we want the neural network to reproduce. |
| 72 | + |
| 73 | +!bc pycod |
| 74 | +# Common imports |
| 75 | +import numpy as np |
| 76 | +from scipy.integrate import odeint |
| 77 | +import matplotlib.pyplot as plt |
| 78 | +import os |
| 79 | + |
| 80 | +# Where to save the figures and data files |
| 81 | +PROJECT_ROOT_DIR = "Results" |
| 82 | +FIGURE_ID = "Results/FigureFiles" |
| 83 | +DATA_ID = "DataFiles/" |
| 84 | + |
| 85 | +if not os.path.exists(PROJECT_ROOT_DIR): |
| 86 | + os.mkdir(PROJECT_ROOT_DIR) |
| 87 | + |
| 88 | +if not os.path.exists(FIGURE_ID): |
| 89 | + os.makedirs(FIGURE_ID) |
| 90 | + |
| 91 | +if not os.path.exists(DATA_ID): |
| 92 | + os.makedirs(DATA_ID) |
| 93 | + |
| 94 | +def image_path(fig_id): |
| 95 | + return os.path.join(FIGURE_ID, fig_id) |
| 96 | + |
| 97 | +def data_path(dat_id): |
| 98 | + return os.path.join(DATA_ID, dat_id) |
| 99 | + |
| 100 | +def save_fig(fig_id): |
| 101 | + plt.savefig(image_path(fig_id) + ".png", format='png') |
| 102 | + |
| 103 | + |
| 104 | +# Selection of parameter values and setting array for time |
| 105 | +dt =0.01; tfinal = 8 |
| 106 | +t = np.arange(0,tfinal+dt, dt) |
| 107 | +beta =8.0/3.0; rho = 28.0; sigma = 10.0 |
| 108 | + |
| 109 | +# define the inputs and outputs for the neural networks |
| 110 | +nninput = np.zeros((100*len(t)-1,3)) |
| 111 | +nnoutput = np.zeros((100*len(t)-1,3)) |
| 112 | +# Define the equations to integrate |
| 113 | +def lorenz_derivative(xyz, t0, sigma=sigma,beta=beta,rho=rho): |
| 114 | + x, y, z = xyz |
| 115 | + return [sigma*(x-y), x*(rho-z)-y, x*y-beta*z] |
| 116 | + |
| 117 | +# generate 100 random initial values |
| 118 | +x0 = -15.0+30.0*np.random.random((100,3)) |
| 119 | + |
| 120 | +# Use odeint functionality by sending in derivative function |
| 121 | +# Feel free to change the choice of integrator |
| 122 | +x_t = np.asarray([odeint(lorenz_derivative, x0_j, t) |
| 123 | + for x0_j in x0]) |
| 124 | + |
| 125 | +# define the inputs and outputs for the neural networks |
| 126 | +for j in range(100): |
| 127 | + nninput[j*(len(t)-1):(j+1)*(len(t)-1),:] = x_t[j,:-1,:] |
| 128 | + nnoutput[j*(len(t)-1):(j+1)*(len(t)-1),:] = x_t[j,1:,:] |
| 129 | + |
| 130 | +!ec |
| 131 | + |
| 132 | +The input and output variables are those we will start trying our |
| 133 | +network with. Your first taks is to set up a neural code (either using |
| 134 | +your own code or TensorFlow/PyTorch or similar libraries)) and use the |
| 135 | +above data to a prediction for the time evolution of Lorenz system for |
| 136 | +various values of the randomly chosen initial values. Study the |
| 137 | +dependence of the fit as function of the architecture of the network |
| 138 | +(number of nodes, hidden layers and types of activation functions) and |
| 139 | +various regularization schemes and optimization methods like standard |
| 140 | +gradient descent with momentum, stochastic gradient descent with |
| 141 | +batches and with and without momentum and various schedulers for the |
| 142 | +learning rate. |
| 143 | + |
| 144 | +Feel free to change the above differential equations. As an example, |
| 145 | +consider the following harmonic oscillator equations solved with the |
| 146 | +Runge-Kutta to fourth order method. This is a one-dimensional problem |
| 147 | +and it produces a position $x_t$ and velocity $v_t$. You could now try |
| 148 | +to fit both the velocities and positions using much of the same recipe |
| 149 | +as for Lorenz attractor. You will find it convenient to analyze one |
| 150 | +set of initial conditions first. The code is included here. |
| 151 | + |
| 152 | + |
| 153 | +This code is an example code that solves Newton's equations of motion |
| 154 | +with a given force and produces an output which in turn can be used to |
| 155 | +train a neural network |
| 156 | + |
| 157 | + |
| 158 | +!bc pycod |
| 159 | +# Common imports |
| 160 | +import numpy as np |
| 161 | +import pandas as pd |
| 162 | +from math import * |
| 163 | +import matplotlib.pyplot as plt |
| 164 | +import os |
| 165 | + |
| 166 | +# Where to save the figures and data files |
| 167 | +PROJECT_ROOT_DIR = "Results" |
| 168 | +FIGURE_ID = "Results/FigureFiles" |
| 169 | +DATA_ID = "DataFiles/" |
| 170 | + |
| 171 | +if not os.path.exists(PROJECT_ROOT_DIR): |
| 172 | + os.mkdir(PROJECT_ROOT_DIR) |
| 173 | + |
| 174 | +if not os.path.exists(FIGURE_ID): |
| 175 | + os.makedirs(FIGURE_ID) |
| 176 | + |
| 177 | +if not os.path.exists(DATA_ID): |
| 178 | + os.makedirs(DATA_ID) |
| 179 | + |
| 180 | +def image_path(fig_id): |
| 181 | + return os.path.join(FIGURE_ID, fig_id) |
| 182 | + |
| 183 | +def data_path(dat_id): |
| 184 | + return os.path.join(DATA_ID, dat_id) |
| 185 | + |
| 186 | +def save_fig(fig_id): |
| 187 | + plt.savefig(image_path(fig_id) + ".png", format='png') |
| 188 | + |
| 189 | + |
| 190 | +def SpringForce(v,x,t): |
| 191 | +# note here that we have divided by mass and we return the acceleration |
| 192 | + return -2*gamma*v-x+Ftilde*cos(t*Omegatilde) |
| 193 | + |
| 194 | + |
| 195 | +def RK4(v,x,t,n,Force): |
| 196 | + for i in range(n-1): |
| 197 | +# Setting up k1 |
| 198 | + k1x = DeltaT*v[i] |
| 199 | + k1v = DeltaT*Force(v[i],x[i],t[i]) |
| 200 | +# Setting up k2 |
| 201 | + vv = v[i]+k1v*0.5 |
| 202 | + xx = x[i]+k1x*0.5 |
| 203 | + k2x = DeltaT*vv |
| 204 | + k2v = DeltaT*Force(vv,xx,t[i]+DeltaT*0.5) |
| 205 | +# Setting up k3 |
| 206 | + vv = v[i]+k2v*0.5 |
| 207 | + xx = x[i]+k2x*0.5 |
| 208 | + k3x = DeltaT*vv |
| 209 | + k3v = DeltaT*Force(vv,xx,t[i]+DeltaT*0.5) |
| 210 | +# Setting up k4 |
| 211 | + vv = v[i]+k3v |
| 212 | + xx = x[i]+k3x |
| 213 | + k4x = DeltaT*vv |
| 214 | + k4v = DeltaT*Force(vv,xx,t[i]+DeltaT) |
| 215 | +# Final result |
| 216 | + x[i+1] = x[i]+(k1x+2*k2x+2*k3x+k4x)/6. |
| 217 | + v[i+1] = v[i]+(k1v+2*k2v+2*k3v+k4v)/6. |
| 218 | + t[i+1] = t[i] + DeltaT |
| 219 | + |
| 220 | + |
| 221 | +# Main part begins here |
| 222 | + |
| 223 | +DeltaT = 0.001 |
| 224 | +#set up arrays |
| 225 | +tfinal = 20 # in dimensionless time |
| 226 | +n = ceil(tfinal/DeltaT) |
| 227 | +# set up arrays for t, v, and x |
| 228 | +t = np.zeros(n) |
| 229 | +v = np.zeros(n) |
| 230 | +x = np.zeros(n) |
| 231 | +# Initial conditions (can change to more than one dim) |
| 232 | +x0 = 1.0 |
| 233 | +v0 = 0.0 |
| 234 | +x[0] = x0 |
| 235 | +v[0] = v0 |
| 236 | +gamma = 0.2 |
| 237 | +Omegatilde = 0.5 |
| 238 | +Ftilde = 1.0 |
| 239 | +# Start integrating using Euler's method |
| 240 | +# Note that we define the force function as a SpringForce |
| 241 | +RK4(v,x,t,n,SpringForce) |
| 242 | + |
| 243 | +# Plot position as function of time |
| 244 | +fig, ax = plt.subplots() |
| 245 | +ax.set_ylabel('x[m]') |
| 246 | +ax.set_xlabel('t[s]') |
| 247 | +ax.plot(t, x) |
| 248 | +fig.tight_layout() |
| 249 | +save_fig("ForcedBlockRK4") |
| 250 | +plt.show() |
| 251 | + |
| 252 | +!ec |
| 253 | + |
| 254 | + |
| 255 | + |
| 256 | + |
| 257 | +The next step is to include recurrent neural networks. These will be discussed in connection with coming lectures. |
| 258 | + |
| 259 | + |
| 260 | + |
| 261 | + |
| 262 | + |
| 263 | +===== Introduction to numerical projects ===== |
| 264 | + |
| 265 | +Here follows a brief recipe and recommendation on how to write a report for each |
| 266 | +project. |
| 267 | + |
| 268 | + * Give a short description of the nature of the problem and the eventual numerical methods you have used. |
| 269 | + |
| 270 | + * 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. |
| 271 | + |
| 272 | + * Include the source code of your program. Comment your program properly. |
| 273 | + |
| 274 | + * If possible, try to find analytic solutions, or known limits in order to test your program when developing the code. |
| 275 | + |
| 276 | + * 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. |
| 277 | + |
| 278 | + * 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. |
| 279 | + |
| 280 | + * Try to give an interpretation of you results in your answers to the problems. |
| 281 | + |
| 282 | + * 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. |
| 283 | + |
| 284 | + * 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. |
| 285 | + |
| 286 | + |
| 287 | + |
| 288 | + |
| 289 | + |
| 290 | + |
| 291 | +===== Format for electronic delivery of report and programs ===== |
| 292 | + |
| 293 | +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: |
| 294 | + |
| 295 | + * Send us an email in order to hand in your projects with a link to your GitHub/Gitlab repository. |
| 296 | + |
| 297 | + * 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. |
| 298 | + |
| 299 | + |
| 300 | +Finally, |
| 301 | +we encourage you to collaborate. Optimal working groups consist of |
| 302 | +2-3 students. You can then hand in a common report. |
| 303 | + |
| 304 | + |
| 305 | + |
| 306 | + |
| 307 | + |
| 308 | + |
0 commit comments