I want to create a network with two parallel layers (same input is given to two different layers and output of them is combined with some mathematical operations). Having said that, I am not sure the back-propagation will be done by Keras automatically. As a simple example of custom RNN
cell,
class Example(keras.layers.Layer):
def __init__(self, units, **kwargs):
super(Example, self).__init__(**kwargs)
self.units = units
self.state_size = units
self.la = keras.layers.Dense(self.units)
self.lb = keras.layers.Dense(self.units)
def call(self, inputs, states):
prev_output = states[0]
# parallel layers
a = tf.sigmoid(self.la(inputs))
b = tf.sigmoid(self.lb(inputs))
# combined using mathematical operation
output = (-1 * prev_output * a) + (prev_output * b)
return output, [output]
Now, the loss gradient to `la` and `lb` layers are different (gradient of loss wrt `a`, should be `-output` but wrt `b` should be `output`), will this be taken care by Keras automatically or should we create custom gradient functions?
Any insights and suggestions are much appreciated :)