In case you are working with deep studying, you’re most likely accustomed to the Sequential mannequin. It’s simple to make use of, and simple, stacking layers one after one other to resolve neural community issues. However as you dive deeper into AI, you’ll discover that many challenges require extra flexibility and complexity. That’s the place Useful API turns out to be useful.
The Useful API in TensorFlow gives a flexible solution to construct refined, non-linear architectures. Whether or not you’re managing a number of inputs and outputs or designing intricate layer connections, this instrument supplies the liberty to innovate and unlock many extra prospects.
TensorFlow Useful API is a strong instrument that permits the creation of extra complicated, and versatile neural community architectures past the chances and limitations of the Sequential fashions. Whereas the Sequential mannequin is linear stacks of layers, it falls quick when coping with extra intricate community issues that require a number of inputs, outputs, or non-linear connections.
With the Useful API, you will have full management over the move of the information inputs by way of the community, enabling you to tailor your individual architectures to your particular necessities. This flexibility is helpful for constructing fashions that:
- Have a number of enter and output layers, that you should course of photographs and textual content concurrently.
- Share layers, equivalent to fashions that reuse the identical layers throughout completely different branches.
- Have non-linear topologies, equivalent to residual networks or Inception modules that embrace skipping connections or parallel branches.
That is significantly useful when coping with superior mannequin structure and duties equivalent to Residual Networks (ResNet), Siamese Networks, object localization, and plenty of extra.
On this article, I’m assuming that you just’re already accustomed to the code to construct a sequential mannequin as proven on these instance:
import tensorflow as tfmannequin = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(28,28))
tf.keras.layers.Dense(128, activation='relu')
tf.keras.layers.Dense(10)
])
Within the offered code snippet, a Sequential mannequin structure is achieved through the use of the Sequential() class. Let’s attempt to construct the identical mannequin structure utilizing the Useful API. Listed below are the next 4 steps:
- Outline the enter layer
- Outline a set of interconnected layers
- Outline the output layers
- Outline the mannequin utilizing the enter and output layers.
Listed below are the code snippet that demonstrated constructing the mannequin architectures:
import tensorflow as tf# Instantiate the enter layer
inputs = tf.keras.Enter(form=(28,28))
# Stack the layers
flatten_layer = tf.keras.layers.Flatten()(inputs)
first_dense = tf.keras.layers.Dense(128, activation='relu')(flatten_layer)
# Outline the output layer
output_layer = tf.keras.layers.Dense(10, activation='softmax')(first_dense)
# Outline the mannequin
mannequin = tf.keras.fashions.Mannequin(inputs=inputs, outputs=output_layer, identify="Mannequin")