自定义Keras层失败

自定义Keras层失败,keras,neural-network,keras-layer,Keras,Neural Network,Keras Layer,我想定制Keras层,实现两个模型的输出分配不同的权重,权重可以训练如下 prediction1=model1.output prediction2=model2.output class WeightedSum(Layer): def __init__(self,**kwargs): super(WeightedSum, self).__init__(**kwargs) def build(self, input_shape): self.wei

我想定制Keras层,实现两个模型的输出分配不同的权重,权重可以训练如下

prediction1=model1.output
prediction2=model2.output
class WeightedSum(Layer):
    def __init__(self,**kwargs):
        super(WeightedSum, self).__init__(**kwargs)
    def build(self, input_shape):
        self.weights =K.variable(np.random.random(1))
        self.trainable_weights=[self.weights]
    def call(self, two_model_outputs):
        return self.weights * two_model_outputs[0] + (1 - self.weights) * two_model_outputs[1]
    def compute_output_shape(self, input_shape):
        return input_shape[0]
final_pred=WeightedSum()([prediction1,prediction2])

但是我在写作中犯了一个错误,不知道怎么做。
    Traceback (most recent call last):
      File "test-paper3.py", line 182, in <module>
        final_pred=WeightedSum()([prediction1,prediction2])
      File "/root/anaconda3/lib/python3.7/site-packages/keras/engine/base_layer.py", line 431, in __call__
        self.build(unpack_singleton(input_shapes))
      File "test-paper3.py", line 162, in build
        self.weights =K.variable(np.random.random(1))
    AttributeError: can't set attribute

回溯(最近一次呼叫最后一次):
文件“test-paper3.py”,第182行,在
final_pred=WeightedSum()([prediction1,prediction2])
文件“/root/anaconda3/lib/python3.7/site packages/keras/engine/base\u layer.py”,第431行,在调用中__
自我构建(解包单例(输入形状))
文件“test-paper3.py”,第162行,内部版本
自权重=K.变量(np.随机.随机(1))
AttributeError:无法设置属性

也许Keras是在保护自己,不让你使用它认为是某种保留的单词

尝试以标准方式添加权重,并使用另一个变量名:

def build(self, input_shape):
    self.kernel = self.add_weight(name='kernel', 
                                  shape=(1,),
                                  initializer='uniform',
                                  #I suggest a constraint here, see below
                                  trainable=True)

    #this works as an initializer for the weights
    K.set_value(self.kernel, np.array([0.5])) 
        #you can use np.random here, but it seems safer to go with 0.5

    #this tells keras that the layer is build in fact
    super(WeightedSum, self).build(shapes)
当然,您需要在
call
方法中将
weights
替换为
kernel


无关:

我建议您也使用约束将内核保持在0和1之间

from keras.constraints import MinMaxNorm


........
    self.kernel = self.add_weight(name='kernel', 
                                  shape=(1,),
                                  initializer='uniform',
                                  constraint = MinMaxNorm(0,1)
                                  trainable=True)
........

也许Keras是在保护自己,不让你使用它认为有保留的词

尝试以标准方式添加权重,并使用另一个变量名:

def build(self, input_shape):
    self.kernel = self.add_weight(name='kernel', 
                                  shape=(1,),
                                  initializer='uniform',
                                  #I suggest a constraint here, see below
                                  trainable=True)

    #this works as an initializer for the weights
    K.set_value(self.kernel, np.array([0.5])) 
        #you can use np.random here, but it seems safer to go with 0.5

    #this tells keras that the layer is build in fact
    super(WeightedSum, self).build(shapes)
当然,您需要在
call
方法中将
weights
替换为
kernel


无关:

我建议您也使用约束将内核保持在0和1之间

from keras.constraints import MinMaxNorm


........
    self.kernel = self.add_weight(name='kernel', 
                                  shape=(1,),
                                  initializer='uniform',
                                  constraint = MinMaxNorm(0,1)
                                  trainable=True)
........

那么,怎么了?怎么了?其中?回溯(最后一次调用):文件“test-paper3.py”,第182行,在final_pred=WeightedSum()([prediction1,prediction2])文件/root/anaconda3/lib/python3.7/site packages/keras/engine/base_layer.py中,第431行,在call self.build(unpack_singleton(input_shapes))文件“test-paper3.py”中,第162行,内建self.weights=K.variable(np.random.random(1))AttributeError:无法设置attributeSo,有什么问题吗?怎么了?其中?回溯(最后一次调用):文件“test-paper3.py”,第182行,在final_pred=WeightedSum()([prediction1,prediction2])文件/root/anaconda3/lib/python3.7/site packages/keras/engine/base_layer.py中,第431行,在call self.build(unpack_singleton(input_shapes))文件“test-paper3.py”中,第162行,内部版本self.weights=K.variable(np.random.random(1))AttributeError:无法设置属性