Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save tensorflow model to file

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
like image 442
Roman Avatar asked Jun 23 '16 19:06

Roman


1 Answers

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)
like image 77
Roman Avatar answered Nov 16 '22 04:11

Roman