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
Roman
  • 8,826
  • 10
  • 63
  • 103
  • 1
    Can you use Tensorflow's built-in feature for this? https://www.tensorflow.org/versions/r0.9/how_tos/variables/index.html#saving-and-restoring – Yao Zhang Jun 23 '16 at 20:28

2 Answers2

12

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)
Roman
  • 8,826
  • 10
  • 63
  • 103
0

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
William
  • 4,258
  • 2
  • 23
  • 20