Theano convolution: TypeError: conv2d() got multiple values for argument 'input'
Theano convolution: TypeError: conv2d() got multiple values for argument 'input'
我正在尝试构建一个 "double" 层,首先使用卷积,然后使用最大池化。该网络将输入 20x20 个输入图像,并应从 [0,25] 输出 class。尝试构建函数时,激活卷积池层时出现错误 TypeError: conv2d() got multiple values for argument '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() 设置为 class 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)
或通过在 __init__
中设置 conv_func 属性。请注意,您将遇到与 pool_func.
相同的问题
我正在尝试构建一个 "double" 层,首先使用卷积,然后使用最大池化。该网络将输入 20x20 个输入图像,并应从 [0,25] 输出 class。尝试构建函数时,激活卷积池层时出现错误 TypeError: conv2d() got multiple values for argument '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() 设置为 class 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)
或通过在 __init__
中设置 conv_func 属性。请注意,您将遇到与 pool_func.