Kras functional API

1. Introduction

The Keras functional API can build class graph models and share a certain layer between different inputs .

Keras callback function + TensorBoard browser-based visualization tool —> monitor the model during training

1. Why not use the Sequential model?

Because the Sequential model assumes that the network is single-input and single-out, and the layers are stacked linearly . It cannot be done in tasks that require multimodal input. At this time, it is necessary to construct a multi-branch model that can view all input modalities simultaneously and perform joint learning. Or when encountering the same input but multiple outputs, and there is a connection between the outputs, it is necessary to construct a more flexible model that is not linearly stacked. At this time, it is necessary to use Keras's functional API to complete some more complex models (such as multi-input, multi-output, class graph models).

2. Functional API

The functional API can manipulate tensors directly, or use layers as functions, accepting tensors and returning tensors.
example:

model=keras.Sequential([
    keras.layers.Dense(128,activation=tf.nn.relu,input_shape=(28,28,1)),
    keras.layers.Dense(128,activation=tf.nn.relu),
    keras.layers.Dense(10,activation=tf.nn.softmax)
])

Switch to a functional API:

#函数式API实现
input_tensor = Input(shape=(28,28,1))
x = keras.layers.Dense(128,activation=tf.nn.relu)(input_tensor)
x = keras.layers.Dense(128,activation=tf.nn.relu)(x)
ootput_tensor = keras.layers.Dense(10,activation=tf.nn.softmax)(x)

model = Model(input_tensor,output_tensor)  #将输入输出转换为一个模型

When the input is a variable-length text sequence: Input(shape = (None,), dtype = 'int32', name = 'text')

Any model can be treated as a layer by calling it on Input or on the output of another layer. By calling the model, you not only reuse the model's architecture, but also its weights. i.e. models can be nested : models can contain submodels (because models are like layers).
like:

、、、
encoder = keras.Model(encoder_input, encoder_output, name="encoder")
、、、
decoder = keras.Model(decoder_input, decoder_output, name="decoder")
、、、
、、、
autoencoder_input = keras.Input(shape=(28, 28, 1), name="img")
encoded_img = encoder(autoencoder_input)
decoded_img = decoder(encoded_img)
autoencoder = keras.Model(autoencoder_input, decoded_img, name="autoencoder")

3. Multiple input and output models

In a certain place, it needs to be merged. In keras, the add layer, concatenate layer, etc. can be used to perform the merge operation.
For example, there is such a model: (this kind of calculation graph rendering of the model can be implemented using the keras.utils.plot_model method)
insert image description here

There are different outputs in the model, and different loss functions can be assigned to different output layers by specifying the layer:

model.compile(
    optimizer=keras.optimizers.RMSprop(1e-3),
    loss={
    
    
        "priority": keras.losses.BinaryCrossentropy(from_logits=True),
        "department": keras.losses.CategoricalCrossentropy(from_logits=True),
    },
    loss_weights=[1.0, 0.2],
)

Train the model by passing a list of NumPy arrays of inputs and targets:

model.fit(
    {
    
    "title": title_data, "body": body_data, "tags": tags_data},
    {
    
    "priority": priority_targets, "department": dept_targets},
    epochs=2,
    batch_size=32,
)

4. Commonly used codes

Create an input node
inputs = keras.Input(shape=(784,))
img_inputs = keras.Input(shape=(32, 32, 3))

add layer
x = layers.Dense(64, activation="relu")(x)

Create a Model by specifying the model's inputs and outputs in a layer computation graph
model = keras.Model(inputs=inputs, outputs=outputs, name="mnist_model")

Model visualization:

Draw the model as a computational graph

keras.utils.plot_model(model, “my_first_model.png”)

Display the input and output shapes of each layer in the drawn computation graph

keras.utils.plot_model(model, “my_first_model_with_shape_info.png”, show_shapes=True)

2. Inception module and residual connection

Inception modules and residual connections are common neural network components.

1. Inception module

Inception is a convolutional neural network architecture that is a stack of modules that themselves resemble small independent networks. The basic form contains 3~4 branches, and the main essence is 1*1 convolution ( point-by-point convolution ). At this time, the convolution is equivalent to the vector of each square passing through a Dense layer, mixing the input information together but not cross-spatial information. Helps to distinguish channel feature learning from spatial feature learning.
(The specific principle will not be repeated, see the official document for details)
keras implementation:

tf.keras.applications.InceptionV3(
include_top=True, weights=‘imagenet’, input_tensor=None,
input_shape=None, pooling=None, classes=1000,
classifier_activation=‘softmax’
)

tf.keras.applications.InceptionResNetV2(
include_top=True, weights=‘imagenet’, input_tensor=None,
input_shape=None, pooling=None, classes=1000,
classifier_activation=‘softmax’, **kwargs
)

Another Xception model is an extreme form of the Inception model, which completely separates spatial features from channel features .

2. Residual connection

Residual connections are a graph-like network component that solves the problems of vanishing gradients and representation bottlenecks in large-scale deep learning models.
Generally, it is useful to add residual connections to models with more than 10 layers.
The residual connection allows the output of a previous layer to be used as the input of the latter layer. The previous output is added to the activation of the later layer instead of concatenated (the activation of the two layers has the same shape, otherwise it must be converted to the same shape)

、、、
block_1_output = layers.MaxPooling2D(3)(x)
、、、
block_2_output = layers.add([x, block_1_output])

input_shape = (2, 3, 4)
x1 = tf.random.normal(input_shape)
x2 = tf.random.normal(input_shape)
y = tf.keras.layers.Add()([x1, x2])
print(y.shape)
(2, 3, 4)

As mentioned earlier, there are two connection methods add and concatenate. When using concatenate connection, it is a direct connection:

x = np.arange(20).reshape(2, 2, 5)
print(x)
###
[[[ 0  1  2  3  4]
  [ 5  6  7  8  9]]

 [[10 11 12 13 14]
  [15 16 17 18 19]]]
  
y = np.arange(20, 30).reshape(2, 1, 5)
print(y)
###
>[[[20 21 22 23 24]]

 [[25 26 27 28 29]]]
 
tf.keras.layers.Concatenate(axis=1)([x, y])
###
<tf.Tensor: shape=(2, 3, 5), dtype=int64, numpy=
array([[[ 0,  1,  2,  3,  4],
        [ 5,  6,  7,  8,  9],
        [20, 21, 22, 23, 24]],

       [[10, 11, 12, 13, 14],
        [15, 16, 17, 18, 19],
        [25, 26, 27, 28, 29]]])>

3. Shared weight

As mentioned before, the functional API can easily call the layer. When the layer is reused, the weight of the layer for different inputs is shared. This mode can solve some problems very well, such as:
semantic similarity analysis, the rules for analyzing sentences are the same, so the same model can be used for semantic analysis, and finally the results are directly connected by the concatenate layer into a Dense (1) layer for binary classification.

、、、
lstm = layers. LSTM(32)
left_input、、、
left_output = lstm(left_input)
right、、、

merged = layers. concatenate([left_output,right_output],axis=1)
predictions=layers. Dense(1,activation='sigmoid')(merged) #输出0~1之间的值来表示相似度
#接下来实例化模型
、、、

Siamese visual models can also be realized, and convolution can be shared. Multiple cameras can extract visual features through the same visual model and then merge them, thus avoiding the need for two model training.

epilogue

Compared with the sequential model, the functional API is more flexible and can create more complex topological structures.
See the official documentation for learning content

Guess you like

Origin blog.csdn.net/qq_43842886/article/details/114239670