简体   繁体   English

为什么在构建Keras模型时同时使用顺序API和功能API?

[英]Why use both Sequential and Functional API when building a Keras model?

I'm trying to understand the code for a DCGAN made with Keras, that creates a model with the sequential api and then wraps that in a functional api model. 我正在尝试了解使用Keras制作的DCGAN的代码,该代码使用顺序api创建模型,然后将其包装在功能api模型中。 Why include the functional model as opposed to just using the sequential model? 为什么要包括功能模型而不是仅使用顺序模型?

I'm a bit of a beginner and I'm trying to understand the design of this Keras GAN: 我是一个初学者,我试图了解Keras GAN的设计:

https://github.com/eriklindernoren/Keras-GAN/blob/master/dcgan/dcgan.py https://github.com/eriklindernoren/Keras-GAN/blob/master/dcgan/dcgan.py

For example, in building the generator, the model is defined with the Sequential API and then a new model is made with the functional API and the sequential model. 例如,在构建生成器时,使用顺序API定义模型,然后使用功能性API和顺序模型创建新模型。

def build_generator(self):

        model = Sequential()

        model.add(Dense(128 * 7 * 7, activation="relu", input_dim=self.latent_dim))
        model.add(Reshape((7, 7, 128)))
        model.add(UpSampling2D())
        model.add(Conv2D(128, kernel_size=3, padding="same"))
        model.add(BatchNormalization(momentum=0.8))
        model.add(Activation("relu"))
        model.add(UpSampling2D())
        model.add(Conv2D(64, kernel_size=3, padding="same"))
        model.add(BatchNormalization(momentum=0.8))
        model.add(Activation("relu"))
        model.add(Conv2D(self.channels, kernel_size=3, padding="same"))
        model.add(Activation("tanh"))

        model.summary()

        noise = Input(shape=(self.latent_dim,))
        img = model(noise)

        return Model(noise, img)

How is this different from just using the sequential model? 这与仅使用顺序模型有何不同? (And sorry if this is a stupid question) (对不起,如果这是一个愚蠢的问题)

Sequential and functional models are in pratice the same. 顺序模型和功能模型实际上是相同的。 Except that for functional model you can create more complex architectures because every layers are stored with variables. 除了功能模型之外,您还可以创建更复杂的架构,因为每个层都存储有变量。 And it is handy when you don't have unique "linear" model and GANs are among them. 如果您没有独特的“线性”模型,并且其中包括GAN,这将非常方便。

An exampe of a model built with the functional API : 使用功能性API构建的模型的示例:

inputs = Input(shape=(784,))

# a layer instance is callable on a tensor, and returns a tensor
layer1 = Dense(64, activation='relu')(inputs)
layer2 = Dense(64, activation='relu')(layer1)
predictions = Dense(10, activation='softmax')(layer2)

# This creates a model that includes
# the Input layer and three Dense layers
model = Model(inputs=inputs, outputs=predictions)

You clearly see that every layers can be accessed easily. 您清楚地看到,可以轻松访问每个图层。 Whereas the same using the Sequential API : 而使用Sequential API的情况相同:

model = Sequential()
model.add(Input(shape=(784,))
model.add(Dense(64, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(10, activation='softmax'))

Of course for a such simple model the Sequential model is cleaner. 当然,对于这样一个简单的模型,顺序模型更为简洁。 But now if want to add a second model (a generator for example) the functional API is cleaner because you can declare a variable for each layer and add this layer to the model** on the same line** whereas with the Sequential API you can't. 但是现在,如果要添加第二个模型(例如,生成器),则功能性API更为简洁,因为您可以为每一层声明一个变量,然后将该层添加到模型中**在同一行**,而使用顺序API,您可以不能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM