Python 为什么不能在Keras上的自定义图层中使用权重约束和退出功能

Python 为什么不能在Keras上的自定义图层中使用权重约束和退出功能,python,keras,constraints,keras-layer,dropout,Python,Keras,Constraints,Keras Layer,Dropout,我想在自定义层中实现退出和权重约束。程序没有报告错误,但这两个函数也没有报告错误 自定义图层为: ''' 类Softmax_解码(层): “”“使用密钥将稀疏表示解码为softmax的层。” Makes it easier to train spiking classifiers by allowing the use of softmax and catagorical-crossentropy loss. Allows for encodings that are n-hot wher

我想在自定义层中实现退出和权重约束。程序没有报告错误,但这两个函数也没有报告错误 自定义图层为: ''' 类Softmax_解码(层): “”“使用密钥将稀疏表示解码为softmax的层。”

Makes it easier to train spiking classifiers by allowing the use of  
softmax and catagorical-crossentropy loss. Allows for encodings that are 
n-hot where 'n' is the number of outputs assigned to each class. Allows
encodings to overlap, where a given output neuron can contribute 
to the probability of more than one class.

# Arguments
    key: A numpy array (num_classes, input_dims) with an input_dim-sized
        {0,1}-vector representative for each class.
    size: A tuple (num_classes, input_dim).  If ``key`` is not specified, then
        size must be specified.  In which case, a key will automatically be generated.
"""
def __init__(self, key=None,size=None,kernel_regularizer=None,kernel_constraint=None,dropout=0.0,**kwargs):
    super(Softmax_Decode, self).__init__(**kwargs)
    self.key = _key_check(key, size)
    if type(self.key) is dict and 'value' in self.key.keys():
        self.key = np.array(self.key['value'], dtype=np.float32)
    elif type(self.key) is list:
        self.key = np.array(self.key, dtype=np.float32)
    #self._rescaled_key = K.variable(np.transpose(2*self.key-1))
    self._rescaled_key = K.variable(2*np.transpose(self.key)-1)
    self.kernel_regularizer = regularizers.get(kernel_regularizer)
    self.kernel_constraint = constraints.get(kernel_constraint)
    self.dropout = dropout

def build(self, input_shape):
    self.kernel = self.add_weight(name='kernel',
                                  #shape=(input_shape[1], self.key.shape[1]),
                                  initializer='uniform',
                                  regularizer=self.kernel_regularizer,
                                  constraint=self.kernel_constraint)
    super(Softmax_Decode, self).build(input_shape)

def call(self, inputs):
    #return K.softmax(K.dot(2*(1-inputs),self._rescaled_key))
    return K.softmax(K.dot(2*inputs-1, self._rescaled_key))

def compute_output_shape(self, input_shape):
    return (input_shape[0],self.key.shape[0])

def get_config(self):
    config={
    'kernel_regularizer': regularizers.serialize(self.kernel_regularizer),
    'kernel_constraint': constraints.serialize(self.kernel_constraint)
    }
    base_config = super(Softmax_Decode, self).get_config()
    return dict(list(base_config.items()) + [('key', self.key)])
''' 主要代码:

model.add(Softmax_Decode(key,kernel_constraint=MinMaxNorm(min_value=0, max_value=1.0, rate=1.0, axis=0),dropout=0.3))

您能帮助我吗?非常感谢您的时间。

我不明白,问题出在哪里?您的意思是什么不能使用?您的代码没有使用dropout参数或self.kernel,因此当然这些都不会做任何事情。谢谢您的回答。dropout没有应用到自定义层吗?您能给我一些改进建议吗它?最终目标是在自定义层中使用dropout和weight约束。对于dropout,您需要在代码中使用keras.backend.dropout函数。我不确定使用weight约束的意思,您是否注意到调用函数不使用任何权重?我注意到代码中没有weight函数,b但是参数的结果包括该层的权重(我对此也有疑问)。权重约束指的是将参数限制在一个范围内。“对于退出,您需要在代码中使用keras.backend.dropout函数。”“,我试图根据你的建议加以改进。非常感谢你!!该层具有权重,因为您使用了“添加权重”,但这些权重不会用于任何计算