阮十二 2021-03-04 16:17 采纳率: 0%
浏览 294

Tensorflow Session关闭后内存不释放

如下图所示,Tensorflow 开启一个Session as sess 进行预测,内存使用暴涨, sess.close 后内存不释放, 请问怎么及时释放内存?

  • 写回答

1条回答 默认 最新

  • Brentbin 2021-03-05 15:01
    关注
    import tensorflow as tf
    import multiprocessing
    import numpy as np
    
    def run_tensorflow():
    
        n_input = 10000
        n_classes = 1000
    
        # Create model
        def multilayer_perceptron(x, weight):
            # Hidden layer with RELU activation
            layer_1 = tf.matmul(x, weight)
            return layer_1
    
        # Store layers weight & bias
        weights = tf.Variable(tf.random_normal([n_input, n_classes]))
    
    
        x = tf.placeholder("float", [None, n_input])
        y = tf.placeholder("float", [None, n_classes])
        pred = multilayer_perceptron(x, weights)
    
        cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
        optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)
    
        init = tf.global_variables_initializer()
    
        with tf.Session() as sess:
            sess.run(init)
    
            for i in range(100):
                batch_x = np.random.rand(10, 10000)
                batch_y = np.random.rand(10, 1000)
                sess.run([optimizer, cost], feed_dict={x: batch_x, y: batch_y})
    
        print "finished doing stuff with tensorflow!"
    
    
    if __name__ == "__main__":
    
        # option 1: execute code with extra process
        p = multiprocessing.Process(target=run_tensorflow)
        p.start()
        p.join()
    
        # wait until user presses enter key
        raw_input()
    
        # option 2: just execute the function
        run_tensorflow()
    
        # wait until user presses enter key
        raw_input()

    这样使用session会解决你的问题

    https://github.com/tensorflow/tensorflow/issues/1727

    这里是当时的issue

    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题