Keras Custom Layer ValueError: An operation has `None` for gradient.

后端 未结 2 1349
轮回少年
轮回少年 2021-01-15 20:46

I have created a custom Keras Conv2D layer as follows:

class CustConv2D(Conv2D):

    def __init__(self, filters, kernel_size, kernelB=None, activation=None,         


        
相关标签:
2条回答
  • 2021-01-15 21:28

    You're destroying your build by calling the original Conv2D build (your self.kernel will be replaced, then self.kernelA will never be used, thus backpropagation will never reach it).

    It's also expecting biases and all the regular stuff:

    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)
    
    0 讨论(0)
  • 2021-01-15 21:32

    It may be because there are some weights in your code that are defined by not used in the calculation of the output. Thus its gradient wrt the loss is None/undefined.

    A coded out example can be found here: https://github.com/keras-team/keras/issues/12521#issuecomment-496743146

    0 讨论(0)
提交回复
热议问题