Learn how to implement a basic model and a multi-input / multi-output model using Keras' Functional API.
This time I'm using Keras integrated with Tensorflow.
tensorflow==2.3.0
--Functional API can be used --Multiple input / multi-output model can be implemented
It allows you to implement more flexible models than the Sequential model. This time, we will implement a multi-input / multi-output model from among the models that cannot be expressed by the Sequential model.
First, I will explain the basic usage of the Functional API. The Functional API is a way to define a model, so learning, evaluation, and prediction are the same as for the Sequential model.
First, define the input layer with keras.Input
.
inputs = keras.Input(shape=(128,))
You can add layers as shown below, and the last layer will be the output layer.
x = layers.Dense(64, activation="relu")(inputs)
outputs = layers.Dense(10)(x)
After defining the layers, specify the input and output layers to create the model.
model = keras.Model(inputs=inputs, outputs=outputs, name="model")
Try implementing the same model with the Sequential model and the Functional API.
The model to be implemented is as follows.
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential
model = Sequential()
model.add(layers.Dense(64, activation='relu', input_shape=(784,)))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
Functional API
from tensorflow import keras
from tensorflow.keras import layers
inputs = keras.Input(shape=(784,))
x = layers.Dense(64, activation='relu')(inputs)
x = layers.Dense(64, activation='relu')(x)
outputs = layers.Dense(10, activation='softmax')(x)
model = keras.Model(inputs=inputs, outputs=outputs)
We will implement a multi-input / multi-output model with the Functional API.
By defining multiple input layers, it is possible to have multiple inputs.
Use layers.concatenate
to combine multiple layers.
inputs1 = keras.Input(shape=(64,), name="inputs1_name")
inputs2 = keras.Input(shape=(32,), name="inputs2_name")
x = layers.concatenate([inputs1, inputs2])
Layers can be branched by passing the middle layer to multiple layers. Multi-output is achieved by having multiple layers as the end points.
outputs1 = layers.Dense(64, name="outputs1_name")(x)
outputs2 = layers.Dense(32, name="outputs2_name")(X)
If you have multiple output layers, you can specify a loss function and weight for each.
model.compile(
optimizer=keras.optimizers.RMSprop(1e-3),
loss={
"outputs1_name": keras.losses.BinaryCrossentropy(from_logits=True),
"outputs2_name": keras.losses.CategoricalCrossentropy(from_logits=True),
},
loss_weights=[1.0, 0.5],
)
Input data and output data (target) can be specified by the name given to the layer and trained.
model.fit(
{"inputs1_name": inputs1_data, "inputs2_name": inputs2_data},
{"outputs1_name": outputs1_targets, "outputs2_name": outputs2_targets},
epochs=2,
batch_size=32,
)
We will implement it using a concrete example.
Here, from the title, body, and tag of the inquiry from the customer, the priority of the inquiry and the corresponding department are predicted.
--Title
from tensorflow import keras
from tensorflow.keras import layers
import numpy as np
num_tags = 12
num_words = 10000
num_departments = 4
#Creating dummy data
title_data = np.random.randint(num_words, size=(1280, 10))
body_data = np.random.randint(num_words, size=(1280, 100))
tags_data = np.random.randint(2, size=(1280, num_tags)).astype("float32")
priority_targets = np.random.random(size=(1280, 1))
dept_targets = np.random.randint(2, size=(1280, num_departments))
#Title layer
title_input = keras.Input(
shape=(None,), name="title"
)
title_features = layers.Embedding(num_words, 64)(title_input)
title_features = layers.LSTM(128)(title_features)
#Body layer
body_input = keras.Input(shape=(None,), name="body")
body_features = layers.Embedding(num_words, 64)(body_input)
body_features = layers.LSTM(32)(body_features)
#Tag layer
tags_input = keras.Input(
shape=(num_tags,), name="tags"
)
tags_features = layers.Dense(36, activation='relu')(tags_input)
#Join layers
x = layers.concatenate([title_features, body_features, tags_features])
#Output layer
priority_output = layers.Dense(1, name="priority")(x)
department_output = layers.Dense(num_departments, name="department")(x)
model = keras.Model(
inputs=[title_input, body_input, tags_input],
outputs=[priority_output, department_output],
)
#Compile the model
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.5],
)
#Learning
model.fit(
{"title": title_data, "body": body_data, "tags": tags_data},
{"priority": priority_targets, "department": dept_targets},
epochs=2,
batch_size=32,
)
--Multi-input / multi-output models can be implemented using the Functional API
Recommended Posts