Ink
Ink

Reputation: 953

How to reuse model framework code in TensorFlow?

There are some common layers with same architecture and I want to train them separately. My codes showed blew and I am worrying that my codes exactly construct and train one model but not two. In fact I want to train the two common layers separately and get two common layers with different weights.

Is my codes right? Or how should I do it?

def common():
    # some layers
    pass

out1 = common(input)
out2 = common(input)

loss1
loss2
...

Upvotes: 0

Views: 238

Answers (1)

iga
iga

Reputation: 3633

The short answer is: If you don't specifically ask tensorflow to reuse variables (e.g. with tf.variable_scope(scope, reuse=True)), it will create new ones every time.

Here is extensive documentation on this topic including a "Sharing variables" section.

Reply to Comment below: There is a stack of variable scopes. The top one (most recent in your back trace) will prevail. Interfaces that don't let you pass a reuse option explicitly look at in this top variable scope. Interfaces that do, effectively create a variable_scope for the duration of their execution.

I top variable scope that is created for you will fail if you attempt to create two variables with the same name.

a = tf.get_variable("v", [1])
b = tf.get_variable("v", [1])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<...> 
ValueError: Variable v already exists, disallowed.
            Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? ...

Upvotes: 1

Related Questions