Save tensorflow model to file

跟風遠走 提交于 2019-12-19 02:28:11

问题


I create a tensorflow model which I would like to save to file so that I can predict against it later. In particular, I need to save the:

  • input_placeholder
    (= tf.placeholder(tf.float32, [None, iVariableLen]))
  • solution_space
    (= tf.nn.sigmoid(tf.matmul(input_placeholder, weight_variable) + bias_variable))
  • session
    (= tf.Session())

I've tried using pickle which works on other objects like sklearn binarizers etc, but not on the above, for which I get the error at the bottom.

How I pickle:

import pickle
with open(sModelSavePath, 'w') as fiModel:
    pickle.dump(dModel, fiModel)

where dModel is a dictionary that contains all the objects I want to persist, which I use for fitting against.

Any suggestions on how to pickle tensorflow objects?

Error message:

pickle.dump(dModel, fiModel)
...
    raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle module objects

回答1:


The way I solved this was by pickleing Sklearn objects like binarizers, and using tensorflow's inbuilt save functions for the actual model:

Saving tensorflow model:
1) Build the model as you usually would
2) Save the session with tf.train.Saver(). For example:

oSaver = tf.train.Saver()

oSess = oSession
oSaver.save(oSess, sModelPath)  #filename ends with .ckpt

3) This saves all available variables etc in that session to their variable names.

Loading tensorflow model:
1) The entire flow needs to be re-initialized. In other words, variables, weights, bias, loss function etc need to be declared, and then initialized with tf.initialize_all_variables() being passed into oSession.run()
2) That session now needs to be passed to the loader. I abstracted the flow, so my loader looks like this:

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) All objects you need for prediction need to be gotten out of your initialisation, which in my case sit in dAlg

PS: Pickle like this:

with open(sSavePathFilename, 'w') as fiModel:
    pickle.dump(dModel, fiModel)

with open(sFilename, 'r') as fiModel:
    dModel = pickle.load(fiModel)



回答2:


You should save your project into two separate parts, one is for tensorflow's objects, another is for other objects. I recommend you to use the following tools:

  1. tf.saved_model: the procedures your want to saved and load tensorflow all in it.
  2. dill: a more powerful pickle tool based on pickle, it can help you bypass most errors encountered by pickle


来源:https://stackoverflow.com/questions/38000180/save-tensorflow-model-to-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!