我希望能够重置整个Keras模型的权重,这样就不必重新编译它了。目前编译模型是我的代码的主要瓶颈。以下是我所指的示例:
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(16, activation='relu'),
tf.keras.layers.Dense(10)
])
model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.001),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
data = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = data.load_data()
model.fit(x=x_train, y=y_train, epochs=10)
# Reset all weights of model here
# model.reset_all_weights() <----- something like that
model.fit(x=x_train, y=y_train, epochs=10)