我正在尝试在Tensorflow 2.0中重新实现Multi-View CNN (MVCNN)。然而,据我所见,keras层没有像tf.layers中那样的reuse=True|False选项。是否有任何方法可以使用新API定义共享参数的层?还是我需要以TFv1的方式构建我的模型?
非常感谢!
非常感谢!
tf.keras.Model
对象。tf.GradientTape
,你可以像下面的例子一样轻松训练共享变量的模型。
# This is your model definition
model = tf.keras.Sequential(...)
#input_1,2 are model different inputs
with tf.GradientTape() as tape:
a = model(input_1)
b = model(input_2)
# you can just copute the loss
loss = a + b
# Use the tape to compute the gradients of the loss
# w.r.t. the model trainable variables
grads = tape.gradient(loss, model.trainable_varibles)
# Opt in an optimizer object, like tf.optimizers.Adam
# and you can use it to apply the update rule, following the gradient direction
opt.apply_gradients(zip(grads, model.trainable_variables))