Keras自定义图层ValueError:一个操作的渐变没有“无”。

时间:2018-07-12 11:38:59

标签: python tensorflow keras

我创建了一个定制的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。

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

2 个答案:

答案 0 :(得分:2)

您正在通过调用原始的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)

答案 1 :(得分:0)

可能是因为您的代码中有一些权重是由未在输出的计算中使用的定义的。因此其损失的梯度为None / undefined。

可在此处找到编码示例:https://github.com/keras-team/keras/issues/12521#issuecomment-496743146