装载小型车的有效方式< gpu内存

时间:2015-10-21 00:20:51

标签: theano

我有以下情况:

  • 我的数据集>> gpu memory
  • 我的迷你吧< gpu memory ...这样根据大小,我可以同时在内存中容纳10个,同时仍然可以训练没问题。

我的数据集的大小意味着我不会重新访问数据点,所以我认为没有必要让它们共享?还是有吗?我想也许最多有10个共享的初始化变量size = mini-batch是有益的,这样我就可以一次交换10个而不是一次交换一个。另外,是否可以并行预装小批量?

1 个答案:

答案 0 :(得分:5)

如果您没有重新访问数据点,那么使用共享变量可能没有任何价值。

以下代码可以修改并用于评估将数据导入特定计算的不同方法。

当您不需要重新访问数据时,“输入”方法可能是最好的方法。 “shared_all”方法可能胜过其他所有方法,但前提是您可以将整个数据集放在GPU内存中。 “shared_batched”允许您评估对数据进行分层批处理是否有帮助。

在“shared_batched”方法中,数据集分为许多宏批处理,每个宏批处理分为许多微批处理。单个共享变量用于保存单个宏批处理。代码评估当前宏批处理中的所有微批次。处理完一个宏批处理后,下一个宏批处理将加载到共享变量中,代码将再次遍历其中的微批处理。

一般来说,可能需要少量的大内存传输比大量较小的传输(其中每个传输的总数相同)运行得更快。但是在确定之前需要对此进行测试(例如使用下面的代码); YMMV。

使用“借用”参数也可能对性能产生重大影响,但在使用之前请注意the implications

import math
import timeit
import numpy
import theano
import theano.tensor as tt


def test_input(data, batch_size):
    assert data.shape[0] % batch_size == 0
    batch_count = data.shape[0] / batch_size
    x = tt.tensor4()
    f = theano.function([x], outputs=x.sum())
    total = 0.
    start = timeit.default_timer()
    for batch_index in xrange(batch_count):
        total += f(data[batch_index * batch_size: (batch_index + 1) * batch_size])
    print 'IN\tNA\t%s\t%s\t%s\t%s' % (batch_size, batch_size, timeit.default_timer() - start, total)


def test_shared_all(data, batch_size):
    batch_count = data.shape[0] / batch_size
    for borrow in (True, False):
        start = timeit.default_timer()
        all = theano.shared(data, borrow=borrow)
        load_time = timeit.default_timer() - start
        x = tt.tensor4()
        i = tt.lscalar()
        f = theano.function([i], outputs=x.sum(), givens={x: all[i * batch_size:(i + 1) * batch_size]})
        total = 0.
        start = timeit.default_timer()
        for batch_index in xrange(batch_count):
            total += f(batch_index)
        print 'SA\t%s\t%s\t%s\t%s\t%s' % (
            borrow, batch_size, batch_size, load_time + timeit.default_timer() - start, total)


def test_shared_batched(data, macro_batch_size, micro_batch_size):
    assert data.shape[0] % macro_batch_size == 0
    assert macro_batch_size % micro_batch_size == 0
    macro_batch_count = data.shape[0] / macro_batch_size
    micro_batch_count = macro_batch_size / micro_batch_size
    macro_batch = theano.shared(numpy.empty((macro_batch_size,) + data.shape[1:], dtype=theano.config.floatX),
                                borrow=True)
    x = tt.tensor4()
    i = tt.lscalar()
    f = theano.function([i], outputs=x.sum(), givens={x: macro_batch[i * micro_batch_size:(i + 1) * micro_batch_size]})
    for borrow in (True, False):
        total = 0.
        start = timeit.default_timer()
        for macro_batch_index in xrange(macro_batch_count):
            macro_batch.set_value(
                data[macro_batch_index * macro_batch_size: (macro_batch_index + 1) * macro_batch_size], borrow=borrow)
            for micro_batch_index in xrange(micro_batch_count):
                total += f(micro_batch_index)
        print 'SB\t%s\t%s\t%s\t%s\t%s' % (
            borrow, macro_batch_size, micro_batch_size, timeit.default_timer() - start, total)


def main():
    numpy.random.seed(1)

    shape = (20000, 3, 32, 32)

    print 'Creating random data with shape', shape
    data = numpy.random.standard_normal(size=shape).astype(theano.config.floatX)

    print 'Running tests'
    for macro_batch_size in (shape[0] / pow(10, i) for i in xrange(int(math.log(shape[0], 10)))):
        test_shared_all(data, macro_batch_size)
        test_input(data, macro_batch_size)
        for micro_batch_size in (macro_batch_size / pow(10, i) for i in
                                 xrange(int(math.log(macro_batch_size, 10)) + 1)):
            test_shared_batched(data, macro_batch_size, micro_batch_size)


main()