- Keras Deep Learning Cookbook
- Rajdeep Dua Manpreet Singh Ghotra
- 301字
- 2021-06-10 19:38:53
Create a Sequential model
We will create a Sequential network with four layers.
- Layer 1 is a dense layer which has input_shape of (*, 784) and an output_shape of (*, 32)
A dense layer is a regular densely-connected neural network layer. A Dense layer implements the operation output = activation(dot(input, kernel) + bias), where activation is the element-wise activation function passed as the activation argument, kernel is a weights matrix created by the layer, and bias is a bias vector created by the layer. (This is only applicable if use_bias is True).
- Layer 2 is an activation layer with the tanh Activation function applies activation to the incoming tensor:
keras.layers.Activation(activation)
Activation can also be applied as a parameter to the dense layer:
model.add(Dense(64, activation='tanh'))
- Layer 3 is a dense layer with output (*,10)
- Layer 4 has Activation that applies the softmax function:
Activation('softmax')
In mathematics, the softmax function, also called the normalized exponential function, is a generalization of the logistic function that squashes a K-dimensional vector z of arbitrary real values to a K-dimensional vector σ(z) of real values; each entry is in the range (0, 1), and all the entries add up to 1. The following formula shows this:
.

from keras.models import Sequential
from keras.layers import Dense, Activation
model = Sequential([
Dense(32, input_shape=(784,)),
Activation('tanh'),
Dense(10),
Activation('softmax'),
])
print(model.summary())
- The summary of the model created is printed in the following snippet:
Layer (type) Output Shape Param #
=================================================================
dense_1 (Dense) (None, 32) 25120
_________________________________________________________________
activation_1 (Activation) (None, 32) 0
_________________________________________________________________
dense_2 (Dense) (None, 10) 330
_________________________________________________________________
activation_2 (Activation) (None, 10) 0
=================================================================
Total params: 25,450
Trainable params: 25,450
Non-trainable params: 0
_______________________________________________________________
Sequential is a subclass of Model and has some additional methods, as shown in the following sections.
推薦閱讀
- Unreal Engine:Game Development from A to Z
- 高效能辦公必修課:Word圖文處理
- Introduction to DevOps with Kubernetes
- Visual FoxPro 6.0數據庫與程序設計
- 基于ARM 32位高速嵌入式微控制器
- 大數據時代
- 影視后期編輯與合成
- 網中之我:何明升網絡社會論稿
- Godot Engine Game Development Projects
- R Data Analysis Projects
- 三菱FX/Q系列PLC工程實例詳解
- 生成對抗網絡項目實戰
- 貫通Java Web輕量級應用開發
- PowerPoint 2010幻燈片制作高手速成
- MySQL Management and Administration with Navicat