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

The Keras sequential model architecture

Keras is structured according to the object-oriented programming methodology. Therefore, the creation of a model is very simple: select the basic architecture and then add the layers necessary to create the desired model. As just mentioned, the sequential model lets you create a layer-by-layer model as a linear stack of layers. However, it is not possible to create models that share levels or that have multiple inputs or outputs.

A sequential model is created by passing a list of layer instances to the constructor. To create a model, we should follow these steps:

1. Import the sequential class from keras.models

2. Stack layers using the .add() method

3. Configure the learning process using the compile() method

4. Import the data

5. Train the model on the train dataset using the .fit() method

The first step is solved by importing the classes that we will use later for the construction of the model. An example of an import is shown in the following command:

from keras.models import Sequential
from keras.layers import Dense, Activation

Three layer classes have been imported: Sequential, Dense, and Activation. Then, we instantiate an object from the Keras.model.Sequential class:

model = Sequential()

All information about your network, such as weights, layers, and operations will be stored in this object.

After instantiating our object, we will move on to adding layers using the add() method:

model.add(Dense(32, activation='relu', input_dim=100))
model.add(Dense(1, activation='sigmoid'))

We have added two Dense layers, which is the basic feedforward fully connected layer.

All operations of a layer can be passed as arguments to the Dense object, as follows:

  • Number of hidden units
  • Activation function
  • Bias
  • Weight/bias initialization
  • Weight/bias regularization
  • Activation regularization

You can create any level in the network using the following command:

model.add (layer_name)

This method will preserve the order of the levels you add. There are lots of layers implemented in Keras. When you add a layer to your model, a gradient operation will be created in the background and it will take care of computing the backward gradient automatically. Before training a model, you need to configure the learning process, which is done via the compile() method; we can see it in the following code block:

model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])

The three arguments that are passed are as follows:

  • An optimizer
  • A loss function
  • A list of metrics

At this point, we have set the model architecture and before proceeding to the training we have to import the data.

In this case, we generate simple dummy data by performing a random sampling using numpy:

import numpy as np
data = np.random.random((1000, 100))
labels = np.random.randint(2, size=(1000, 1))

To train a model, the fit () method is used, as seen in the following code block:

model.fit(data, labels, epochs=10, batch_size=32)

In this way, we have trained the model, iterating on the data in groups of 32 samples. With these few lines of code, we have already built our first network in Keras. This is a simple example of binary classification that uses a single entry model with two classes.

In Keras, to summarize a model, it is possible to use the summary() function. The summary is returned in text format and includes the following information:

  • The layers and their order in the model
  • The output shape of each layer
  • The number of parameters (weights) in each layer
  • The total number of parameters (weights) in the model

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

model.summary()

In the following screenshot, the results are shown:

Here, we can clearly see the output shape and number of weights in each layer.

主站蜘蛛池模板: 华池县| 淮安市| 东乡| 会昌县| 漳州市| 牙克石市| 博客| 贵德县| 咸阳市| 芷江| 双桥区| 彭州市| 万州区| 怀仁县| 蕉岭县| 富蕴县| 丰城市| 理塘县| 莱西市| 大竹县| 深水埗区| 崇明县| 德化县| 潜山县| 福泉市| 酒泉市| 隆化县| 依兰县| 凤阳县| 华阴市| 磐安县| 通化市| 建湖县| 峨边| 翼城县| 门源| 波密县| 六盘水市| 本溪市| 伊宁县| 若尔盖县|