当前位置:网站首页>Functional model
Functional model
2022-07-16 08:21:00 【booze-J】
article
One 、 Build a single channel model
Build a single channel model , Use sequential (Sequential) Models and functions (Functional) The model can be built .
1. sequential (Sequential) Model structures,
Build code :
# Define sequential models
model = Sequential()
# The first convolution layer
# input_shape Input plane
# filters Convolution kernel / Number of filters
# kernel_size Convolution window size
# strides step
# padding padding The way same/valid
# activation Activation function
model.add(Conv2D(
input_shape = (28,28,1),
filters = 32,
kernel_size = 5,
strides = 1,
padding = 'same',
activation = 'relu'
))
# The first pool
model.add(MaxPooling2D(
pool_size = 2,
strides = 2,
padding = 'same',
))
# Second convolution layer
model.add(Conv2D(64,5,strides=1,padding='same',activation = 'relu'))
# The second pooling layer
model.add(MaxPooling2D(2,2,'same'))
# Flatten the output of the second pool layer into 1 dimension
model.add(Flatten())
# The first full connection layer
model.add(Dense(1024,activation = 'relu'))
# Dropout
model.add(Dropout(0.5))
# The second full connection layer
model.add(Dense(10,activation='softmax'))
2. Functional expression (Functional) Model structures,
Build code :
inputs = Input(shape=(28,28,1))
x = Conv2D(filters=32, kernel_size=5, padding='same', activation='relu')(inputs)
x = MaxPooling2D(pool_size = 2)(x)
x = Conv2D(filters=64, kernel_size=5, padding='same', activation='relu')(x)
x = MaxPooling2D(pool_size = 2)(x)
x = Flatten()(x)
x = Dense(1024,activation = 'relu')(x)
x = Dropout(0.5)(x)
predictions = Dense(10,activation='softmax')(x)
model = Model(inputs=inputs, outputs=predictions)
You need to import from keras.models import Model.
Two 、 Build a multi-channel model
In addition to the common single channel model , There are also many well-known multi-channel models . for example google Of Inception:
It is a typical multi-channel model . sequential (Sequential) A model can only define a model with one channel , If a model has many channels , that Sequential() Sequential models cannot be defined , At this time, only functional models can be used to build network models .
Build some multi-channel Inception Network model code :
from keras.layers import Conv2D, MaxPooling2D, Input
input_img = Input(shape=(256, 256, 3))
tower_1 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img)
tower_1 = Conv2D(64, (3, 3), padding='same', activation='relu')(tower_1)
tower_2 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img)
tower_2 = Conv2D(64, (5, 5), padding='same', activation='relu')(tower_2)
tower_3 = MaxPooling2D((3, 3), strides=(1, 1), padding='same')(input_img)
tower_3 = Conv2D(64, (1, 1), padding='same', activation='relu')(tower_3)
output = keras.layers.concatenate([tower_1, tower_2, tower_3], axis=1)
3、 ... and 、 Complete code
The code running platform is jupyter-notebook, Code blocks in the article , According to jupyter-notebook Written in the order of division in , Run article code , Glue directly into jupyter-notebook that will do .
1. Import third-party library
import numpy as np
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense,Dropout,Conv2D,MaxPooling2D,Flatten,Input
from keras.optimizers import Adam
from keras.models import Model
2. Data processing
# Load data
(x_train,y_train),(x_test,y_test) = mnist.load_data()
# (60000,28,28)->(60000,28,28,1)
x_train = x_train.reshape(-1,28,28,1)/255.0
x_test = x_test.reshape(-1,28,28,1)/255.0
# in one hot Format
y_train = np_utils.to_categorical(y_train,num_classes=10)
y_test = np_utils.to_categorical(y_test,num_classes=10)
3. sequential (Sequential) Model building a single channel network
# Define sequential models
model = Sequential()
# The first convolution layer
# input_shape Input plane
# filters Convolution kernel / Number of filters
# kernel_size Convolution window size
# strides step
# padding padding The way same/valid
# activation Activation function
model.add(Conv2D(
input_shape = (28,28,1),
filters = 32,
kernel_size = 5,
strides = 1,
padding = 'same',
activation = 'relu'
))
# The first pool
model.add(MaxPooling2D(
pool_size = 2,
strides = 2,
padding = 'same',
))
# Second convolution layer
model.add(Conv2D(64,5,strides=1,padding='same',activation = 'relu'))
# The second pooling layer
model.add(MaxPooling2D(2,2,'same'))
# Flatten the output of the second pool layer into 1 dimension
model.add(Flatten())
# The first full connection layer
model.add(Dense(1024,activation = 'relu'))
# Dropout
model.add(Dropout(0.5))
# The second full connection layer
model.add(Dense(10,activation='softmax'))
4. Functional expression (Functional) Model building a single channel network
inputs = Input(shape=(28,28,1))
x = Conv2D(filters=32, kernel_size=5, padding='same', activation='relu')(inputs)
x = MaxPooling2D(pool_size = 2)(x)
x = Conv2D(filters=64, kernel_size=5, padding='same', activation='relu')(x)
x = MaxPooling2D(pool_size = 2)(x)
x = Flatten()(x)
x = Dense(1024,activation = 'relu')(x)
x = Dropout(0.5)(x)
predictions = Dense(10,activation='softmax')(x)
model = Model(inputs=inputs, outputs=predictions)
5. Training models
# Define optimizer
adam = Adam(lr=1e-4)
# Define optimizer ,loss function, The accuracy of calculation during training
model.compile(optimizer=adam,loss='categorical_crossentropy',metrics=['accuracy'])
# Training models
model.fit(x_train,y_train,batch_size=64,epochs=10)
# Evaluation model
loss,accuracy = model.evaluate(x_test,y_test)
print('test loss',loss)
print('test accuracy',accuracy)
6. Functional model to build a multi-channel network

from keras.layers import Conv2D, MaxPooling2D, Input
input_img = Input(shape=(256, 256, 3))
tower_1 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img)
tower_1 = Conv2D(64, (3, 3), padding='same', activation='relu')(tower_1)
tower_2 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img)
tower_2 = Conv2D(64, (5, 5), padding='same', activation='relu')(tower_2)
tower_3 = MaxPooling2D((3, 3), strides=(1, 1), padding='same')(input_img)
tower_3 = Conv2D(64, (1, 1), padding='same', activation='relu')(tower_3)
output = keras.layers.concatenate([tower_1, tower_2, tower_3], axis=1)
边栏推荐
- Fluent: environment construction and project creation
- 小程序毕设作品之微信教室预约小程序毕业设计(6)开题答辩PPT
- [U - boot] u - boot Sandbox compilation Construction and use Summary
- 小程序毕设作品之微信企业公司小程序毕业设计(6)开题答辩PPT
- [u-boot] summary of compilation, construction and use of u-boot sandbox
- Analysis of local anti debugging principle of sojson
- Heterogeneous computing - heterogeneous chip convergence trend
- ora-01153
- [daily question 1] binary search tree and bidirectional linked list
- [daily question 1] judge whether it is a balanced binary tree
猜你喜欢

基于51单片机智能家居监控系统设计仿真(proteus仿真+源码+报告)

使用cpolar建立一个商业网站(申请网站安全证书)

Fluent: environment construction and project creation

Wechat enterprise small program graduation project (6) opening defense ppt

小程序毕设作品之微信企业公司小程序毕业设计(8)毕业设计论文模板

猫狗分類-VGG16-bottleneck

小程序毕设作品之微信企业公司小程序毕业设计(2)小程序功能

C primer plus learning notes - 4. File IO (input / output)

MySQL basic query statement

Profile - sessions_per_user :限制用户会话数
随机推荐
猫狗分类-VGG16-bottleneck
源码编译按照mongoc
解决GD32F20X支持包安装后打开官方例程无法识别芯片问题
最后一篇CSDN博客
【二叉树】在受污染的二叉树中查找元素
golang 处理web post、get请求以及string to json格式的转化
"Everyday Mathematics" serial 59: February 28
The boss recruited a tester with 6 years of experience, which let me see what the ceiling is
MallBook:如何通过供应链结算管理系统推动供应链金融快速落地?
Dart中的库
The source code is compiled according to mongoc
[daily question 1] binary search tree and bidirectional linked list
Send your code into space and develop "the greatest work" together
从全球价值链视角看,京东云数智供应链对未来经济有何影响?
Force buckle 719 Find the distance of the number pair with the smallest K
第54章 业务逻辑之折扣、商品类别实体定义实现
“电信级”运行多年,亚信科技推出核心交易数据库AntDB7.0
突然宣布解散!
Small program graduation project of wechat enterprise company (5) assignment
深度揭秘阿里云函数计算异步任务能力