keras - 自定义 Keras 层失败
问题描述
我想要自定义 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
解决方案
也许 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)
当然,您需要在方法中weights
替换为。kernel
call
不相关:
我建议您还使用约束将内核保持在 0 和 1 之间。
from keras.constraints import MinMaxNorm
........
self.kernel = self.add_weight(name='kernel',
shape=(1,),
initializer='uniform',
constraint = MinMaxNorm(0,1)
trainable=True)
........