0% found this document useful (0 votes)
42 views6 pages

Keras1-Introduction Two KEras

Uploaded by

Ayşe Bat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views6 pages

Keras1-Introduction Two KEras

Uploaded by

Ayşe Bat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

4/24/24, 5:57 PM OneNote

1.1 Introducing Keras


19 April 2024 Friday 12:12

1. Introduction to Deep Learning with Keras


1.1 INTRODUCING 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.

Why Use Keras

• Fast industry-ready models


• For beginnings and experts
• Less code
• Build any architecture
• Deploy models in multiple platforms like Android, IOS, web-apps etc.

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.

So, when to use neural networks?


If you are dealing with unstructured data, you don't need to interpret the results, and your problem can benefit from a known
architecture, then you probably should use neural networks. For instance, when classifying images of cats and dogs: Images are
unstructured data, we don't care as much about why the network knows it's a cat or a dog, and we can benefit from convolutional neural
networks. So it's wise to use neural networks. You will learn more about the usefulness of convolutional neural networks later on in the
course.

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

The sequential API


Keras allows you to build models in two different ways; using either the Functional API or the Sequential API. We will focus on the
Sequential API. This is a simple, yet very powerful way of building neural networks that will get you covered for most use cases.
With the sequential API you're essentially building a model as a stack of layers. You can start with an input layer.Add a couple of
hidden layers.And finally, end your model by adding an output layer. Let's go through a code example.

Defining a neural network


To create a simple neural network we'd do the following: Import the Sequential model from tensorflow.keras.models. Import a
Dense layer, also known as fully connected layer, from tensorflow.keras.layers. We can then create an instance of a Sequential
model. In this next line of code we add two layers; a 2 neuron Dense fully connected layer, and an input layer consisting of 3
neurons. The input layer is defined with the input_shape parameter and it matches the dimensions of our input data. We finally
add another fully connected layer, this time with 1 neuron. We just built the network to the right.

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

Summarize your model!


Once we've created our model we can call the summary() method on it. This displays a table with 3 columns: The first with the
layers name and type, the second with the shape of the outputs produced by each layer and the third containing the number of
parameters, these are the weights, including the bias weight of each neuron in the layer. When the input layer is defined via the
input_shape parameter, as we did before, it is not shown as a layer in the summary but it's included in the layer where it was
defined, in this case the dense_3 layer.

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.

Summarize your model!


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.

EXERCISE: The Problem at Hand

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

EXERCISE: Specifying a model

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.

Predicting the orbit!


You've already trained a model that approximates the orbit of the meteor approaching Earth and it's loaded for you to use.
Since you trained your model for values between -10 and 10 minutes, your model hasn't yet seen any other values for different
time steps. You will now visualize how your model behaves on unseen data.
If you want to check the source code of plot_orbit, paste show_code(plot_orbit) into the console.
Hurry up, the Earth is running out of time!
Remember np.arange(x,y) produces a range of values from x to y-1. That is the [x, y) interval.

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

You might also like