I have a CNN model with two inputs, like this:
input_a = tf.keras.layers.Input(shape=[128, 128, 3])
input_b = tf.keras.layers.Input(shape=[28, 28, 3])
conv_a = tf.keras.layers.Conv2D(32, 3, 2)(input_a)
conv_b = tf.keras.layers.Conv2D(32, 3, 2)(input_b)
dense_a = tf.keras.layers.Dense(1)(conv_a)
dense_b = tf.keras.layers.Dense(1)(conv_b)
combined = tf.keras.layers.Add()([dense_a, dense_b])
model = tf.keras.models.Model(inputs=[input_a, input_b], outputs=conbined)
I found this network learned more informations from input_b
branch, but my design is input_a
is more important and input_b
assist input_a
. So, I want to set different importances to those two layers, because I want to make input_a
more important. I implement a weighted layer:
class WeightedAdd(tf.keras.layers.Layer):
def __init__(self, weight):
self.weight = weight
super(WeightedAdd, self).__init__()
def call(self, inputs, **kwargs):
return self.weight * inputs[0] + (1. - self.weight) * inputs[1]
def compute_output_shape(self, input_shape):
return input_shape[0]
Now, I replace my Add by WeightedAdd:
combined = tf.keras.layers.Add()([dense_a, dense_b])
combined = WeightedAdd(0.8)([dense_a, dense_b])
But, it seems not work. How can I implement my idea?
question from:
https://stackoverflow.com/questions/65878600/two-inputs-with-different-importance 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…