简体   繁体   中英

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. 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:

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.

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.

An exampe of a model built with the functional 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 :

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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