TransWikia.com

How to perform weighted addition of multiple layers in Keras using subclassing?

Data Science Asked on April 5, 2021

I am trying subclassing to perform weighted addition of three layers in Keras. The code for the class is shown below:

class Wt_Add(keras.layers.Layer):
  def __init__(self, units=1, input_dim=1):
      super(Wt_Add, self).__init__()
      w_init = tf.initializers.GlorotUniform()
      self.w1 = tf.Variable(
        initial_value=w_init(shape=(input_dim,units),dtype="float32"),
        trainable=True,
    )
      self.w2 = tf.Variable(
        initial_value=w_init(shape=(input_dim, units), dtype="float32"),
        trainable=True,
    )       

  def call(self, input1, input2, input3):
      sum1 = tf.multiply(input1,self.w1) + tf.multiply(input2, self.w2) + tf.multiply(input3,(1-self.w1-self.w2))
      return sum1

However, when I am using it in the code (shown below)

A = Input(shape=(128,128,3), name='inputA')
B = Input(shape=(128,128,3), name='inputB')
C = Input(shape=(128,128,1), name='inputL')

wt_add = Wt_Add()
ad1 = wt_add(A,B,C)

model = Model([A,B,C], ad1, name = 'model')
keras.utils.plot_model(model)

I get the following output:
enter image description here

However, the expected output should be something like this:

enter image description here

Can somebody help with this code? I am using Keras with Tensorflow 2.0.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP