gpt4 book ai didi

python - 是否有必要在 tensorflow InteractiveSession() 之后关闭 session

转载 作者:太空狗 更新时间:2023-10-30 02:53:17 24 4
gpt4 key购买 nike

我对 Tensorflow 中的 InteractiveSession 有疑问

我知道 tf.InteractiveSession() 只是方便的语法用于保持默认 session 打开的糖,基本上与下面的工作相同:

with tf.Session() as sess:
# Do something

但是,我在网上看到一些例子,他们在使用InteractiveSession后并没有在代码末尾调用close()

问题:
1. 不关闭session会不会造成session leak之类的问题?
2. 如果我们不关闭 InteractiveSession,GC 是如何工作的?

最佳答案

是的,tf.InteractiveSession 只是方便的语法糖,用于保持默认 session 打开。

session 实现有a comment

Calling this method frees all resources associated with the session.

快速测试

#! /usr/bin/env python
# -*- coding: utf-8 -*-


import argparse
import tensorflow as tf
import numpy as np


def open_interactive_session():
A = tf.Variable(np.random.randn(16, 255, 255, 3).astype(np.float32))
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())


def open_and_close_interactive_session():
A = tf.Variable(np.random.randn(16, 255, 255, 3).astype(np.float32))
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
sess.close()


def open_and_close_session():
A = tf.Variable(np.random.randn(16, 255, 255, 3).astype(np.float32))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())


if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--num', help='repeat', type=int, default=5)
parser.add_argument('type', choices=['interactive', 'interactive_close', 'normal'])
args = parser.parse_args()

sess_func = open_and_close_session

if args.type == 'interactive':
sess_func = open_interactive_session
elif args.type == 'interactive_close':
sess_func = open_and_close_interactive_session

for _ in range(args.num):
sess_func()
with tf.Session() as sess:
print("bytes used=", sess.run(tf.contrib.memory_stats.BytesInUse()))

给予

"""
python example_session2.py interactive
('bytes used=', 405776640)
python example_session2.py interactive_close
('bytes used=', 7680)
python example_session2.py
('bytes used=', 7680)
"""

这会在不关闭 session 时引发 session 泄漏。 请注意,即使在关闭 session 时,TensorFlow 目前也存在一个错误,每个 session 保留 1280 字节,请参阅 Tensorflow leaks 1280 bytes with each session opened and closed? . (此问题现已修复)。

此外,__del__ 中有一些逻辑尝试启动 GC。

有趣的是,我从来没有看到警告

An interactive session is already active. This can cause out-of-memory errors in some cases. You must explicitly call InteractiveSession.close() to release resources held by the other session(s)

这似乎是 implemented .它猜测 InteractiveSession 的唯一存在理由是它在 Jupyter Notebook 文件或非事件 shell 中与 .eval() 结合使用。但我建议不要使用 eval(参见 Official ZeroOut gradient example error: AttributeError: 'list' object has no attribute 'eval')

However, I have seen some examples online, they did't call close() at the end of the code after using InteractiveSession.

我对此并不感到惊讶。猜猜有多少代码片段在一些 malloc 之后没有 freedelete。祝福操作系统,它释放了内存。

关于python - 是否有必要在 tensorflow InteractiveSession() 之后关闭 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50229091/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com