Keras is a high-level deep learning framework that provides a user-friendly interface for building and training neural networks. It is widely used in the field of artificial intelligence and has gained popularity due to its simplicity and flexibility. In this answer, we will discuss the two main ways to use Keras: the Sequential API and the Functional API.
The Sequential API is the simplest and most commonly used way to build neural networks in Keras. It allows you to create a linear stack of layers, where each layer has exactly one input tensor and one output tensor. You can add layers to the network using the `add()` method, specifying the type of layer and its parameters. Here is an example of how to create a simple sequential model in Keras:
python from keras.models import Sequential from keras.layers import Dense model = Sequential() model.add(Dense(units=64, activation='relu', input_shape=(input_dim,))) model.add(Dense(units=10, activation='softmax'))
In this example, we create a sequential model with two layers: a fully connected layer with 64 units and ReLU activation, and an output layer with 10 units and softmax activation. The `input_shape` parameter in the first layer defines the shape of the input data. Once the model is defined, you can compile it using the `compile()` method, specifying the loss function, optimizer, and optional metrics. Finally, you can train the model using the `fit()` method, providing the training data and the number of epochs.
The Functional API, on the other hand, allows for more complex network architectures, including multi-input and multi-output models, shared layers, and non-sequential connections. It is particularly useful when building models with branching or merging layers. Instead of using the `Sequential` class, you define the model as a directed acyclic graph of layers. Each layer is a callable object that takes a tensor as input and returns a tensor as output. Here is an example of how to create a model using the Functional API:
python from keras.models import Model from keras.layers import Input, Dense input_tensor = Input(shape=(input_dim,)) hidden_tensor = Dense(units=64, activation='relu')(input_tensor) output_tensor = Dense(units=10, activation='softmax')(hidden_tensor) model = Model(inputs=input_tensor, outputs=output_tensor)
In this example, we define an input tensor, apply a dense layer with 64 units and ReLU activation, and connect it to an output tensor with 10 units and softmax activation. The `Model` class is then used to create the model, specifying the input and output tensors. Similarly to the Sequential API, you can compile and train the model using the same methods.
Keras provides two main ways to build and train neural networks: the Sequential API and the Functional API. The Sequential API is simpler and suitable for linear stack architectures, while the Functional API allows for more complex network structures. Both APIs offer a user-friendly interface for building and training models in Keras.
Other recent questions and answers regarding Examination review:
- What are the three components that need to be specified when compiling a Keras model?
- What are the activation functions used in the layers of the Keras model in the example?
- What are the steps involved in preprocessing the Fashion-MNIST dataset before training the model?
- How is Keras described in terms of its design and functionality?

