Keras: Merge和merge区别、Sequencial()和Model()区别

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/quiet_girl/article/details/84106625

一、Merge和merge

1、Merge和merge区别

Keras中提供了Merge和merge两个不同的功能(新的版本中可能Merge功能已经删除,亲测:Keras2.0.4中Merge和merge功能都是存在的,但是Keras2.2.4中Merge功能已经删除)。

关于这两者的区别,请详见链接:“Merge” versus “merge”, what is the difference?
这里主要提一下主要区别,Merge操作对象是layer, merge操作对象是tensor。具体为(来自上述链接):
(1)Merge的使用

Merge is a layer.
Merge takes layers as input
Merge is usually used with Sequential models

示例:

left = Sequential()
left.add(...)
left.add(...)

right = Sequential()
right.ad(...)
right.add(...)

model = Sequential()
model.add(Merge([left, right]))
model.add(...)

(2)merge的使用

merge is a function.
merge takes tensors as input.
merge is a wrapper around Merge.
merge is used in Functional API

示例:

a = Input((10,))
b = Dense(10)(a)
c = Dense(10)(a)
d = merge([b, c])
model = Model(a, d)

2、遇到的问题:

在Keras2.2.4中,Merge已经删除,使用merge时会报错: TypeError: ‘module’ object is not callable。
解决方法一:(来自博客keras中merge用法下的评论)

将keras版本降到2.1.6之前(含2.1.6)

解决方法二:
若实现的是concat的功能,则可使用以下方法替代merge([tensor1, tensor2], mode='concat', concat_axis=3)

from keras.layers import concatenate
merge = concatenate([tensor1, tensor2], axis=3)

二、Model()和Sequencial()

1、Model()和Sequencial()区别

参考博客:Keras Models: Sequential vs. Functional

Sequencial()可以用于逐层创建网络。
Model() 比Sequencial()更灵活,其除了可以逐层创建网络之外,还可以进行2个网络的合并,网络的拼接等。

(1)Sequencial()模型

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

model = Sequential()
model.add(Dense(2, input_dim=1))
model.add(Dense(1))

Sequencial()可以用来满足基本的网络训练的功能,也简单易实现,但是其仍具有以下缺点:
①不支持多输入
②不支持多输出
③不支持模型网络层重用

(2)Model()模型

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

# Define the input
#   Unlike the Sequential model, you must create and define 
#   a standalone "Input" layer that specifies the shape of input 
#   data. The input layer takes a "shape" argument, which is a 
#   tuple that indicates the dimensionality of the input data.
#   When input data is one-dimensional, such as the MLP, the shape 
#   must explicitly leave room for the shape of the mini-batch size 
#   used when splitting the data when training the network. Hence, 
#   the shape tuple is always defined with a hanging last dimension.
#   For instance, "(2,)", as in the example below:
visible = Input(shape=(2,))  

# Connecting layers
#   The layers in the model are connected pairwise.
#   This is done by specifying where the input comes from when 
#   defining each new layer. A bracket notation is used, such that 
#   after the layer is created, the layer from which the input to 
#   the current layer comes from is specified.
#   Note how the "visible" layer connects to the "Dense" layer:
hidden = Dense(2)(visible)  

# Create the model
#   After creating all of your model layers and connecting them 
#   together, you must then define the model.
#   As with the Sequential API, the model is the thing that you can
#   summarize, fit, evaluate, and use to make predictions.
#   Keras provides a "Model" class that you can use to create a model 
#   from your created layers. It requires that you only specify the 
#   input and output layers. For example:
model = Model(inputs=visible, outputs=hidden)

Model()模型更灵活,可以很好地解决上述Sequencial()存在的问题。

猜你喜欢

转载自blog.csdn.net/quiet_girl/article/details/84106625