Keras1-Introduction Two KEras
Keras1-Introduction Two KEras
1.1.1.What is Keras?
Keras is an open source deep learning library that enables fast experimentation with neural networks. It runs on top of other
frameworks like Tensorflow, Theano or CNTK. And it was created by French AI researcher François Chollet.
Keras + TensorFlow
It's the best moment to be learning Keras. Keras is now fully integrated into TensorFlow 2.0, so you can use the best of both worlds as
needed and in the same code pipeline. If as you dive into deep learning, you find yourself needing to use low-level features, for
instance to have a finer control of how your network applies gradients, you could use TensorFlow and tweak whatever you need.
Feature Engineering
Now that you know better what Keras is and why to use it, perhaps we should discuss when and why to use a neural network in the first
place. Neural networks are good feature extractors, since they learn the best way to make sense of unstructured data. Previously, it
was the domain expert that had to set rules based on experimentation and heuristics to extract the relevant features of data. Neural
networks can learn the best features and their combination, they can perform feature engineering themselves. That's why they are so
useful. But what is unstructured data?
Unstructured data
Unstructured data is data that is not easily put into a table. For instance, sound, Videos, images, etc. It's also the type of data where
performing feature engineering can be more challenging, that's why leaving this task to neural networks is a good idea.
A NEURAL NETWORK
In a nutshell, a neural network is a machine learning algorithm that is fetch with training data through its input layer to then predict a
value at its output layer.
Parameters
Each connection from one neuron to another has an associated weight, w. Each neuron, except the input layer which just holds
the input value, also has an extra weight and we call this the bias weight, b. During feed-forward our input gets transformed by
weight multiplications and additions at each layer, the output of each neuron can also get transformed by the application of what
we call an activation function.
https://onedrive.live.com/redir?resid=F8B40D88517D3A9B%212246&page=Edit&wd=target%28DataCamp.one%7C9d25a080-e50e-1e4c-a91a-f55a08b0a4b5… 1/6
4/24/24, 5:57 PM OneNote
Gradient descent
Learning in neural networks consists of tuning the weights or parameters to give the desired output. One way of achieving this is
by using the famous gradient descent algorithm and applying weight updates incrementally via a process known as back-
propagation. That was a lot of theory! The code in Keras is much simpler as we will see now
Adding activations
For instance, this is how we'd add a ReLU activation to our hidden layer. Don't worry about the choice of activation functions, that
will be covered later on in the course
Visualize parameter
That's why we see that this layer has 8 parameters: 6 parameters or weights come from the connections of the 3 input neurons to
the 2 neurons in this layer, the missing 2 parameters come from the bias weights, b0 and b1, 1 per each neuron in the hidden
layer.
These add up to 8 different parameters. Just what we had in our summary.
https://onedrive.live.com/redir?resid=F8B40D88517D3A9B%212246&page=Edit&wd=target%28DataCamp.one%7C9d25a080-e50e-1e4c-a91a-f55a08b0a4b5… 2/6
4/24/24, 5:57 PM OneNote
EXERCISE: You're going to build a simple neural network to get a feeling of how quickly it is to accomplish this in Keras.
You will build a network that takes two numbers as an input, passes them through a hidden layer of 10 neurons, and finally outputs
a single non-constrained number.
A non-constrained output can be obtained by avoiding setting an activation function in the output layer. This is useful for problems
like regression, when we want our output to be able to take any non-constrained value.
Counting parameters
You've just created a neural network. But you're going to create a new one now, taking some time to think about the weights of
each layer. The Keras Dense layer and the Sequential model are already loaded for you to use.
This is the network you will be creating
Compiling
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 choose ADAM as our optimizer and mean
squared 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!
Training
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 X_train, the
labels in y_train and the number of epochs to train for. During an epoch, our entire training data passes through the network and
the respective weight updates take place using back-propagation. As our model is being trained, we will get some output showing
its progress. We can see the model is improving since the mean squared error loss is decreasing at each epoch
https://onedrive.live.com/redir?resid=F8B40D88517D3A9B%212246&page=Edit&wd=target%28DataCamp.one%7C9d25a080-e50e-1e4c-a91a-f55a08b0a4b5… 3/6
4/24/24, 5:57 PM OneNote
Predicting
To obtain predictions from our trained model we just need to call predict on the new set of data. We can store the predictions in a
variable for later use. The predictions are just numbers in a numpy array, we will interpret these depending on our dataset and
problem at hand.
Evaluating
To quickly evaluate how well our model performs on unseen data we can use the model's evaluate method. This performs feed-
forward with all samples in our test dataset (X_test). Feed-forward 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 y_test. In this particular example, the model we
trained for 5 epochs before, has a mean squared 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 of similar meteors.
Scientific prediction
Scientist have used this data alongside their knowledge to estimate an 80-minute orbit, that is, an orbit from -40 minutes to +40
minutes. t=0 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!
Your task
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 then extrapolate your predictions to an 80-minute orbit to see how it
compares to the scientists prediction. Will your orbit be similar to that of the scientists or are we facing the extinction of humanity
as we know it?
https://onedrive.live.com/redir?resid=F8B40D88517D3A9B%212246&page=Edit&wd=target%28DataCamp.one%7C9d25a080-e50e-1e4c-a91a-f55a08b0a4b5… 4/6
4/24/24, 5:57 PM OneNote
You will build a simple regression model to predict the orbit of the meteor!
Your training data consist of measurements taken at time steps from -10 minutes before the impact region to +10 minutes after.
Each time step can be viewed as an X coordinate in our graph, which has an associated position Y for the meteor orbit at that time
step.
Note that you can view this problem as approximating a quadratic function via the use of neural networks.
This data is stored in two numpy arrays: one called time_steps , what we call features, and another called y_positions, with
the labels. Go on and build your model! It should be able to predict the y positions for the meteor orbit at future time steps.
Keras Sequential model and Dense layers are available for you to use.
Training
You're going to train your first model in this course, and for a good cause!
Remember that before training your Keras models you need to compile them. This can be done with the .compile() method.
The .compile() method takes arguments such as the optimizer, used for weight updating, and the loss function, which is what we
want to minimize. Training your model is as easy as calling the .fit() method, passing on the features, labels and a number
of epochs to train for.
The regression model you built in the previous exercise is loaded for you to use, along with the time_steps and y_positions data.
Train it and evaluate it on this very same data, let's see if your model can learn the meteor's trajectory.
https://onedrive.live.com/redir?resid=F8B40D88517D3A9B%212246&page=Edit&wd=target%28DataCamp.one%7C9d25a080-e50e-1e4c-a91a-f55a08b0a4b5… 5/6
4/24/24, 5:57 PM OneNote
https://onedrive.live.com/redir?resid=F8B40D88517D3A9B%212246&page=Edit&wd=target%28DataCamp.one%7C9d25a080-e50e-1e4c-a91a-f55a08b0a4b5… 6/6