Tensorflow 2.0 如何在卷积层之间共享参数?

4
我正在尝试在Tensorflow 2.0中重新实现Multi-View CNN (MVCNN)。然而,据我所见,keras层没有像tf.layers中那样的reuse=True|False选项。是否有任何方法可以使用新API定义共享参数的层?还是我需要以TFv1的方式构建我的模型?
非常感谢!
1个回答

4
为了共享模型参数,你只需使用同一模型即可。这是在TensorFlow 2.0中引入的新范式。在TF 1.x中,我们使用面向图的方法来重用相同的图形以共享变量,但现在我们可以仅重用不同输入的同一tf.keras.Model对象。
这个对象携带着它自己的变量。
使用Keras模型和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))


谢谢@nessuno!所以,例如,在多视图模型的情况下,我想重用模型的前半部分从多个输入中提取特征,后半部分用于组合它们的连接特征。我需要将模型分成两个吗? - Tai Christian
你需要使用Keras模型定义2个输出 :) 可以查看一下函数式Keras API https://www.tensorflow.org/beta/guide/keras/functional - nessuno

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接