In keras we can create neural network layers in many ways.

1. Sequential API.
for example


2. Functional
for example


3. Subclassing
for example

class Mymodel(keras.layers.Layers):
    def __init__(self,output):

My Question is:
1. Whether which one to use and their pros and cons.


The sequential API is the simplest. It allows you to declare a sequence of layers with an single input and a single output. Naturally, this API is a good choice for sequential networks - those where data flows through each layer in sequence.

Pros: straightforward to read and write.

Cons: Difficult to accommodate multiple inputs/outputs. Cannot define arbitrary acyclic networks. No layer reuse.

The functional API allows you more freedom in how you connect inputs and outputs of different layers. You can route the output of any layer to any other layer, which allows you to define arbitrary acyclic graphs. Use this API when you need a network architecture which is not sequential.

Pros: More flexibility. Can define arbitrary networks with multiple inputs/outputs, residual layers, etc.

Cons: More effort to read/write. Higher potential for bugs (since you have to connect layers manually)

Subclassing is actually a different mechanism than the sequential or functional API. The APIs above are used to build custom networks by linking together pre-existing layers.

By contrast, subclassing keras.layers.Layer is used to build your own custom layers. This is useful if you are doing cutting-edge research or if you are trying to implement some technique from the literature that is not part of the standard Keras library. After you have created a custom layer, you can use it with either the sequential or functional APIs to include your layer in a network.

Pros: Ultimate flexibility - you can make your layer do whatever you like.

Cons: Massive effort compared to using pre-built layers.

  • $\begingroup$ thank you so much for your time $\endgroup$ – Shiv Jul 27 '20 at 17:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.