官术网_书友最值得收藏!

Keras functional API model architecture

The functional API is much better when you want to do something that diverges from the basic idea of having an input, a succession of levels, and an output, for example, models with multiple inputs, multiple outputs, or a more complex internal structure, such as using the output of a given layer as an input to multiple layers or, on the contrary, combining the output of different layers to use them together as an input of another level.

In fact, as already said, the functional API allows you to create models with greater flexibility. We can easily define models in which the levels are connected in different ways and not just from the previous level to the next. In fact, we can link a layer to any other level, thus creating complex networks.

To understand the difference between the two models that Keras offers, we will use a simple example. This is a densely connected network of the type already seen in The Keras sequential model architecture section. In a densely connected network, every input is connected to every output by a weight, which is generally followed by a non-linear activation function. Again, we recommend it for its simplicity. The first step is solved by importing the classes that we will use later for the construction of the model. We will run the example using the following steps:

We will begin by importing the required libraries using the following code block:

from keras.layers import Input, Dense
from keras.models import Model

Three layer classes have been imported: Input, Dense, and Model.

Then, we have to instantiate a Keras Tensor. In fact, in this case we must define an autonomous input level that specifies the shape of the input data (tensor). The input layer accepts a shape argument, which is a tuple indicating the dimensionality of the input data:

InputTensor = Input(shape=(100,))

This returns a tensor.

A Keras tensor is a tensor object from the underlying backend that we can add to certain attributes that allow us to construct a Keras model only by knowing the inputs and outputs of the model.

Now, we can define the layers using the following code block:

H1 = Dense(10, activation='relu')(InputTensor)

The first dense layer is created, which connects the input layer output (InputTensor) as the input to the dense layer, (x). It is this way of connecting layers (layers by layer) that gives the functional API its flexibility. A layer instance is callable on a tensor, and returns a tensor.

Let's move on to the next layer:

H2 = Dense(20, activation='relu')(H1)

So, a second dense layer is created, that connects the Dense layer output, (x), as the input to the other dense layer, (H2).

Let's move on to the final layer creation:

Output = Dense(1, activation='softmax')(H2)

Finally, a third dense layer is created that connects the Dense layer output, (H2), as the input to the other dense layer, (Output).

Now, we can create a model that includes the Input layer and three Dense layers:

model = Model(inputs=InputTensor, outputs= Output)

The model created has 100 inputs, two hidden layers with 10 and 20 neurons, and an output layer with one output.

To print a summary of the model, simply type the following command:

model.summary()

In the following screenshot, we can see the results:

All these terms will become clearer in the following chapters.

主站蜘蛛池模板: 临城县| 奉节县| 荃湾区| 乌兰县| 九江县| 永川市| 疏勒县| 峨边| 西平县| 新疆| 晴隆县| 常熟市| 祁门县| 泾源县| 五寨县| 鲜城| 高密市| 安陆市| 巴塘县| 彭山县| 云林县| 赤城县| 杂多县| 韶山市| 石首市| 恩施市| 延吉市| 永吉县| 都安| 清原| 原阳县| 玛曲县| 金华市| 西畴县| 浦江县| 商水县| 阜新| 民权县| 堆龙德庆县| 鄱阳县| 喜德县|