Python Tutorial: Keras input and dense layers

DataCamp
DataCamp
31.6 هزار بار بازدید - 4 سال پیش - Want to learn more? Take
Want to learn more? Take the full course at  https://learn.datacamp.com/courses/ad... at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.

---
Hi! I'm Zach Deane-Mayer, and in this course, I'll be teaching you advanced deep learning concepts using the keras functional API.

You will learn how to build functional keras models, including advanced topics such as shared layers, categorical embeddings, multiple inputs, and multiple outputs.

The keras functional API is extremely simple, yet immensely powerful.

By the end of this class, you will build a model that is capable of solving a regression and a classification problem at the same time.

Chapter 1 is a refresher on building simple models, where you will learn to use the keras functional API.

In chapter 2, you will build a keras model with 2 inputs.

In chapter 3, you will learn how to generalize your 2-input model to 3 or more inputs.

And finally, in chapter 4, you will build models with multiple outputs that can solve multiple problems.

You will be using two datasets of college basketball games from American colleges. The first dataset is from the regular season and has the following data: the IDs of the 2 teams that played, whether the first team was home or away, whether the first team won or lost the game, and by how many points the first team won or lost.

For the tournament dataset, you also have the tournament "seed", which is a pre-tournament ranking for each team. These seeds range from 1 to 16, where the best 4 teams get a seed of 1, and the worst 4 teams get a seed of 16.

You will use the difference in the two team's seeds as an input to your model.

Here are the first five rows of both the datasets.

You can see that the team variables are encoded as integers, and the tournament dataset has one additional column: the difference between the tournament seeds for both teams.

Other than the seed difference, the two datasets have identical columns.

Within a given year, a team's roster stays relatively constant, but between years it can change a lot, as seniors graduate, and freshmen start. Therefore, for every year, each school is given a unique integer ID.

Keras models at their simplest, are fundamentally composed of 2 parts: an input layer and an output layer.

To start, I'll define a very simple keras model, which only expects a single input.

I specify this using the Input() function from the keras.layers module.

The number of columns in the input is specified using the shape parameter. This tells the model how much data to expect. Note that the shape argument expects a tuple.

The Input function returns a "tensor."

If you print this tensor, you'll see that it is a tf.Tensor object, which indicates it is ready to be used by our model as input.

Now that we've defined our input layer, let's define the output layer.

Outputs in keras are most commonly a single dense layer, which specifies the shape of the expected output.

In this case, we are expecting our model to predict a single value, so we pass one unit to the dense layer.

If you print the output layer, the result is NOT a tensorflow tensor. It is a function, which takes a tensor as input and produces a tensor as output.

The difference between layers and tensors is key to understanding the keras functional API.

Layers are used to construct a deep learning model, and tensors are used to define the data flow through the model.

In this case, the input layer defines a tensor, which we pass to the output_layer() function.

The final output of our model is a tensor.  It's time for you to build some layers!

#PythonTutorial #Python #DataCamp #Keras #denselayers
4 سال پیش در تاریخ 1399/01/02 منتشر شده است.
31,619 بـار بازدید شده
... بیشتر