所以我有一个带有分类器的项目,它试图像这样对用户隐藏tensorflow背景:
class CNN_Classifier:
def __init__(self,
save_as='cnn_classifier.ckpt',
saved_model='cnn_classifier.ckpt'
):
# ...
model_restorable = (
saved_model is not None
and os.path.exists(f'{saved_model}.index')
)
if self.debug:
print('restore from file: {0}'.format('yes' if model_restorable else 'no'))
self._session = tf.Session()
saver = tf.train.Saver(self._tf_variables)
with self._session.as_default():
if model_restorable:
saver.restore(self._session,saved_model)
else:
tf.global_variables_initializer().run()
saver.save(self._session,save_as,write_meta_graph=False)
def train(self,encoding='utf8',*examples):
# makes use of self._session
# periodically saves back to disk
def predict(self,encoding='utf8',query):
# makes use of self._session
# ...
这样,我们希望避免每次调用train
、predict
等时都必须重新加载分类器。在
但是,当用户完成分类器/终止脚本时,我们如何关闭self._session
?在
“显而易见”的方法是简单地把它推到__del__
中,并希望它能成功。在
坦白地说,我看不出其他办法来处理。在
不过,如果说我从google上学到了一点关于__del__
的东西,那就是它总是非常不受欢迎,因为它不能与垃圾收集器协同工作。在
那么__del__
是该走的路还是还有一个我们还不知道的替代方案呢?在
从不,永远,永远在python中使用
__del__
-ever。它会导致极其痛苦的虫子追踪,你的真的花了几个月在其中一个上面。在@AllenLavoie是对的:
然后使用你的课程:
^{pr2}$相关问题 更多 >
编程相关推荐