2015-06-17 21 views
2

我想在Theano中为CNN网络实现自定义卷积层,并且为了这样做,我使用了扫描功能。这个想法是将新的卷积掩模应用于每个像素。来自Theano的扫描功能复制non_sequences共享变量

scan函数编译正确,但出于某种原因,我收到了内存不足的错误。调试(见下文)表示non_sequences变量复制为循环的每个实例(每个像素),这当然杀死了我的GPU内存:

def convolve_location(index, input, bias): 
    hsize = self.W.shape/2 
    t = T.switch(index[0]-hsize[0] < 0, 0, index[0]-hsize[0]) 
    l = T.switch(index[1]-hsize[1] < 0, 0, index[1]-hsize[1]) 
    b = T.switch(index[0]+hsize[0] >= input.shape[2], input.shape[2]-1, index[0]+hsize[0]) 
    r = T.switch(index[1]+hsize[1] >= input.shape[3], input.shape[3]-1, index[1]+hsize[1]) 

    r_image = (input[:, :, t:b, l:r] - input[:, :, index[0], index[1]][:, :, None, None]) ** 2 
    r_delta = (bias[:, :, t:b, l:r] - bias[:, :, index[0], index[1]][:, :, None, None]) ** 2 
    return T.sum(r_image*r_delta) 

# # Define cost function over all pixels 
self.inds = theano.shared(np.array([(i, j) for i in range(self.image_shape[2]) for j in range(self.image_shape[3])], dtype='int32'), borrow=True) 
self.cost = T.sum(theano.scan(
    fn=convolve_location, 
    outputs_info=None, 
    sequences=[self.inds], 
    non_sequences=[self.input, self.b], 
    n_steps=np.prod(self.image_shape[-2:]) 
)[0]) 

下面是从调试器输出:

MemoryError: alloc failed Apply node that caused the error: Alloc(TensorConstant{0.0}, TensorConstant{1025}, TensorConstant{2000}, TensorConstant{3}, TensorConstant{32}, TensorConstant{32}) Inputs types: [TensorType(float32, scalar), TensorType(int64, scalar), TensorType(int64, scalar), TensorType(int64, scalar), TensorType(int64, scalar), TensorType(int64, scalar)] Inputs shapes: [(),(),(),(),(),()] Inputs strides: [(),(),(),(),(),()] Inputs values: [array(0.0, dtype=float32), array(1025), array(2000), array(3), array(32), array(32)] 

Debugprint of the apply node: Alloc [@A] <TensorType(float32, 5D)> '' |TensorConstant{0.0} [@B] <TensorType(float32, scalar)> |TensorConstant{1025} [@C] <TensorType(int64, scalar)> |TensorConstant{2000} [@D] <TensorType(int64, scalar)> |TensorConstant{3} [@E] <TensorType(int64, scalar)> |TensorConstant{32} [@F] <TensorType(int64, scalar)> |TensorConstant{32} [@F] <TensorType(int64, scalar)> Storage map footprint: 
- CudaNdarrayConstant{[[[[ 0.]]]]}, Shape: (1, 1, 1, 1), ElemSize: 4 Byte(s), TotalSize: 4 Byte(s) 
- Constant{18}, Shape: (1,), ElemSize: 8 Byte(s), TotalSize: 8.0 Byte(s) 

- TensorConstant{(1, 1) of 0}, Shape: (1, 1), ElemSize: 1 Byte(s), TotalSize: 1 Byte(s) 
- Constant{1024}, Shape: (1,), ElemSize: 8 Byte(s), TotalSize: 8.0 Byte(s) 
- Constant{-1}, Shape: (1,), ElemSize: 8 Byte(s), TotalSize: 8.0 Byte(s) 
- TensorConstant{32}, Shape: (1,), ElemSize: 8 Byte(s), TotalSize: 8.0 Byte(s) 
- Subtensor{:int64:}.0, Shape: (1024,), ElemSize: 4 Byte(s), TotalSize: 4096 Byte(s) 
- Constant{34}, Shape: (1,), ElemSize: 8 Byte(s), TotalSize: 8.0 Byte(s) 
- Constant{2}, Shape: (1,), ElemSize: 8 Byte(s), TotalSize: 8.0 Byte(s) 
- TensorConstant{[2000 3.. 32 32]}, Shape: (4,), ElemSize: 8 Byte(s), TotalSize: 32 Byte(s) 
- Reshape{4}.0, Shape: (2000, 3, 32, 32), ElemSize: 4 Byte(s), TotalSize: 24576000 Byte(s) 
- TensorConstant{(1, 1, 1, 1) of 0}, Shape: (1, 1, 1, 1), ElemSize: 1 Byte(s), TotalSize: 1 Byte(s) 
- CudaNdarrayConstant{[[[[ 0.1]]]]}, Shape: (1, 1, 1, 1), ElemSize: 4 Byte(s), TotalSize: 4 Byte(s) 
- <TensorType(float32, matrix)>, Shape: (50000, 3072), ElemSize: 4 Byte(s), TotalSize: 614400000 Byte(s) 

,你可以+ 1

为什么是non_sequences变量看到显示为1025x2000x3x32x32张量,而原来的张量大小2000x3x32x32,而1025是扫描的迭代次数输入为每个迭代复制而不是简单地被重用,我该如何解决它?

编辑:

两个self.inputself.b和共享变量。 Self.input被传递到初始化当类,而self.b在类内定义如下:

self.b = theano.shared(np.zeros(image_shape, dtype=theano.config.floatX), borrow=True) 
+0

您没有显示如何定义'self.input'和'self.b'。它们是共享变量吗?另外它可以帮助调试给你的Theano变量名称。 – cfh

+0

感谢cfh,我编辑了这篇文章。这两个变量确实是共享的。命名它们虽然会有点混乱,因为网络中的每一层都会生成它们自己的这些变量版本。 – gaspercat

回答

2

有可能的是,当第一次创建扫描或在优化过程中的某个时刻,创建具有该形状的符号Alloc。然而,它应该在优化过程的后期阶段进行优化。

我们知道最近有一个但与此相关的问题,现在应该在Theano的开发(“最新版”)版本中解决。事实上,我只是用最近的开发版本试过你的代码片段(稍微编辑过),并没有内存错误。而且,计算图中的任何地方都没有5D张量,这表明该错误确实已被修复。

最后,请注意,如果卷积操作(如卷积不真正反复出现),用scan而不是现有卷积操作中的一种表示时,可能会慢得多。特别是,当循环迭代不相互依赖时,scan将无法​​高效并行化。