将 tensorflow 模型保存到文件
Save tensorflow model to file
我创建了一个 tensorflow 模型,我想将其保存到文件中,以便稍后进行预测。特别是,我需要保存:
- input_placeholder
(= tf.placeholder(tf.float32, [None, iVariableLen])
)
- solution_space
(= tf.nn.sigmoid(tf.matmul(input_placeholder, weight_variable) + bias_variable)
)
- 会话
(= tf.Session()
)
我试过使用 pickle,它适用于其他对象,如 sklearn binarizers 等,但不适用于上面的对象,我在底部得到了错误。
我是如何腌制的:
import pickle
with open(sModelSavePath, 'w') as fiModel:
pickle.dump(dModel, fiModel)
其中 dModel
是一个字典,其中包含我想要保留的所有对象,我将其用于拟合。
关于如何 pickle tensorflow 对象有什么建议吗?
错误信息:
pickle.dump(dModel, fiModel)
...
raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle module objects
我解决这个问题的方法是 pickleing Sklearn objects like binarizers, and using tensorflow's inbuilt save functions 对于实际模型:
正在保存张量流模型:
1) 像往常一样构建模型
2) 使用 tf.train.Saver()
保存会话。例如:
oSaver = tf.train.Saver()
oSess = oSession
oSaver.save(oSess, sModelPath) #filename ends with .ckpt
3) 这会将该会话中的所有可用变量等保存到它们的变量名中。
正在加载张量流模型:
1)整个流程需要重新初始化。换句话说,需要声明变量、权重、偏差、损失函数等,然后用 tf.initialize_all_variables()
初始化并传递给 oSession.run()
2) 现在需要将该会话传递给加载器。我抽象了流程,所以我的加载器看起来像这样:
dAlg = tf_training_algorithm() #defines variables etc and initializes session
oSaver = tf.train.Saver()
oSaver.restore(dAlg['oSess'], sModelPath)
return {
'oSess': dAlg['oSess'],
#the other stuff I need from my algorithm, like my solution space etc
}
3) 预测所需的所有对象都需要从初始化中取出,在我的例子中,它位于 dAlg
PS:像这样泡菜:
with open(sSavePathFilename, 'w') as fiModel:
pickle.dump(dModel, fiModel)
with open(sFilename, 'r') as fiModel:
dModel = pickle.load(fiModel)
您应该将您的项目保存为两个独立的部分,一个用于 tensorflow
的对象,另一个用于其他对象。我推荐你使用以下工具:
- tf.saved_model: 你要保存和载入的程序
tensorflow
都在里面
- dill:基于
pickle
的更强大的pickle工具,可以帮助您绕过pickle
遇到的大部分错误
我创建了一个 tensorflow 模型,我想将其保存到文件中,以便稍后进行预测。特别是,我需要保存:
- input_placeholder
(= tf.placeholder(tf.float32, [None, iVariableLen])
) - solution_space
(= tf.nn.sigmoid(tf.matmul(input_placeholder, weight_variable) + bias_variable)
) - 会话
(= tf.Session()
)
我试过使用 pickle,它适用于其他对象,如 sklearn binarizers 等,但不适用于上面的对象,我在底部得到了错误。
我是如何腌制的:
import pickle
with open(sModelSavePath, 'w') as fiModel:
pickle.dump(dModel, fiModel)
其中 dModel
是一个字典,其中包含我想要保留的所有对象,我将其用于拟合。
关于如何 pickle tensorflow 对象有什么建议吗?
错误信息:
pickle.dump(dModel, fiModel)
...
raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle module objects
我解决这个问题的方法是 pickleing Sklearn objects like binarizers, and using tensorflow's inbuilt save functions 对于实际模型:
正在保存张量流模型:
1) 像往常一样构建模型
2) 使用 tf.train.Saver()
保存会话。例如:
oSaver = tf.train.Saver()
oSess = oSession
oSaver.save(oSess, sModelPath) #filename ends with .ckpt
3) 这会将该会话中的所有可用变量等保存到它们的变量名中。
正在加载张量流模型:
1)整个流程需要重新初始化。换句话说,需要声明变量、权重、偏差、损失函数等,然后用 tf.initialize_all_variables()
初始化并传递给 oSession.run()
2) 现在需要将该会话传递给加载器。我抽象了流程,所以我的加载器看起来像这样:
dAlg = tf_training_algorithm() #defines variables etc and initializes session
oSaver = tf.train.Saver()
oSaver.restore(dAlg['oSess'], sModelPath)
return {
'oSess': dAlg['oSess'],
#the other stuff I need from my algorithm, like my solution space etc
}
3) 预测所需的所有对象都需要从初始化中取出,在我的例子中,它位于 dAlg
PS:像这样泡菜:
with open(sSavePathFilename, 'w') as fiModel:
pickle.dump(dModel, fiModel)
with open(sFilename, 'r') as fiModel:
dModel = pickle.load(fiModel)
您应该将您的项目保存为两个独立的部分,一个用于 tensorflow
的对象,另一个用于其他对象。我推荐你使用以下工具:
- tf.saved_model: 你要保存和载入的程序
tensorflow
都在里面 - dill:基于
pickle
的更强大的pickle工具,可以帮助您绕过pickle
遇到的大部分错误