Python Tutorial: Surviving a meteor strike

Learn to build and train a Keras sequential model using the ADAM optimizer and mean squared error loss. The tutorial uses 20 minutes of historical meteor path data to predict an 80-minute orbit and compares the model's forecast to scientists' estimates to verify Earth's safety.
Chapters
Welcome back. We are going to learn just what you are missing to be able to save the Earth from a meteor strike. But first, let's see how to compile, train and predict with your models. In the previous lesson, we saw how easy it was to create a model with Keras. You instantiate your sequential model, add a couple of layers under activations, and that's it. You've built a simple model in no time. A model needs to be compiled before training. We can compile our model by calling the compile method. On it, the compile method receives an optimizer, which we can see as the algorithm that will be used to update our neural network weights, and a loss function, which is the function we want to minimize during training. In this case, we chose ADAM as our optimizer and mean square error as our loss function. Optimizers and loss functions will be covered later on in the course, so don't worry about it for now. Compiling our model produces no output. Our model is now ready to train. Creating a model is useless if we don't train it. We train our model by calling the FIT method and passing the features in xtrain. The labels in ytrain and the number of epochs to train for an epoch corresponds to our entire training data passing through the network once and the respective weight updates during backpropagation. As our model is being trained, we will get some output showing the progress we can see. The model is improving since the mean square error lost is decreasing at each epoch. To obtain predictions from our training model, we just need to code predict of. On the new set of data, we can store the predictions in a variable for later use. The predictions are just numbers in an numpy array. We will interpret this depending on our dataset and program at hand. To quickly evaluate how well our model performs on unseen data, we can use the model's evaluate method. This performs FeedForward with all samples in our test dataset. XTest FeedForward consists in computing a model's outputs from a given set of inputs. It then computes the error, comparing the results to the true values stored in ytest. In this particular example, the model we trained for 5 epochs before has a mean square error of 0.25. Are you ready? A meteor is approaching the Earth and we want to make sure it won't take us to extinction. A group of scientists is trying to estimate the orbit by using historical data gathered about previous orbits or similar meteors. Scientists have used this data alongside their knowledge to estimate an 80 minute orbit, that is an orbit from minus 40 minutes to 40 minutes. T0 corresponds to the time of crossing the impact region. It looks like the meteor will be close. Perhaps it won't hit us, but we must make sure we are right. You have data for the path a previous meteor took during a period of 20 minutes. 10 minutes before and 10 minutes after crossing the impact region. You will train a model on this data and extrapolate your predictions to an 80 minute orbit to see how it compares to to the scientist's prediction. Will your orbit be similar to that of the scientists? Where we don't die just by a small bit. Let's find out.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







