Keras: how to reuse variable when use keras with tensorflow

Created on 22 Jul 2018  路  2Comments  路  Source: keras-team/keras

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?

Most helpful comment

What if one wants to share variables between two different layers?

All 2 comments

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?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

anjishnu picture anjishnu  路  3Comments

braingineer picture braingineer  路  3Comments

Imorton-zd picture Imorton-zd  路  3Comments

kylemcdonald picture kylemcdonald  路  3Comments

oweingrod picture oweingrod  路  3Comments