I want to share variable between 2 layers, If I implement with pure tensorflow. I can do this:
with tf.variable_scope("model", reuse=True):
h = tf.variable(xxx, name=yyy)
put these 2 layer under same variable scope with same name and set reuse=True. But if I want to use layers from keras, for example:
keras.layers.Dense
It doesn't work. in the graph, tensorflow will create 2 sets of variables.
How Can implement this with keras?
Hello @scotthuang1989, you can reuse variables in Keras by creating one instance of a layer and call it multiple times.
shared_dense = Dense(2)
out_1 = shared_dense(inp_1)
out_2 = shared_dense(inp_2)
What if one wants to share variables between two different layers?
Most helpful comment
What if one wants to share variables between two different layers?