'How to reuse variables in tensorflow 2.0?
When using tensorflow 2.0, I find something weird about tf.Variable? There are two cases bellow.
The first one
x1 = tf.Variable(12., name='x')
x2 = tf.Variable(12., name='x')
print(x1 is x2)
x1.assign(1.)
print(x1)
print(x2)
The output is
False
<tf.Variable 'x:0' shape=() dtype=float32, numpy=1.0>
<tf.Variable 'x:0' shape=() dtype=float32, numpy=12.0>
which means variables with the same name don't share the same memory.
The second one
x = tf.Variable(12., name='x')
print(x)
y = x.assign(5.)
print(y)
print(x is y)
x.assign(3.)
print(x)
print(y)
The output is
<tf.Variable 'x:0' shape=() dtype=float32, numpy=12.0>
<tf.Variable 'UnreadVariable' shape=() dtype=float32, numpy=5.0>
False
<tf.Variable 'x:0' shape=() dtype=float32, numpy=3.0>
<tf.Variable 'UnreadVariable' shape=() dtype=float32, numpy=3.0>
The result is unexpected, variables x and y with different names share the same memory, but id(x) is not equal to id(y).
Therefore, the name of variable can't distinguish whether variables are identical(share the same memory). And how can I reuse variables in tensorflow 2.0, like with tf.variable_scope("scope", reuse=True) tf.get_variable(...) in tensorflow 1.0?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
