transcribe

Chapter 8.9 - Introduction to Machine Learning: NN Time-Stepper

Nathan Kutz · 17m · transcribed 17d ago
More from Nathan Kutz Business
𝕏 Share ▶ YouTube 📥 PDF 🤖 .md

Section Insights

# 0:08

Introduction to Neural Network Training

What is the focus of this training session?

The session focuses on training a feed forward neural network to model a dynamical system using time sequence data.

  • The goal is to learn how to predict future states from current states.
  • This approach differs from classification tasks, focusing instead on numerical mapping.
  • Feed forward neural networks are a good starting point for learning neural network programming.
# 0:29

Understanding Lorenz Equations

What are Lorenz equations and their significance?

Lorenz equations are a system of differential equations that exhibit chaotic behavior, making them an interesting model for studying dynamical systems.

  • Lorenz equations serve as a benchmark for testing neural network predictions.
  • The chaotic nature of the system provides a challenging environment for modeling.
  • Understanding these equations is crucial for setting up the neural network training.
# 0:44

Data Generation for Training

How is the training data generated?

Training data is generated by simulating 100 trajectories of the Lorenz system over a time domain, collecting input-output pairs for the neural network.

  • Data is collected in pairs: input at time t and output at time t plus delta t.
  • The simulation runs for multiple initial conditions to create a diverse dataset.
  • Accurate time stepping is crucial for effective training.
# 1:10

Neural Network Architecture Setup

What is the architecture of the neural network?

The neural network is a feed forward architecture that maps the current state to the next state using defined layers and activation functions.

  • The architecture is customizable based on the problem requirements.
  • Input is three-dimensional, corresponding to the state variables of the Lorenz system.
  • Training involves optimizing weights to minimize prediction error.
# 1:50

Training the Neural Network

How is the neural network trained?

The neural network is trained using the generated data through multiple epochs, applying gradient descent to optimize its weights.

  • Training involves adjusting the model to learn the mapping from current to future states.
  • The process is similar to curve fitting, where the model parameters are optimized.
  • Effective training requires careful selection of epochs and batch sizes.
# 2:30

Testing the Neural Network

How is the trained model tested?

The model is tested by using a new initial condition to predict future states and comparing these predictions to the true values obtained from the Lorenz equations.

  • Testing involves unrolling the neural network to generate future predictions.
  • Comparison with the true model helps evaluate the accuracy of the neural network.
  • The model's ability to track the true system indicates its effectiveness.
# 3:40

Results and Observations

What are the results of the neural network's predictions?

The neural network's predictions closely follow the true trajectories of the Lorenz system, demonstrating its ability to learn the underlying dynamics.

  • The neural network captures the essential features of the chaotic system.
  • Despite initial divergence, the model shows promising predictive capabilities.
  • The approach highlights the potential of data-driven modeling in chaotic systems.
# 4:40

Advantages of Data-Driven Modeling

What are the advantages of using a data-driven approach?

Data-driven modeling allows for predictions without needing the governing equations, relying solely on trajectory information.

  • This method is beneficial when governing equations are unknown or difficult to obtain.
  • It emphasizes the power of neural networks in learning from data.
  • The approach can be applied to various systems where traditional modeling is challenging.

Transcript

0:08 We're going to return now to training neural nets. And what we're going to do is train another feed forward neural network. The last one we did was just a simple curve fit, but now we're going to do it try to model sort of a dynamical system or let's say something that might come from governing equations. And so what we're going to try to do here is build a neural network for time sequence data. It's going to be a feed forward neural network and I'm going to look at at how the dynamics evolve and I'm going to try to train a neural network to understand how to go from t to t plus delta t. So in some sense a fairly simple task that we we build numerical steppers to do this and those are just tailaylor series based. We're going to train a neural network to try to do some kind of operation like this.

0:51 I'm going to show you walk through the code explicitly and show you how well it does in practice. So it's a different application. It's not a classification task. It is just actually learning a map from one time point to the delta t into the future. Okay, that's going to be the goal. And again, this is all machine learning using sort of neural networks. All I'm training here are feed for neural networks, but that's a really good place to start. Even though there's the diversity of neural networks available to you are are are many this is a good starting point for learning how to to program and and build architectures.

1:29 So let's go to the code. So the code itself, first of all, I want to start off with my model. And here's going to be the model. It's going to be the Loren equations. So, Loren's equations are a three 3x3 system of differential equations and exhibits really interesting dynamical behavior including chaotic behavior. It's it's a fairly simple system and you know if you're using a a a solver OD solver in Python here is the definition of the right hand side. This is just something you would define that differential equation and you use an OD int or any one of the steppers that you have in Python to walk this forward into the future. Okay. And so this is the code block associated with the lorens. And so we're going to do is we're going to do simulations of this. And by the way, when you walk this forward into the future, we're typically doing it with something like a a runga method. So we already know how to go from t to t plus delta t with rungakuta given that we know this right hand side.

2:29 But now we're going to take a datadriven approach where all I have is the data let's say in steps of delta t from zero to capital t. And I want to learn how to use that data to build a time stepper. So I don't even necessarily have to know the dynamical system. I just have the data for this. Okay. And I want to still figure out how do I build this time stepper to emulate this differential equation. So we're going to create the data itself and then we're going to build this neural network emulator for it.

3:00 So the goal when we come back to this broader framework of curve fitting right curve fitting is basically take an input to an output through some model f whose parameters are theta. That's a generic regression. This is what it looks like in terms of the stepping scheme. I want to take the solution at time t of n and map it to solution at time t of n plus one with some model f which has parameters theta. And this model f is going to be a neural network and theta is going to be the weights of that neural network. So remember this is just curve fitting. Just want to emphasize that always always it's curve fitting.

3:42 So I'm gonna have to optimize to find the weights given that I've picked a model. Right? So in a curve fit, given that I have a line, you have to find the slope and the offset of the line. That's how you do a curve fit. This how I do curve fit. Here I pick a neural network, find all the weights of that neural network. Okay. So here's going to be the architecture. It's going to be a a simple feed forward architecture. I'm going to go from the input y ofn. In other words, the solution of the neural network at y ofn. I'm gonna map it to the solution at y of n plus one. And I might have a certain number of layers, sizes, and so forth. So this is just a map. It's a map in time from t to t plus delta t. And I'm going to train it on some trajectory data that we get from simulation and see how well does this thing work is in something like building a neural network proxy for a time stepper. Okay, which is sort of an important task that we might have in you know physics-based modeling.

4:42 All right. So, first let's make some data. So, I'm going to pick some time step 0.01. So, this is the clock I'm going to be saving data on. I'm going to pick a time domain eight. And so, I'm going to go from t goes from zero to eight in steps of 0.01. Okay. So, that's going to be my trajectory information. And I'm going to pick a 100 trajectories. So, in other words, I'm not going to just do one model. I'm going to just take a bunch of random data, random initial conditions, run 100 models from time zero to time 8, and that's going to be my data set. Okay, for that Loren model. Now, I'm going to already kind of set this up. So, I'm going to have input data and output data. Let me talk about what those are.

5:27 The input data is going to be the data at time t. The output data will be that data at time t plus delta t. So as I run these simulations, I'm going to have pairs of data. Oh, this is the data at time zero for the input data. The output data has time one. This is the input data at time one for the input. That's time two in the output data. So I come in these pairs and I put them all in here. Input to output. The input is time t. The output is t plus delta t. And I'm going collect it for all of these 100 trajectories.

6:03 for all of these 800 time steps that I have in here or 8,000 time steps, excuse me. So, right goes from time zero to eight in steps of 0.01. So, there's 8,000 time points each and but then and there's 100 trajectories. So, that's my data set and I collect all of the out the data into an input output file. And this is what I'm going to use for training. This is where I'm going to train my neural network right here.

6:24 Okay. Okay. So, let's set this up in a loop. So, this is the loop here. It's going to go through this loop a 100 times. It's going to call on this code right here. So, here we go. We're going to run this code right here. It's going to do the OD solver. And notice something I did here. I cranked the tolerances way down. So, I want this to be as accurate as possible. So, when I collect the data, I have very accurate time stepping data. And so, I run this thing over and over a 100 times. And then I save my input output pairs. I'm going to plot everything here in a moment. And so then I've generated the data I want. I have a 100 trajectories for different initial conditions. And the initial conditions are right here.

7:09 So I make random initial conditions distributed, you know, times 30. So I make a a random vector. And then I just pick random X, Y, and Z initial points. Run it for time 0 to 8. Save the data. Do it again. Do it again. Do it again. And then I have the input output data here. Okay. And I'm going to use this input output data to train a neural network to learn how to go from t to t plus delta t. In other words, I'm going to learn a map that walks me one delta t into the future.

7:41 Okay, so let's go for it. Well, by the way, here's a picture. This is the this is the training data. This is what I'm doing here. so what you're looking at here is the blue balls are the initial conditions. and these are all trajectories that are colored different colors and that map out the Lauren system. So many of you have already seen pictures of of this Loren system. And so, but this is I'm just showing you the full training data set, right? It's 8,000 points per trajectory, right? And it's got X, Y, and Z. So, it's 8,000 by three.

8:19 And I've got 100 trajectories times 100. That's my training data set size. So perfectly reasonable to be working on my laptop. Okay, to do a training run on my laptop. So I just wanted to visualize that because I think it's important that you see what we're actually doing. And once it's seen this, the question is, can it learn how to map from T to T plus delta T? All right. So, first of all, very much like our previous code, we we import all the torch PyTorch objects that we want, but now we're going to basically turn these into torch objects right here. Okay, so this is what this is doing. We take the input and the output data, turn them into torch objects so that they're ready now for being used in torch itself. Okay, here's the code block that we want.

9:10 so we're going to start training this thing. We're going to have to build out the neural network. and let me see here. So here's the training. We're going to go through a number of epics, right? We get to determine the number of epics, the batch size. This is all here. And we're going to basically run this thing directly in here. And we're going to train it with on a feed forward neural network which I believe hold on where did I not have it in here like sorry I'm going to go back okay I think I didn't have the feed forward neural network but we saw an example of a feed forward neural network in our previous work here. So actually I actually built a feed forward neural network here. So if you go to the feed forward neural network, this is actually in a code block and it's very much like we did in the last example. You define how many layers you want, how big the layers are, what the activation functions are. In the previous lecture on that curve fit, we actually build we built a feed forward neural network with four layers and then we made a customizable one. So it's it's exactly the same infrastructure here. The input to this neural network is three-dimensional. It's the XYZ.

10:30 And then you decide how many points you want to go to layer 1 2 3 4 5 and what the how many layers you want, what the activation functions are. And then you go back down to three, which is the solution XYZ at T plus delta T. Here sets it all up. Once you have that defined, you basically do the training and this thing marches it and does gradient descent towards the solution. So that's the training of the neural network and it's basically learning with all those trajectory information how to go from t to t plus delta t across all of those 100 trajectories which have 8,000 time points. And it's learning a stepper. Okay.

11:10 So once it's done training, we can actually then go execute here. Oops, this clicker is terrible. here's the execution stage. So now what I'm going to do now that I've got this model trained, what I'm going to do now is make two new, you know, a new trajectory. I'm going to pick a new random data point as an initial condition. Run that. And that's what this does here. So I'm going to just random new point, run the solution.

11:37 Okay, so I'm running it with an integrator as if I know the dynamical system. And so this is going to be my truth. So now this is a test set. I didn't have this in the training. It's a new trajectory. So I'm going to put that in. I'm going to run it from time 0 to 8. And what I'm going to do with this initial condition now is also say with that initial condition, remember my neural network learned how to go from t to t plus delta t. So I can take this initial condition as initial condition put it into my neural network. So x of0 in other words the solution at time zero. It's going to give me the solution delta t later. Let's call that x1. I can take the x1 put it back into the neural network to get x2. Put x2 in to get x3 and so forth. In other words, I can unroll my neural network to produce a future state prediction of time stepping off the same initial condition. And what I want to do is to see how well did my neural network learn how to do time stepping is I want to compare it to the truth which is this model here.

12:43 All right let's see here there this is the model. So now here's the input to the neural network and it's exactly what I'm saying to you which is you basically throw this into the neural network right here net x knot this is the this is the initial condition that you have is going to give you the next time step and then you use that you update the old time step you put it back in here to get the next time step and the next time step and you save your data. So this here is basically unrolling into the future with your neural network. So you've trained it.

13:19 Now you march it forward in time. Okay? And we're going to march it forward in time 8,000 steps just like our test data had which was 8,000 steps. And then we're going to compare the two. There it is. So here's a picture of this. The blue ball is the new initial condition. It's a test set. So I actually don't have it in the training data set. And what you're seeing here is a solid line and a dotted line. The solid line is me running the OD solver.

13:59 In other words, the truth. The dotted line is what's coming from this neural network that learned how to step. And you can see both of them actually they track very well. they eventually diverge from each other. But you can see in any case the neural network is really getting the right features of the evolution of that data. In fact, let me show you one other picture here which is exactly this one here which is the X component, Y component and Z component.

14:24 As I march forward in time, you can see the difference between the neural network and the OD solver. By the way, this is interesting because what we know about Lauren's that even if you have the right model, even if I did a simulation of this again, if I just perturbed the initial conditions a little bit, I know I'll fall off. But notice they both behave in the right way. In other words, they both behave like the Loren system.

14:50 And so, it's kind of remarkable that the neural network has learned how to behave like the Loren system. Even though this is a chaotic system, it gives you these nice smooth trajectories and you can run them way out. and in fact the unroll seems to actually be be fairly stable on the training data set we have. And so this is just an example of how you can use this neural network. And by the way, here's the interesting thing about this.

15:13 essentially the neural network doesn't have access to the Loren equations. So it's equation free. If it just has trajectory information, it's learning how to walk it to the future, right? And so you can see the big advantage of this. Oops. The big advantage of this is that if you walk this into the future, right, the nor the way we normally walk things into the future is I have the governing equations. But suppose I actually only have sensors or data that I collect on a system that I don't have the underlying governing equations. Well, if you have all these trajectory information, you can build a model for how to walk into the future with it. And so you can start saying, well, here's what my sensors have going going on. Can I learn how to make predictions about the future state of the system? And this is a model that we trained here, which is now equation free, right? It only learned on the trajectory information or the sensor information. It doesn't know about the governing equations. and yet I can still unroll it into the future without having recourse to have access to the governing equations. So this kind of reflects some of the power of neural networks, right?

16:28 And again here I'm not extrapolating. I'm really interpolating in the sense that I trained it on all of this data, different trajectories, different initial conditions, but the parameters of the of the Loren itself remained fixed throughout all of those different training runs. And so I I got a fairly good model out of this. You can download this notebook and play around with this yourself and and start seeing how well it works. And there's some exercises also homeworks to go along with this as we go.

Summary

The video discusses training a feedforward neural network to model a dynamical system, specifically using the Lorenz equations to predict future states based on time-sequence data. The approach emphasizes learning a mapping from one time point to the next without needing to know the underlying governing equations, showcasing the potential of neural networks in data-driven modeling.

- The goal is to train a neural network to predict future states of a dynamical system using time-sequence data.
- The Lorenz equations serve as the model for generating training data, exhibiting chaotic behavior.
- The training involves creating input-output pairs from simulation data, where inputs are states at time t and outputs are states at time t + delta t.
- A feedforward neural network architecture is defined, with the input being three-dimensional (X, Y, Z) and the output corresponding to the next time step.
- The network is trained using gradient descent to optimize its weights based on the trajectory data.
- After training, the neural network is tested on new initial conditions to evaluate its predictive capability against the truth from an ordinary differential equation solver.
- The results show that the neural network effectively learns the dynamics of the Lorenz system, even without direct access to the equations.
- This method illustrates the power of neural networks in creating models based solely on observed data, making them useful for systems where governing equations are unknown.

Questions Answered

What is the focus of this training session?

The session focuses on training a feed forward neural network to model a dynamical system using time sequence data.

What are Lorenz equations and their significance?

Lorenz equations are a system of differential equations that exhibit chaotic behavior, making them an interesting model for studying dynamical systems.

How is the training data generated?

Training data is generated by simulating 100 trajectories of the Lorenz system over a time domain, collecting input-output pairs for the neural network.

What is the architecture of the neural network?

The neural network is a feed forward architecture that maps the current state to the next state using defined layers and activation functions.

How is the neural network trained?

The neural network is trained using the generated data through multiple epochs, applying gradient descent to optimize its weights.

How is the trained model tested?

The model is tested by using a new initial condition to predict future states and comparing these predictions to the true values obtained from the Lorenz equations.

What are the results of the neural network's predictions?

The neural network's predictions closely follow the true trajectories of the Lorenz system, demonstrating its ability to learn the underlying dynamics.

What are the advantages of using a data-driven approach?

Data-driven modeling allows for predictions without needing the governing equations, relying solely on trajectory information.

© transcribe · For agents Built with care and craft by Gokul Rajaram