text
stringlengths 0
4.99k
|
---|
model.load_weights("pretrained_ckpt") |
# Check that all of the pretrained weights have been loaded. |
for a, b in zip(pretrained.weights, model.weights): |
np.testing.assert_allclose(a.numpy(), b.numpy()) |
print("\n", "-" * 50) |
model.summary() |
# Example 2: Sequential model |
# Recreate the pretrained model, and load the saved weights. |
inputs = keras.Input(shape=(784,), name="digits") |
x = keras.layers.Dense(64, activation="relu", name="dense_1")(inputs) |
x = keras.layers.Dense(64, activation="relu", name="dense_2")(x) |
pretrained_model = keras.Model(inputs=inputs, outputs=x, name="pretrained") |
# Sequential example: |
model = keras.Sequential([pretrained_model, keras.layers.Dense(5, name="predictions")]) |
model.summary() |
pretrained_model.load_weights("pretrained_ckpt") |
# Warning! Calling `model.load_weights('pretrained_ckpt')` won't throw an error, |
# but will *not* work as expected. If you inspect the weights, you'll see that |
# none of the weights will have loaded. `pretrained_model.load_weights()` is the |
# correct method to call. |
Model: "pretrained_model" |
_________________________________________________________________ |
Layer (type) Output Shape Param # |
================================================================= |
digits (InputLayer) [(None, 784)] 0 |
_________________________________________________________________ |
dense_1 (Dense) (None, 64) 50240 |
_________________________________________________________________ |
dense_2 (Dense) (None, 64) 4160 |
================================================================= |
Total params: 54,400 |
Trainable params: 54,400 |
Non-trainable params: 0 |
_________________________________________________________________ |
-------------------------------------------------- |
Model: "new_model" |
_________________________________________________________________ |
Layer (type) Output Shape Param # |
================================================================= |
digits (InputLayer) [(None, 784)] 0 |
_________________________________________________________________ |
dense_1 (Dense) (None, 64) 50240 |
_________________________________________________________________ |
dense_2 (Dense) (None, 64) 4160 |
_________________________________________________________________ |
predictions (Dense) (None, 5) 325 |
================================================================= |
Total params: 54,725 |
Trainable params: 54,725 |
Non-trainable params: 0 |
_________________________________________________________________ |
Model: "sequential_3" |
_________________________________________________________________ |
Layer (type) Output Shape Param # |
================================================================= |
pretrained (Functional) (None, 64) 54400 |
_________________________________________________________________ |
predictions (Dense) (None, 5) 325 |
================================================================= |
Total params: 54,725 |
Trainable params: 54,725 |
Non-trainable params: 0 |
_________________________________________________________________ |
<tensorflow.python.training.tracking.util.CheckpointLoadStatus at 0x10e58f3d0> |
It is generally recommended to stick to the same API for building models. If you switch between Sequential and Functional, or Functional and subclassed, etc., then always rebuild the pre-trained model and load the pre-trained weights to that model. |
The next question is, how can weights be saved and loaded to different models if the model architectures are quite different? The solution is to use tf.train.Checkpoint to save and restore the exact layers/variables. |
Example: |
# Create a subclassed model that essentially uses functional_model's first |
# and last layers. |
# First, save the weights of functional_model's first and last dense layers. |
first_dense = functional_model.layers[1] |
last_dense = functional_model.layers[-1] |
ckpt_path = tf.train.Checkpoint( |
dense=first_dense, kernel=last_dense.kernel, bias=last_dense.bias |
).save("ckpt") |
# Define the subclassed model. |
class ContrivedModel(keras.Model): |
def __init__(self): |
super(ContrivedModel, self).__init__() |
self.first_dense = keras.layers.Dense(64) |
self.kernel = self.add_variable("kernel", shape=(64, 10)) |
self.bias = self.add_variable("bias", shape=(10,)) |
def call(self, inputs): |
x = self.first_dense(inputs) |
return tf.matmul(x, self.kernel) + self.bias |
model = ContrivedModel() |