Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

tensorflow - two inputs with different importance

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Fine! It seems works~~. Set the weight of input_a more bigger than input_b like 0.9999, the difference after update of input_a is bigger than input_b. But I'm not sure it whether or not works for my idea.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...