Keras自定义图层ValueError:操作没有“ None”用于渐变。


问题内容

我创建了一个定制的Keras Conv2D图层,如下所示:

class CustConv2D(Conv2D):

    def __init__(self, filters, kernel_size, kernelB=None, activation=None, **kwargs): 
        self.rank = 2
        self.num_filters = filters
        self.kernel_size = conv_utils.normalize_tuple(kernel_size, self.rank, 'kernel_size')
        self.kernelB = kernelB
        self.activation = activations.get(activation)

        super(CustConv2D, self).__init__(self.num_filters, self.kernel_size, **kwargs)

    def build(self, input_shape):
        if K.image_data_format() == 'channels_first':
            channel_axis = 1
        else:
            channel_axis = -1
        if input_shape[channel_axis] is None:
            raise ValueError('The channel dimension of the inputs '
                     'should be defined. Found `None`.')

        input_dim = input_shape[channel_axis]
        num_basis = K.int_shape(self.kernelB)[-1]

        kernel_shape = (num_basis, input_dim, self.num_filters)

        self.kernelA = self.add_weight(shape=kernel_shape,
                                      initializer=RandomUniform(minval=-1.0, 
                                      maxval=1.0, seed=None),
                                      name='kernelA',
                                      regularizer=self.kernel_regularizer,
                                      constraint=self.kernel_constraint)

        self.kernel = K.sum(self.kernelA[None, None, :, :, :] * self.kernelB[:, :, :, None, None], axis=2)

        # Set input spec.
        self.input_spec = InputSpec(ndim=self.rank + 2, axes={channel_axis: input_dim})
        self.built = True
        super(CustConv2D, self).build(input_shape)

我将CustomConv2D用作模型的第一个Conv层。

img = Input(shape=(width, height, 1))
l1 = CustConv2D(filters=64, kernel_size=(11, 11), kernelB=basis_L1, activation='relu')(img)

该模型编译良好;但是在训练时给了我以下错误。

ValueError:操作具有None渐变。请确保您所有的操作都定义了渐变(即可区分)。不带渐变的常见操作:K.argmax,K.round,K.eval。

有没有办法找出哪个操作引发了错误?另外,编写自定义层的方式是否存在任何实现错误?


问题答案:

您正在通过调用原始的Conv2D构建来破坏构建(您的构建self.kernel将被替换,然后self.kernelA将不再使用,因此反向传播将永远无法实现)。

它还期望有偏差和所有常规内容:

class CustConv2D(Conv2D):

    def __init__(self, filters, kernel_size, kernelB=None, activation=None, **kwargs):

        #...
        #...

        #don't use bias if you're not defining it:
        super(CustConv2D, self).__init__(self.num_filters, self.kernel_size, 
              activation=activation,
              use_bias=False, **kwargs)

        #bonus: don't forget to add the activation to the call above
        #it will also replace all your `self.anything` defined before this call


    def build(self, input_shape):

        #...
        #...

        #don't use bias:
        self.bias = None

        #consider the layer built
        self.built = True

        #do not destroy your build
        #comment: super(CustConv2D, self).build(input_shape)