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

后端 未结 2 1348
轮回少年
轮回少年 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)
    

提交回复
热议问题