如何有效地使用 python 对象中的 Session 来保持 tensorflow 作为实现细节? [英] How to efficiently use a Session from within a python object to keep tensorflow as an implementation detail?

查看:33
本文介绍了如何有效地使用 python 对象中的 Session 来保持 tensorflow 作为实现细节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个自定义 sklearn 转换器,它需要一个已在 Tensorflow 中编码的优化步骤.TF 需要一个 Session,它应该用作上下文管理器或显式关闭.问题是:将 close() 方法添加到 Transformer 会很奇怪(并且对用户来说是意外的),关闭会话的最佳位置是什么?我应该为每次 fit() 调用打开和关闭一个新会话吗?或者我应该保持打开状态并将 session.close() 留给转换器的 __del__ 方法?还有其他选择吗?

I'm implementing a custom sklearn Transformer, which requires an optimization step which has been coded in Tensorflow. TF requires a Session, which should be used as a context manager or explicitly closed. The question is: adding a close() method to the Transformer would be odd (and unexpected for a user), what is the best place to close the session? Should I open and close a new session for a every call to fit()? Or should I keep it open and leave the session.close() to the __del__ method of the transformer? Any other options?

推荐答案

不要在每次函数调用时都打开一个会话,如果多次调用该函数,效率会非常低.

Don't open a session at each function call, that could be very inefficient if the function is called many times.

如果出于某种原因,您不想公开上下文管理器,那么您需要自己打开会话,并保持打开状态.对于用户来说,这可能更简单一些,但与其他对象或库共享 tf.Session 可能更困难.还试图隐藏您使用 tensorflow 的事实可能有点徒劳,因为它可能与也依赖 GPU 的其他库不兼容.(此外,用户需要安装 tensorflow 才能使用该库,他/他肯定知道您正在使用它.

If for some reason, you don't want to expose a context manager, then you need to open the session yourself, and leave it open. It is perhaps a bit simpler for the user, but sharing the tf.Session with other objects or libraries might be more difficult. Also trying to hide the fact that you are using tensorflow may be a bit vain, as it is potentially incompatible with other libraries also relying on the GPU. (Also the user will need to install tensorflow to use the library, s/he will definitely know that you are using it).

所以我不会尝试封装不能或不应该(在我看来)的东西,并为 tf.Session 使用上下文管理器(甚至可能直接使用 tf.Session 本身,如果我不介意公开 tensorflow,否则将其包装在我自己的上下文管理器中).

So I would not try to encapsulate things that can't or shouldn't (in my opinion) and use a context manager for the tf.Session (maybe even using directly a tf.Session itself if I don't mind exposing tensorflow, otherwise wrapping it in my own context manager).

这篇关于如何有效地使用 python 对象中的 Session 来保持 tensorflow 作为实现细节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆