Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Theano 装载小批量的有效方法<;gpu存储器_Theano - Fatal编程技术网

Theano 装载小批量的有效方法<;gpu存储器

Theano 装载小批量的有效方法<;gpu存储器,theano,Theano,我有以下情况: 我的数据集>>gpu内存 我的迷你gpu内存。。。这样,根据大小,我可以一次在内存中容纳多达10个,而训练仍然没有问题 我的数据集的大小意味着我不会重新访问数据点,所以我想没有必要让它们共享?还是有?我在想,如果有多达10个共享的初始化变量size=mini-batch可能会有好处,这样我就可以一次交换10个,而不是一次只交换一个。另外,可以并行预加载小批量吗?如果不重新访问数据点,那么使用共享变量可能没有任何价值 下面的代码可以修改并用于评估将数据输入特定计算的不同方法 当

我有以下情况:

  • 我的数据集>>gpu内存
  • 我的迷你gpu内存。。。这样,根据大小,我可以一次在内存中容纳多达10个,而训练仍然没有问题

我的数据集的大小意味着我不会重新访问数据点,所以我想没有必要让它们共享?还是有?我在想,如果有多达10个共享的初始化变量size=mini-batch可能会有好处,这样我就可以一次交换10个,而不是一次只交换一个。另外,可以并行预加载小批量吗?

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

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

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

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

一般来说,小数量的大内存传输比大数量的小内存传输运行得更快(每个传输的总传输量相同)。但这需要测试(例如,使用下面的代码)才能确定;YMMV

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

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()