【发布时间】:2021-03-19 16:08:12
【问题描述】:
我想创建一个具有两个并行层的网络(将相同的输入提供给两个不同的层,并将它们的输出与一些数学运算相结合)。话虽如此,我不确定 Keras 会自动完成反向传播。作为自定义RNN单元格的简单示例,
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 :)
【问题讨论】:
标签: tensorflow keras keras-layer