使用 borrow=True 创建 Theano 共享变量时内存不足

Out of memory when creating a Theano shared variable with borrow=True

我正在尝试将一个非常大的数据集(ndarray 中约 28GB 的​​ RAM)分配到 theano 共享变量中,使用 borrow=True 来避免复制内存。为此,我使用了以下函数:

def load_dataset(path):
    # Load dataset from memory
    data_f = np.load(path+'train_f.npy')
    data_t = np.load(path+'train_t.npy')

    # Split into training and validation
    return (
        (
            theano.shared(data_f[:-1000, :], borrow=True),
            theano.shared(data_t[:-1000, :], borrow=True)
        ), (
            theano.shared(data_f[-1000:, :], borrow=True),
            theano.shared(data_t[-1000:, :], borrow=True)
        )
    )

为了避免数据转换,在将数组保存到磁盘之前,我已经将它们定义为正确的格式(之后用 np.save() 填充它们并将它们转储到磁盘):

data_f = np.ndarray((len(rows), 250*250*3), dtype=theano.config.floatX)
data_t = np.ndarray((len(rows), 1), dtype=theano.config.floatX)

尽管如此,theano 似乎厌倦了复制内存,向我抛出以下错误:

分配 25594500000 字节的设备内存时出错(内存不足)。驱动程序报告 3775729664 字节可用,总计 4294639616 字节。

Theano 配置为在 GPU (GTX 970) 上工作。

可以使用 theano.tensor._shared 来强制将数据分配到 CPU 内存中,而不是使用 theano.shared。固定代码最终是这样的:

def load_dataset(path):
    # Load dataset from memory
    data_f = np.load(path+'train_f.npy')
    data_t = np.load(path+'train_t.npy')

    # Split into training and validation
    return (
        (
            theano.tensor._shared(data_f[:-1000, :], borrow=True),
            theano.tensor._shared(data_t[:-1000, :], borrow=True)
        ), (
            theano.tensor._shared(data_f[-1000:, :], borrow=True),
            theano.tensor._shared(data_t[-1000:, :], borrow=True)
        )
    )