如何将在 tensorflow 2 中训练的模型转换为 tensorflow 1 冻结图 [英] How can I convert a model trained in tensorflow 2 to a tensorflow 1 frozen graph
问题描述
我想使用 tensorflow 2 训练模型,但之后我需要使用仅与 tensorflow 1 兼容的转换器.是否可能,如果可以,我如何将使用 tensorflow 2 训练的模型转换为 tensorflow1 种格式?
I would like to train a model using tensorflow 2 but afterwards I need to use a converter that is only compatible with tensorflow 1. Is it possible and if so how can I convert a model that was trained using tensorflow 2 to a tensorflow 1 format?
推荐答案
如果没有可靠的方法将您的 TF2 模型转换为 TF1,您可以随时保存训练好的参数(权重、偏差)并在以后使用它们来启动您的TF1 图.我以前是出于其他目的而这样做的.您可以按如下方式保存:
If there is no method that reliably converts your TF2 model to TF1, you can always save the trained parameters (weights, biases) and used them later to initiate your TF1 graph. I did it for some other purpose before. You can save as follows:
weights = []
for layer in model.layers:
w = layer.get_weights()
if len(w)>0:
print(layer.name)
weights.append(w)
with open('mnist_weights.pkl', 'wb') as f:
pickle.dump(weights, f)
对于每一层 w[0]=weights
和 w[1]=biases
这篇关于如何将在 tensorflow 2 中训练的模型转换为 tensorflow 1 冻结图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!