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

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 用verilog实现tanh函数和softplus函数
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题