Theano - 共享变量作为大型数据集的函数输入

Eri*_*uth 4 python theano conv-neural-network

我是Theano的新手......如果这很明显,我道歉.

我正在尝试根据LeNet教程培训CNN .与本教程的主要区别在于我的数据集太大而无法放入内存中,所以我必须在训练期间加载每个批处理.

原始模型有:

train_model = theano.function(
    [index],
    cost,
    updates=updates,
    givens={
        x: train_set_x[index * batch_size: (index + 1) * batch_size],
        y: train_set_y[index * batch_size: (index + 1) * batch_size]
    }
)
Run Code Online (Sandbox Code Playgroud)

...这对我不起作用,因为它假定它train_set_x完全加载到内存中.

所以我改用这个:

train_model = theano.function([x,y], cost, updates=updates)
Run Code Online (Sandbox Code Playgroud)

并试图用它来称呼它:

data, target = load_data(minibatch_index)  # load_data returns typical numpy.ndarrays for a given minibatch

data_shared = theano.shared(np.asarray(data, dtype=theano.config.floatX), borrow=True)
target_shared = T.cast(theano.shared(np.asarray(target, dtype=theano.config.floatX), borrow=True), 'int32')

cost_ij = train_model(data_shared ,target_shared )
Run Code Online (Sandbox Code Playgroud)

但得到了:

TypeError :('ofano函数的错误输入参数,名称为":103",索引0(从0开始)','期望类似于数组的对象,但找到一个变量:也许你正在尝试调用一个函数(可能共享)变量而不是数字数组?')

所以我想我不能使用共享变量作为Theano函数的输入.但是,我该怎么办......?

Dan*_*haw 5

编译的Theano函数的所有输入(即调用的输出theano.function(...))应始终是具体值,通常是标量或numpy数组.共享变量是一种包装numpy数组并将其视为符号变量的方法,但是当数据作为输入传递时,这不是必需的.

因此,您应该能够省略将数据和目标值包装为共享变量,而是执行以下操作:

cost_ij = train_model(data, target)
Run Code Online (Sandbox Code Playgroud)

请注意,如果您使用的是GPU,这意味着您的数据将驻留在计算机的主内存中,并且您作为输入传递的每个部分都需要单独复制到GPU内存,从而增加了开销并降低了速度.另请注意,您必须将数据分开并仅传递部分数据; 如果整个数据集不适合GPU内存,这种方法的改变将不允许您立即对整个数据集进行GPU计算.