human
Solve the following python programming problem given the following code:
# 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()
# Call model on inputs to create the variables of the dense layer.
_ = model(tf.ones((1, 784)))

# Create a Checkpoint with the same structure as before, and load the weights.
tf.train.Checkpoint(
    dense=model.first_dense, kernel=model.kernel, bias=model.bias
).restore(ckpt_path).assert_consumed()
Given the above code, generate some markdown instructions for it.
assistant
#Here is the Python program you requested.

通常建议使用相同的 API 来构建模型。如果您在序贯模型和函数式模型之间，或在函数式模型和子类化模型等之间进行切换，请始终重新构建预训练模型并将预训练权重加载到该模型。
下一个问题是，如果模型架构截然不同，如何保存权重并将其加载到不同模型？解决方案是使用 tf.train.Checkpoint 来保存和恢复确切的层/变量。
示例：