Neural network Theano卷积:TypeError:conv2d()为参数';输入';

Neural network Theano卷积:TypeError:conv2d()为参数';输入';,neural-network,theano,deep-learning,conv-neural-network,Neural Network,Theano,Deep Learning,Conv Neural Network,我正在尝试构建一个“双”层,首先是卷积层,然后是最大池。网络将被馈送20x20输入图像,并从[0,25]输出一个类。在尝试构建函数时,我得到一个错误TypeError:conv2d()在激活卷积池层时为参数'input'获得了多个值 class ConvPoolLayer: conv_func = T.nnet.conv2d pool_func = max_pool_2d def __init__(self, image_shape, n_feature_maps, a

我正在尝试构建一个“双”层,首先是卷积层,然后是最大池。网络将被馈送20x20输入图像,并从[0,25]输出一个类。在尝试构建函数时,我得到一个错误
TypeError:conv2d()在激活卷积池层时为参数'input'
获得了多个值

class ConvPoolLayer:
    conv_func = T.nnet.conv2d
    pool_func = max_pool_2d

    def __init__(self, image_shape, n_feature_maps, act_func,
                 local_receptive_field_size=(5,5), pool_size=(2,2),
                 init_weight_func=init_rand_weights, init_bias_weight_func=init_rand_weights):
        """
        Generate a convolutional and a subsequent pooling layer with one bias node for each channel in the pooling layer.
        :param image_shape: tuple(batch size, input channels, input rows, input columns) where
            input_channels = number of feature maps in upstream layer
            input rows, input columns = output size of upstream layer
        :param n_feature_maps: number of feature maps/filters in this layer
        :param local_receptive_field_size: = size of local receptive field
        :param pool_size:
        :param act_func:
        :param init_weight_func:
        :param init_bias_weight_func:
        """
        self.image_shape = image_shape
        self.filter_shape = (n_feature_maps, image_shape[1]) + local_receptive_field_size
        self.act_func = act_func
        self.pool_size = pool_size
        self.weights = init_weight_func(self.filter_shape)
        self.bias_weights = init_bias_weight_func((n_feature_maps,))
        self.params = [self.weights, self.bias_weights]
        self.output_values = None

    def activate(self, input_values):
        """
        :param input_values: the output from the upstream layer (which is input to this layer)
        :return:
        """
        input_values = input_values.reshape(self.image_shape)
        conv = self.conv_func(
            input=input_values,
            image_shape=self.image_shape,
            filters=self.weights,
            filter_shape=self.filter_shape
        )
        pooled = self.pool_func(
            input=conv,
            ds=self.pool_size,
            ignore_border=True
        )
        self.output_values = self.act_func(pooled + self.bias_weights.dimshuffle('x', 0, 'x', 'x'))

    def output(self):
        assert self.output_values is not None, 'Asking for output before activating layer'
        return self.output_values


def test_conv_layer():
    batch_size = 10
    input_shape = (20, 20)
    output_shape = (26,)
    image_shape = (batch_size, 1) + input_shape  # e.g image_shape = (10, 1, 20, 20)
    n_feature_maps = 10
    convpool_layer = ConvPoolLayer(image_shape, n_feature_maps, T.nnet.relu)

    x = T.fmatrix('X')
    y = T.fmatrix('Y')

    convpool_layer.activate(x)


test_conv_layer()

问题是您将conv_func()设置为类ConvPoolLayer()上的方法。所以当你这样做的时候:

conv = self.conv_func(input=input_values,
                      image_shape=self.image_shape,
                      filters=self.weights,
                      filter_shape=self.filter_shape)
Python的幕后工作是:

conv = ConvPoolLayer.conv_func(self, input=input_values,
                               image_shape=self.image_shape,
                               filters=self.weights,
                               filter_shape=self.filter_shape)
由于
input
是第一个参数,因此可以为其提供多个值

可以通过如下方式在staticmethod()中包装方法来避免此问题:

conv_func = staticmethod(T.nnet.conv2d)

或者从
\uuu init\uuu
中设置conv_func属性。请注意,pool_func也会遇到同样的问题。

问题在于您将conv_func()设置为类ConvPoolLayer()上的方法。所以当你这样做的时候:

conv = self.conv_func(input=input_values,
                      image_shape=self.image_shape,
                      filters=self.weights,
                      filter_shape=self.filter_shape)
Python的幕后工作是:

conv = ConvPoolLayer.conv_func(self, input=input_values,
                               image_shape=self.image_shape,
                               filters=self.weights,
                               filter_shape=self.filter_shape)
由于
input
是第一个参数,因此可以为其提供多个值

可以通过如下方式在staticmethod()中包装方法来避免此问题:

conv_func = staticmethod(T.nnet.conv2d)

或者从
\uuu init\uuu
中设置conv_func属性。请注意,pool_func也会遇到同样的问题。

非常感谢。我已经苦思冥想了好几个小时,一事无成!可选的后续问题:为什么在将偏差权重添加到池层之前需要对其进行“dimshuffle”
self.output\u values=self.act\u func(pooled+self.bias\u weights.dimshuffle('x',0',x','x'))
是否可以首先将偏差权重设置为正确的形状?如果需要,可以将偏差权重设置为(1,n\u特征映射,1,1),但dimshuffling只是暂时做到这一点。您需要这样做的原因是,需要具有相同的(广播兼容)形状才能添加到一起。非常感谢。我已经苦思冥想了好几个小时,一事无成!可选的后续问题:为什么在将偏差权重添加到池层之前需要对其进行“dimshuffle”
self.output\u values=self.act\u func(pooled+self.bias\u weights.dimshuffle('x',0',x','x'))
是否可以首先将偏差权重设置为正确的形状?如果需要,可以将偏差权重设置为(1,n\u特征映射,1,1),但dimshuffling只是暂时做到这一点。您需要这样做的原因是,事物需要具有相同的(广播兼容)形状才能添加到一起。