Vigneshwaran
Vigneshwaran

Reputation: 45

Difference between implementation of network by Sequential() and directly?

model = tf.keras.Sequential()
model.add(tf.keras.layers.Input(shape=(32, 32, 3)))
model.add(tf.keras.layers.Dense(10))

and

model = tf.keras.layers.Input(shape=(32,32, 3))
model = tf.keras.layers.Dense(10)(model)

What is the difference between these two implementations? I know that both of these do the same thing.

I'm trying to implement the Inception Model using TensorFlow and I'm hitting a problem when I'm implementing the inception_block function. I could adopt 2nd version and get going by the code mentioned below, but I want to implement the same thing using Sequential() and add().

def inception_block(model, filter_map):
  path1 = tf.keras.layers.Conv2D(filter_map[0], (1, 1), padding='same', activation='relu')(model)
  
  path2 = layers.Conv2D(filter_map[1], (1, 1), padding='same', activation='relu')(model)
  path2 = layers.Conv2D(filter_map[2], (1, 1), padding='same', activation='relu')(path2)

  return tf.concat([path1, path2], axis=3)
input_ = tf.keras.layers.Input(shape=(32, 32, 3))
input_tensor = tf.keras.layers.experimental.preprocessing.Resizing(224, 224, interpolation="bilinear", input_shape=x_train.shape[1:])(input_)
x = tf.keras.layers.Conv2D(64, 7, strides=2, padding='same', activation='relu')(input_tensor)
x = tf.keras.layers.MaxPooling2D(3, strides=2)(x)
x = tf.keras.layers.Conv2D(64, 1, strides=1, padding='same', activation='relu')(x)
x = tf.keras.layers.Conv2D(192, 3, strides=1, padding='same', activation='relu')(x)
x = tf.keras.layers.MaxPooling2D(3, strides=2)(x)
x = inception_block(x, [64, 64, 192])
x = tf.keras.layers.MaxPooling(3, strides=2)(x)

Upvotes: 1

Views: 130

Answers (1)

Kaveh
Kaveh

Reputation: 4960

Based on the docs here:

You have 3 ways to define a model:

  • Sequential: (Your First approach) The Sequential model, which is very straightforward (a simple list of layers), but is limited to single-input, single-output stacks of layers (as the name gives away).

  • Functional API: (Your second approach) The Functional API, which is an easy-to-use, fully-featured API that supports arbitrary model architectures. For most people and most use cases, this is what you should be using.

  • Model subclassing: Where you implement everything from scratch on your own. Use this if you have complex, out-of-the-box research use cases.

So, because Inception Model has branches, you cannot use Sequential model to implement. You should either use Functional API or create a subclass from model and implement your model from scratch.

Notice that even Functional API has some limitations. It is only suited to models that are directed acyclic graphs of layers e.g. MobileNet, Inception, etc. (models that have no cycle or loops). For more exotic architectures e.g. dynamic and recursive networks or architectures that may be changed on the fly you have to use Model Subclassing.

Upvotes: 1

Related Questions