Firefox [Link]
io/guides/functional_api/
import numpy as np
import tensorflow as tf
from tensorflow import keras
from [Link] import layers
[Link]
(input: 784-dimensional vectors)
↧
[Dense (64 units, relu activation)]
↧
[Dense (64 units, relu activation)]
↧
[Dense (10 units, softmax activation)]
↧
(output: logits of a probability distribution over 10 classes)
inputs = [Link](shape=(784,))
(32, 32, 3)
# Just for demonstration purposes.
img_inputs = [Link](shape=(32, 32, 3))
inputs dtype
[Link]
TensorShape([None, 784])
[Link]
1 of 18 01-12-2022, 12:28 am
Firefox [Link]
tf.float32
inputs
dense = [Link](64, activation="relu")
x = dense(inputs)
dense x
x = [Link](64, activation="relu")(x)
outputs = [Link](10)(x)
Model
model = [Link](inputs=inputs, outputs=outputs, name="mnist_model")
[Link]()
Model: "mnist_model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 784)] 0
dense (Dense) (None, 64) 50240
dense_1 (Dense) (None, 64) 4160
dense_2 (Dense) (None, 10) 650
=================================================================
Total params: 55,050
Trainable params: 55,050
Non-trainable params: 0
_________________________________________________________________
[Link].plot_model(model, "my_first_model.png")
[Link].plot_model(model, "my_first_model_with_shape_info.png", show_shapes=True)
2 of 18 01-12-2022, 12:28 am
Firefox [Link]
Sequential
Model fit()
evaluate()
(x_train, y_train), (x_test, y_test) = [Link].load_data()
x_train = x_train.reshape(60000, 784).astype("float32") / 255
x_test = x_test.reshape(10000, 784).astype("float32") / 255
[Link](
loss=[Link](from_logits=True),
optimizer=[Link](),
metrics=["accuracy"],
)
history = [Link](x_train, y_train, batch_size=64, epochs=2, validation_split=0.2)
test_scores = [Link](x_test, y_test, verbose=2)
print("Test loss:", test_scores[0])
print("Test accuracy:", test_scores[1])
Epoch 1/2
750/750 [==============================] - 2s 2ms/step - loss: 0.3435 - accuracy: 0.9026 -
val_loss: 0.1797 - val_accuracy: 0.9507
Epoch 2/2
750/750 [==============================] - 1s 2ms/step - loss: 0.1562 - accuracy: 0.9539 -
val_loss: 0.1307 - val_accuracy: 0.9603
313/313 - 0s - loss: 0.1305 - accuracy: 0.9609 - 248ms/epoch - 793us/step
Test loss: 0.1305118203163147
Test accuracy: 0.9609000086784363
3 of 18 01-12-2022, 12:28 am
Firefox [Link]
Sequential [Link]()
compile
[Link]("path_to_my_model")
del model
# Recreate the exact same model purely from the file:
model = [Link].load_model("path_to_my_model")
INFO:tensorflow:Assets written to: path_to_my_model/assets
encoder
autoencoder
encoder_input = [Link](shape=(28, 28, 1), name="img")
x = layers.Conv2D(16, 3, activation="relu")(encoder_input)
x = layers.Conv2D(32, 3, activation="relu")(x)
x = layers.MaxPooling2D(3)(x)
x = layers.Conv2D(32, 3, activation="relu")(x)
x = layers.Conv2D(16, 3, activation="relu")(x)
encoder_output = layers.GlobalMaxPooling2D()(x)
encoder = [Link](encoder_input, encoder_output, name="encoder")
[Link]()
x = [Link]((4, 4, 1))(encoder_output)
x = layers.Conv2DTranspose(16, 3, activation="relu")(x)
x = layers.Conv2DTranspose(32, 3, activation="relu")(x)
x = layers.UpSampling2D(3)(x)
x = layers.Conv2DTranspose(16, 3, activation="relu")(x)
decoder_output = layers.Conv2DTranspose(1, 3, activation="relu")(x)
autoencoder = [Link](encoder_input, decoder_output, name="autoencoder")
[Link]()
4 of 18 01-12-2022, 12:28 am
Firefox [Link]
Model: "encoder"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
img (InputLayer) [(None, 28, 28, 1)] 0
conv2d (Conv2D) (None, 26, 26, 16) 160
conv2d_1 (Conv2D) (None, 24, 24, 32) 4640
max_pooling2d (MaxPooling2D (None, 8, 8, 32) 0
)
conv2d_2 (Conv2D) (None, 6, 6, 32) 9248
conv2d_3 (Conv2D) (None, 4, 4, 16) 4624
global_max_pooling2d (Globa (None, 16) 0
lMaxPooling2D)
=================================================================
Total params: 18,672
Trainable params: 18,672
Non-trainable params: 0
_________________________________________________________________
Model: "autoencoder"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
img (InputLayer) [(None, 28, 28, 1)] 0
conv2d (Conv2D) (None, 26, 26, 16) 160
conv2d_1 (Conv2D) (None, 24, 24, 32) 4640
max_pooling2d (MaxPooling2D (None, 8, 8, 32) 0
)
conv2d_2 (Conv2D) (None, 6, 6, 32) 9248
conv2d_3 (Conv2D) (None, 4, 4, 16) 4624
global_max_pooling2d (Globa (None, 16) 0
lMaxPooling2D)
reshape (Reshape) (None, 4, 4, 1) 0
conv2d_transpose (Conv2DTra (None, 6, 6, 16) 160
nspose)
conv2d_transpose_1 (Conv2DT (None, 8, 8, 32) 4640
ranspose)
up_sampling2d (UpSampling2D (None, 24, 24, 32) 0
)
conv2d_transpose_2 (Conv2DT (None, 26, 26, 16) 4624
ranspose)
conv2d_transpose_3 (Conv2DT (None, 28, 28, 1) 145
ranspose)
=================================================================
Total params: 28,241
Trainable params: 28,241
Non-trainable params: 0
_________________________________________________________________
(28, 28, 1)
Conv2D Conv2DTranspose MaxPooling2D
UpSampling2D
Input
5 of 18 01-12-2022, 12:28 am
Firefox [Link]
encoder_input = [Link](shape=(28, 28, 1), name="original_img")
x = layers.Conv2D(16, 3, activation="relu")(encoder_input)
x = layers.Conv2D(32, 3, activation="relu")(x)
x = layers.MaxPooling2D(3)(x)
x = layers.Conv2D(32, 3, activation="relu")(x)
x = layers.Conv2D(16, 3, activation="relu")(x)
encoder_output = layers.GlobalMaxPooling2D()(x)
encoder = [Link](encoder_input, encoder_output, name="encoder")
[Link]()
decoder_input = [Link](shape=(16,), name="encoded_img")
x = [Link]((4, 4, 1))(decoder_input)
x = layers.Conv2DTranspose(16, 3, activation="relu")(x)
x = layers.Conv2DTranspose(32, 3, activation="relu")(x)
x = layers.UpSampling2D(3)(x)
x = layers.Conv2DTranspose(16, 3, activation="relu")(x)
decoder_output = layers.Conv2DTranspose(1, 3, activation="relu")(x)
decoder = [Link](decoder_input, decoder_output, name="decoder")
[Link]()
autoencoder_input = [Link](shape=(28, 28, 1), name="img")
encoded_img = encoder(autoencoder_input)
decoded_img = decoder(encoded_img)
autoencoder = [Link](autoencoder_input, decoded_img, name="autoencoder")
[Link]()
6 of 18 01-12-2022, 12:28 am
Firefox [Link]
Model: "encoder"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
original_img (InputLayer) [(None, 28, 28, 1)] 0
conv2d_4 (Conv2D) (None, 26, 26, 16) 160
conv2d_5 (Conv2D) (None, 24, 24, 32) 4640
max_pooling2d_1 (MaxPooling (None, 8, 8, 32) 0
2D)
conv2d_6 (Conv2D) (None, 6, 6, 32) 9248
conv2d_7 (Conv2D) (None, 4, 4, 16) 4624
global_max_pooling2d_1 (Glo (None, 16) 0
balMaxPooling2D)
=================================================================
Total params: 18,672
Trainable params: 18,672
Non-trainable params: 0
_________________________________________________________________
Model: "decoder"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
encoded_img (InputLayer) [(None, 16)] 0
reshape_1 (Reshape) (None, 4, 4, 1) 0
conv2d_transpose_4 (Conv2DT (None, 6, 6, 16) 160
ranspose)
conv2d_transpose_5 (Conv2DT (None, 8, 8, 32) 4640
ranspose)
up_sampling2d_1 (UpSampling (None, 24, 24, 32) 0
2D)
conv2d_transpose_6 (Conv2DT (None, 26, 26, 16) 4624
ranspose)
conv2d_transpose_7 (Conv2DT (None, 28, 28, 1) 145
ranspose)
=================================================================
Total params: 9,569
Trainable params: 9,569
Non-trainable params: 0
_________________________________________________________________
Model: "autoencoder"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
img (InputLayer) [(None, 28, 28, 1)] 0
encoder (Functional) (None, 16) 18672
decoder (Functional) (None, 28, 28, 1) 9569
=================================================================
Total params: 28,241
Trainable params: 28,241
Non-trainable params: 0
_________________________________________________________________
7 of 18 01-12-2022, 12:28 am
Firefox [Link]
def get_model():
inputs = [Link](shape=(128,))
outputs = [Link](1)(inputs)
return [Link](inputs, outputs)
model1 = get_model()
model2 = get_model()
model3 = get_model()
inputs = [Link](shape=(128,))
y1 = model1(inputs)
y2 = model2(inputs)
y3 = model3(inputs)
outputs = [Link]([y1, y2, y3])
ensemble_model = [Link](inputs=inputs, outputs=outputs)
Sequential
num_tags = 12 # Number of unique issue tags
num_words = 10000 # Size of vocabulary obtained when preprocessing text data
num_departments = 4 # Number of departments for predictions
title_input = [Link](
shape=(None,), name="title"
) # Variable-length sequence of ints
body_input = [Link](shape=(None,), name="body") # Variable-length sequence of ints
tags_input = [Link](
shape=(num_tags,), name="tags"
) # Binary vectors of size `num_tags`
# Embed each word in the title into a 64-dimensional vector
title_features = [Link](num_words, 64)(title_input)
# Embed each word in the text into a 64-dimensional vector
body_features = [Link](num_words, 64)(body_input)
# Reduce sequence of embedded words in the title into a single 128-dimensional vector
title_features = [Link](128)(title_features)
# Reduce sequence of embedded words in the body into a single 32-dimensional vector
body_features = [Link](32)(body_features)
# Merge all available features into a single large vector via concatenation
x = [Link]([title_features, body_features, tags_input])
# Stick a logistic regression for priority prediction on top of the features
priority_pred = [Link](1, name="priority")(x)
# Stick a department classifier on top of the features
department_pred = [Link](num_departments, name="department")(x)
# Instantiate an end-to-end model predicting both priority and department
model = [Link](
inputs=[title_input, body_input, tags_input],
outputs=[priority_pred, department_pred],
)
8 of 18 01-12-2022, 12:28 am
Firefox [Link]
[Link].plot_model(model, "multi_input_and_output_model.png", show_shapes=True)
[Link](
optimizer=[Link](1e-3),
loss=[
[Link](from_logits=True),
[Link](from_logits=True),
],
loss_weights=[1.0, 0.2],
)
[Link](
optimizer=[Link](1e-3),
loss={
"priority": [Link](from_logits=True),
"department": [Link](from_logits=True),
},
loss_weights={"priority": 1.0, "department": 0.2},
)
# Dummy input data
title_data = [Link](num_words, size=(1280, 10))
body_data = [Link](num_words, size=(1280, 100))
tags_data = [Link](2, size=(1280, num_tags)).astype("float32")
# Dummy target data
priority_targets = [Link](size=(1280, 1))
dept_targets = [Link](2, size=(1280, num_departments))
[Link](
{"title": title_data, "body": body_data, "tags": tags_data},
{"priority": priority_targets, "department": dept_targets},
epochs=2,
batch_size=32,
)
Epoch 1/2
40/40 [==============================] - 3s 23ms/step - loss: 1.3256 - priority_loss: 0.7024 -
department_loss: 3.1160
Epoch 2/2
40/40 [==============================] - 1s 25ms/step - loss: 1.2926 - priority_loss: 0.6976 -
department_loss: 2.9749
<[Link] at 0x1300d6110>
Dataset ([title_data, body_data,
9 of 18 01-12-2022, 12:28 am
Firefox [Link]
tags_data], [priority_targets, dept_targets]) ({'title': title_data,
'body': body_data, 'tags': tags_data}, {'priority': priority_targets, 'department':
dept_targets})
Sequential
inputs = [Link](shape=(32, 32, 3), name="img")
x = layers.Conv2D(32, 3, activation="relu")(inputs)
x = layers.Conv2D(64, 3, activation="relu")(x)
block_1_output = layers.MaxPooling2D(3)(x)
x = layers.Conv2D(64, 3, activation="relu", padding="same")(block_1_output)
x = layers.Conv2D(64, 3, activation="relu", padding="same")(x)
block_2_output = [Link]([x, block_1_output])
x = layers.Conv2D(64, 3, activation="relu", padding="same")(block_2_output)
x = layers.Conv2D(64, 3, activation="relu", padding="same")(x)
block_3_output = [Link]([x, block_2_output])
x = layers.Conv2D(64, 3, activation="relu")(block_3_output)
x = layers.GlobalAveragePooling2D()(x)
x = [Link](256, activation="relu")(x)
x = [Link](0.5)(x)
outputs = [Link](10)(x)
model = [Link](inputs, outputs, name="toy_resnet")
[Link]()
10 of 18 01-12-2022, 12:28 am
Firefox [Link]
Model: "toy_resnet"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
img (InputLayer) [(None, 32, 32, 3)] 0 []
conv2d_8 (Conv2D) (None, 30, 30, 32) 896 ['img[0][0]']
conv2d_9 (Conv2D) (None, 28, 28, 64) 18496 ['conv2d_8[0][0]']
max_pooling2d_2 (MaxPooling2D) (None, 9, 9, 64) 0 ['conv2d_9[0][0]']
conv2d_10 (Conv2D) (None, 9, 9, 64) 36928 ['max_pooling2d_2[0][0]']
conv2d_11 (Conv2D) (None, 9, 9, 64) 36928 ['conv2d_10[0][0]']
add (Add) (None, 9, 9, 64) 0 ['conv2d_11[0][0]',
'max_pooling2d_2[0][0]']
conv2d_12 (Conv2D) (None, 9, 9, 64) 36928 ['add[0][0]']
conv2d_13 (Conv2D) (None, 9, 9, 64) 36928 ['conv2d_12[0][0]']
add_1 (Add) (None, 9, 9, 64) 0 ['conv2d_13[0][0]',
'add[0][0]']
conv2d_14 (Conv2D) (None, 7, 7, 64) 36928 ['add_1[0][0]']
global_average_pooling2d (Glob (None, 64) 0 ['conv2d_14[0][0]']
alAveragePooling2D)
dense_6 (Dense) (None, 256) 16640
['global_average_pooling2d[0][0]'
]
dropout (Dropout) (None, 256) 0 ['dense_6[0][0]']
dense_7 (Dense) (None, 10) 2570 ['dropout[0][0]']
==================================================================================================
Total params: 223,242
Trainable params: 223,242
Non-trainable params: 0
__________________________________________________________________________________________________
[Link].plot_model(model, "mini_resnet.png", show_shapes=True)
11 of 18 01-12-2022, 12:28 am
Firefox [Link]
12 of 18 01-12-2022, 12:28 am
Firefox [Link]
(x_train, y_train), (x_test, y_test) = [Link].cifar10.load_data()
x_train = x_train.astype("float32") / 255.0
x_test = x_test.astype("float32") / 255.0
y_train = [Link].to_categorical(y_train, 10)
y_test = [Link].to_categorical(y_test, 10)
[Link](
optimizer=[Link](1e-3),
loss=[Link](from_logits=True),
metrics=["acc"],
)
# We restrict the data to the first 1000 samples so as to limit execution time
# on Colab. Try to train on the entire dataset until convergence!
[Link](x_train[:1000], y_train[:1000], batch_size=64, epochs=1, validation_split=0.2)
13/13 [==============================] - 2s 98ms/step - loss: 2.3066 - acc: 0.1150 - val_loss:
2.2940 - val_acc: 0.1050
<[Link] at 0x1305fee10>
Embedding
# Embedding for 1000 unique words mapped to 128-dimensional vectors
shared_embedding = [Link](1000, 128)
# Variable-length sequence of integers
text_input_a = [Link](shape=(None,), dtype="int32")
# Variable-length sequence of integers
text_input_b = [Link](shape=(None,), dtype="int32")
# Reuse the same layer to encode both inputs
encoded_input_a = shared_embedding(text_input_a)
encoded_input_b = shared_embedding(text_input_b)
vgg19 = [Link].VGG19()
13 of 18 01-12-2022, 12:28 am
Firefox [Link]
features_list = [[Link] for layer in [Link]]
feat_extraction_model = [Link](inputs=[Link], outputs=features_list)
img = [Link]((1, 224, 224, 3)).astype("float32")
extracted_features = feat_extraction_model(img)
[Link]
Conv1D Conv2D Conv3D Conv2DTranspose
MaxPooling1D MaxPooling2D MaxPooling3D AveragePooling1D
GRU LSTM ConvLSTM2D
BatchNormalization Dropout Embedding
Layer
call
build
__init__
[Link]
class CustomDense([Link]):
def __init__(self, units=32):
super(CustomDense, self).__init__()
[Link] = units
def build(self, input_shape):
self.w = self.add_weight(
shape=(input_shape[-1], [Link]),
initializer="random_normal",
trainable=True,
)
self.b = self.add_weight(
shape=([Link],), initializer="random_normal", trainable=True
)
def call(self, inputs):
return [Link](inputs, self.w) + self.b
inputs = [Link]((4,))
outputs = CustomDense(10)(inputs)
model = [Link](inputs, outputs)
get_config
14 of 18 01-12-2022, 12:28 am
Firefox [Link]
class CustomDense([Link]):
def __init__(self, units=32):
super(CustomDense, self).__init__()
[Link] = units
def build(self, input_shape):
self.w = self.add_weight(
shape=(input_shape[-1], [Link]),
initializer="random_normal",
trainable=True,
)
self.b = self.add_weight(
shape=([Link],), initializer="random_normal", trainable=True
)
def call(self, inputs):
return [Link](inputs, self.w) + self.b
def get_config(self):
return {"units": [Link]}
inputs = [Link]((4,))
outputs = CustomDense(10)(inputs)
model = [Link](inputs, outputs)
config = model.get_config()
new_model = [Link].from_config(config, custom_objects={"CustomDense": CustomDense})
from_config(cls, config)
from_config
def from_config(cls, config):
return cls(**config)
Model
Model
super(MyClass, self).__init__(...) def call(self, ...):
inputs = [Link](shape=(32,))
x = [Link](64, activation='relu')(inputs)
outputs = [Link](10)(x)
mlp = [Link](inputs, outputs)
15 of 18 01-12-2022, 12:28 am
Firefox [Link]
class MLP([Link]):
def __init__(self, **kwargs):
super(MLP, self).__init__(**kwargs)
self.dense_1 = [Link](64, activation='relu')
self.dense_2 = [Link](10)
def call(self, inputs):
x = self.dense_1(inputs)
return self.dense_2(x)
# Instantiate the model.
mlp = MLP()
# Necessary to create the model's state.
# The model doesn't have a state until it's called at least once.
_ = mlp([Link]((1, 32)))
Input
features_list = [[Link] for layer in [Link]]
feat_extraction_model = [Link](inputs=[Link], outputs=features_list)
get_config()
from_config()
[Link]
Sequential
Sequential
16 of 18 01-12-2022, 12:28 am
Firefox [Link]
units = 32
timesteps = 10
input_dim = 5
# Define a Functional model
inputs = [Link]((None, units))
x = layers.GlobalAveragePooling1D()(inputs)
outputs = [Link](1)(x)
model = [Link](inputs, outputs)
class CustomRNN([Link]):
def __init__(self):
super(CustomRNN, self).__init__()
[Link] = units
self.projection_1 = [Link](units=units, activation="tanh")
self.projection_2 = [Link](units=units, activation="tanh")
# Our previously-defined Functional model
[Link] = model
def call(self, inputs):
outputs = []
state = [Link](shape=([Link][0], [Link]))
for t in range([Link][1]):
x = inputs[:, t, :]
h = self.projection_1(x)
y = h + self.projection_2(state)
state = y
[Link](y)
features = [Link](outputs, axis=1)
print([Link])
return [Link](features)
rnn_model = CustomRNN()
_ = rnn_model([Link]((1, timesteps, input_dim)))
(1, 10, 32)
call
call(self, inputs, **kwargs) inputs
**kwargs
call(self, inputs, training=None, **kwargs) training
call(self, inputs, mask=None, **kwargs) mask
call(self, inputs, training=None, mask=None, **kwargs)
get_config
17 of 18 01-12-2022, 12:28 am
Firefox [Link]
units = 32
timesteps = 10
input_dim = 5
batch_size = 16
class CustomRNN([Link]):
def __init__(self):
super(CustomRNN, self).__init__()
[Link] = units
self.projection_1 = [Link](units=units, activation="tanh")
self.projection_2 = [Link](units=units, activation="tanh")
[Link] = [Link](1)
def call(self, inputs):
outputs = []
state = [Link](shape=([Link][0], [Link]))
for t in range([Link][1]):
x = inputs[:, t, :]
h = self.projection_1(x)
y = h + self.projection_2(state)
state = y
[Link](y)
features = [Link](outputs, axis=1)
return [Link](features)
# Note that you specify a static batch size for the inputs with the `batch_shape`
# arg, because the inner computation of `CustomRNN` requires a static batch size
# (when you create the `state` zeros tensor).
inputs = [Link](batch_shape=(batch_size, timesteps, input_dim))
x = layers.Conv1D(32, 3)(inputs)
outputs = CustomRNN()(x)
model = [Link](inputs, outputs)
rnn_model = CustomRNN()
_ = rnn_model([Link]((1, 10, 5)))
18 of 18 01-12-2022, 12:28 am