微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决为什么不能在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 举报,一经查实,本站将立刻删除。