2015-10-07 56 views
0

如何擴展theanos downsample.max_pool_2d_same_size以便不僅可以在特徵映射中共享,還可以在這些映射之間以高效方式共享?假設我得到了3張特徵地圖,每張特徵地圖的大小均爲10x10,這將是一個4D張量(1,3,10,10)。首先讓每個(10,10)特徵映射的最大池((2,2),不重疊)。結果是3個稀疏特徵映射,仍然(10,10),但大多數值等於零:在(2,2)窗口內至多是一個大於零的值。這是什麼downsample.max_pool_2d_same_size做。接下來,我想比較某個(2,2)窗口的每個最大值與窗口的所有其他特徵映射的所有其他最大值在同一位置。 我想只保留所有功能地圖中的最大值。結果又是3個特徵圖(10,10),幾乎所有的值都是零。Theano max_pool_3d

有沒有這樣做的快速方法? 我不介意其他max_pooling功能,但我需要的最大值的確切位置爲pooling/unpooling的目的(但這是另一個話題)。

回答

2

我解決了它與cudnn烤寬麪條。以下是一些最簡單的示例,介紹如何獲取最大池操作(2d和3d)的索引。見https://groups.google.com/forum/#!topic/lasagne-users/BhtKsRmFei4

import numpy as np 
import theano 
import theano.tensor as T 
from theano.tensor.type import TensorType 
from theano.configparser import config 
import lasagne 

def tensor5(name=None, dtype=None): 
    if dtype is None: 
     dtype = config.floatX 
    type = TensorType(dtype, (False, False, False, False, False)) 
    return type(name) 

def max_pooling_2d(): 
    input_var = T.tensor4('input') 
    input_layer = lasagne.layers.InputLayer(shape=(None, 2, 4, 4), input_var=input_var) 
    max_pool_layer = lasagne.layers.MaxPool2DLayer(input_layer, pool_size=(2, 2)) 

    pool_in, pool_out = lasagne.layers.get_output([input_layer, max_pool_layer]) 
    indices = T.grad(None, wrt=pool_in, known_grads={pool_out: T.ones_like(pool_out)}) 
    get_indices_fn = theano.function([input_var], indices,allow_input_downcast=True) 

    data = np.random.randint(low=0, high=9, size=32).reshape((1,2,4,4)) 
    indices = get_indices_fn(data) 
    print data, "\n\n", indices 

def max_pooling_3d(): 
    input_var = tensor5('input') 
    input_layer = lasagne.layers.InputLayer(shape=(1, 1, 2, 4, 4), input_var=input_var) 
    # 5 input dimensions: (batchsize, channels, 3 spatial dimensions) 
    max_pool_layer = lasagne.layers.dnn.MaxPool3DDNNLayer(input_layer, pool_size=(2, 2, 2)) 

    pool_in, pool_out = lasagne.layers.get_output([input_layer, max_pool_layer]) 
    indices = T.grad(None, wrt=pool_in, known_grads={pool_out: T.ones_like(pool_out)}) 
    get_indices_fn = theano.function([input_var], indices,allow_input_downcast=True) 

    data = np.random.randint(low=0, high=9, size=32).reshape((1,1,2,4,4)) 
    indices = get_indices_fn(data) 
    print data, "\n\n", indices