Saving with monitored session

分布式模型保存时,提示:TypeError: ‘sess’ must be a Session
具体如下:

File "/users/spraak/spch/prog/spch/tensorflow-1.0.0/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 1369, in save
    raise TypeError("'sess' must be a Session; %s" % sess)
TypeError: 'sess' must be a Session; <tensorflow.python.training.monitored_session.MonitoredSession object at 0x2be710d0>

出现这种情况时的代码大概为:

with tf.train.MonitoredTrainingSession(...) as sess:
    ...
    saver.save(sess, 'model.ckpt')

查找问题时,发现了一个解决办法:
地址为:https://github.com/tensorflow/tensorflow/issues/8425
Saving with monitored session_第1张图片
解决办法:
实现一个函数获得真实的session

def get_session(sess):
    session = sess
    while type(session).__name__ != 'Session':
        #pylint: disable=W0212
        session = session._sess
    return session

在保存模型时,使用如下方式调用

saver.save(get_session(sess), 'model.ckpt')

你可能感兴趣的:(机器学习,TensorFlow模型保存)