在 tensorflow InteractiveSession() 之后是否有必要关闭会话 [英] Is it necessary to close session after tensorflow InteractiveSession()

查看:60
本文介绍了在 tensorflow InteractiveSession() 之后是否有必要关闭会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 Tensorflow 中的 InteractiveSession 的问题

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

 使用 tf.Session() 作为 sess:# 做点什么

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

问题:
1. 不关闭会话会导致什么问题,比如会话泄漏?
2. 如果不关闭 InteractiveSession,GC 是如何工作的?

解决方案

是的,tf.InteractiveSession 只是保持默认会话打开的方便语法糖.

Session 实现有 评论

<块引用>

调用此方法会释放与会话相关的所有资源.

快速测试

#!/usr/bin/env python# -*- 编码:utf-8 -*-导入参数解析将张量流导入为 tf将 numpy 导入为 npdef 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))使用 tf.Session() 作为 sess:sess.run(tf.global_variables_initializer())如果 __name__ == '__main__':解析器 = argparse.ArgumentParser()parser.add_argument('--num', help='repeat', type=int, default=5)parser.add_argument('type', choice=['interactive', 'interactive_close', 'normal'])args = parser.parse_args()sess_func = open_and_close_session如果 args.type == '交互式':sess_func = open_interactive_sessionelif args.type == 'interactive_close':sess_func = open_and_close_interactive_session对于 _ 范围(args.num):sess_func()使用 tf.Session() 作为 sess:打印(使用的字节数=",sess.run(tf.contrib.memory_stats.BytesInUse()))

给予

<代码>"""python example_session2.py 交互式('使用的字节数=', 405776640)python example_session2.py interactive_close('使用的字节数=', 7680)python example_session2.py('使用的字节数=', 7680)"""

这会在不关闭会话时引发会话泄漏.注意,即使关闭会话,TensorFlow 中当前也存在错误,每个会话保持 1280 字节,请参阅 Tensorflow 在每个会话打开和关闭时泄漏 1280 字节?.(现已修复).

此外,__del__ 试图启动 GC.

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

<块引用>

交互式会话已处于活动状态.在某些情况下,这可能会导致内存不足错误.您必须显式调用 InteractiveSession.close() 以释放其他会话持有的资源

这似乎是 实施.它猜测 InteractiveSession 的唯一存在理由是它在 Jupyter Notebookfiles 或非活动 shell 中与 .eval() 结合使用.但我建议不要使用 eval(参见 官方 ZeroOut 渐变示例错误:AttributeError: 'list' object has no attribute 'eval')

<块引用>

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

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

I have a question about InteractiveSession in Tensorflow

I know tf.InteractiveSession() is just convenient syntactic sugar for keeping a default session open and basically work the same like below:

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

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

Question:
1. Would it caused any problem without closing the session like session leak?
2. How the GC work for the InteractiveSession if we don't close it?

解决方案

Yes, tf.InteractiveSession is just convenient syntactic sugar for keeping a default session open.

The Session implementation has a comment

Calling this method frees all resources associated with the session.

A quick test

#! /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()))

gives

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

This provokes a session-leak, when not closing the session.Note, even when closing the session, there is currently bug in TensorFlow which keep 1280 bytes per session see Tensorflow leaks 1280 bytes with each session opened and closed?. (This has been fixed now).

Further, there is some logic in the __del__ trying to start the GC.

Interestingly, I never saw the warning

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)

which seems to be implemented. It guess the only raison d'être of the InteractiveSession is its usage in Jupyter Notebookfiles or inactive shells in combination with .eval(). But I advised against using eval (see 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.

And I am not surprised by that. Guess how many code snippets are the without a free or delete after some malloc. Bless the OS that it frees up the memory.

这篇关于在 tensorflow InteractiveSession() 之后是否有必要关闭会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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