Skip to main content
 首页 » 编程设计

python之如何到 "unfinalize"一个图

2024年09月07日8mengfanrong

在进入迭代优化过程之前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() 很有用,尤其是在训练出错并且无论如何都会被杀死的情况下。