为什么不能在Keras的自定义图层中使用权重约束和辍学功能

问题描述

我想在自定义层中实现辍学和权重约束。该程序未报告错误,但是这两个功能均未报告 自定义层是: ''' 类Softmax_Decode(Layer): “”“使用密钥将稀疏表示解码为softmax的层。

BOOL test(LPTSTR a) {
char res[1024];
strcat(res,"before");
strcat(res,a); //<--
strcat(res,"after");
printf("%s",res);
}

''' 主要代码:

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)])

您能帮我吗?非常感谢您抽出宝贵的时间。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...