如何在TensorFlow中保存和加载模型
文章标签
tensorflow
在TensorFlow中保存和加载模型可以通过使用tf.keras.models.save_model()
和tf.keras.models.load_model()
来实现。下面是保存和加载模型的示例代码:
保存模型:
# 定义模型
model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu', input_shape=(784,)),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
# 编译模型
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# 训练模型
model.fit(x_train, y_train, epochs=5)
# 保存模型
model.save('my_model.h5')
加载模型:
# 加载模型
model = tf.keras.models.load_model('my_model.h5')
版权声明
本文仅代表作者观点,不代表米安网络立场。
上一篇:如何在Keras中使用自定义的损失函数 下一篇:kafka数据同步的方法是什么
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。