- Keras 2.x Projects
- Giuseppe Ciaburro
- 603字
- 2021-07-02 14:36:13
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.
- 火格局的時空變異及其在電網(wǎng)防火中的應(yīng)用
- OpenStack for Architects
- 傳感器技術(shù)應(yīng)用
- 80x86/Pentium微型計算機(jī)原理及應(yīng)用
- Docker Quick Start Guide
- 機(jī)器人創(chuàng)新實(shí)訓(xùn)教程
- 可編程序控制器應(yīng)用實(shí)訓(xùn)(三菱機(jī)型)
- 計算機(jī)網(wǎng)絡(luò)原理與技術(shù)
- 悟透JavaScript
- 網(wǎng)絡(luò)管理工具實(shí)用詳解
- Photoshop行業(yè)應(yīng)用基礎(chǔ)
- 水晶石影視動畫精粹:After Effects & Nuke 影視后期合成
- 計算機(jī)組成與操作系統(tǒng)
- PowerMill 2020五軸數(shù)控加工編程應(yīng)用實(shí)例
- ZigBee無線通信技術(shù)應(yīng)用開發(fā)