在进入迭代优化过程之前finalize()
图形是一个很好的做法,特别是发现在优化过程中添加操作的错误,从而导致可避免的内存不足错误。
但是,当图形最终确定时,很难以交互方式检查或调试代码,因为唯一拥有的句柄是已经定义的句柄——并且您最好将它们保存在 python 变量中或给定它们一个不错的名字,因为使用默认名称浏览大图的所有操作可能会很痛苦。
那么有没有更好的方法,特别是图表可以“未最终确定”吗?
最佳答案
图确实可以使用 protected 方法 _unsafe_unfinalize()
来取消最终化,例如
tf.get_current_graph()._unsafe_unfinalize()
正如该方法的文档(和名称)所述,
Unfinalizing a graph could have negative impact on performance, especially in a multi-threaded environment. Unfinalizing a graph when it is in use by a Session may lead to undefined behavior. Ensure that all sessions using a graph are closed before calling this method.
尽管有这个令人震惊的警告,我发现 _unsafe_finalize()
很有用,特别是在训练出错并且无论如何都会被终止的情况下。
关于python - 如何 "unfinalize"图表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50620483/