【问题标题】:Tensorflow uses same amount of gpu memory regardless of batch size无论批量大小如何,Tensorflow 都使用相同数量的 gpu 内存
【发布时间】:2016-10-13 01:25:01
【问题描述】:

我是 tensorflow 的新手,我正在尝试在 CIFAR 10 数据集上进行训练。我注意到,根据我的 nvidia 控制面板,无论我使用什么批量大小,我都在使用 97% 的 gpu 内存。我尝试了从 100 到 2 的批量大小,在每种情况下,我的 gpu 内存使用率始终为 97%。为什么要这样做?

def batchGenerator(batch_size = 32):
    bi = 0
    random.shuffle(train_data)
    while bi + batch_size < len(train_data):
        x = np.zeros((batch_size, 32, 32, 3))
        y = np.zeros((batch_size, 10))
        for b in range(batch_size):
            x[b] = train_data[bi + b][0]
            if random.choice((True, False)):
                img = cv2.flip(x[b], 0)
            if random.choice((True, False)):
                img = cv2.flip(x[b], 1)
            y[b][train_data[bi + b][1]] = 1
        bi += batch_size
        yield(x, y)

with tf.Session() as s:
    s.run(tf.initialize_all_variables())
    for epoch in range(100):
        a = 0.0
        i = 0
        for x_train, y_train in batchGenerator(2):
            outs = s.run([opt, accuracy], feed_dict = {x: x_train, y_exp: y_train, p_keep: 0.5})
            a += outs[-1]
            i += 1
        print('Epoch', epoch, 'Accuracy', a / i)

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    此问题与How to prevent tensorflow from allocating the totality of a GPU memory?有关


    TensorFlow 默认使用您 GPU 的所有内存,这是正常行为。来自教程Using GPUs

    默认情况下,TensorFlow 将几乎所有 GPU 的所有 GPU 内存都映射到进程可见。这样做是为了通过减少内存碎片来更有效地使用设备上相对宝贵的 GPU 内存资源。


    如果您需要减少 TensorFlow 占用的内存,它们还提供不同的选项,例如:

    config = tf.ConfigProto()
    config.gpu_options.per_process_gpu_memory_fraction = 0.4
    session = tf.Session(config=config, ...)
    

    (来自链接的文档),将内存使用限制为 GPU 可用内存的 40%。

    【讨论】:

      猜你喜欢
      • 2018-05-10
      • 1970-01-01
      • 2018-05-13
      • 1970-01-01
      • 2020-08-23
      • 2012-03-07
      • 1970-01-01
      • 2017-10-22
      • 2016-01-19
      相关资源
      最近更新 更多