Valjean
Valjean

Reputation: 13

Dense.get_weights() vs Dense.weights

Why get_weights() returns different values for weights compared to actual weights? I think after initialization both methods should show the same weights.

import tensorflow as tf
import os

sess = tf.Session()
x = tf.placeholder(tf.float32, shape=[None, 3])

linear_model = tf.layers.Dense(units=1,use_bias=False,activation=None)
y = linear_model(x)

init = tf.global_variables_initializer()
sess.run(init)

print(linear_model.get_weights())
print(sess.run(linear_model.weights))
print('------------------')
print(sess.run(y, {x: [[1, 1, 1]]}))

Output

[array([[-0.26290017],
    [ 0.11782396],
    [ 0.51118207]], dtype=float32)]
[array([[-0.12011003],
    [ 0.13160932],
    [ 1.1303514 ]], dtype=float32)]
------------------
[[1.1418507]]

Upvotes: 1

Views: 273

Answers (1)

Tyler
Tyler

Reputation: 28874

In your code there are actually two tf.Session() instances; the fix is to enclose the use of your sess in a with clause like this:

# Define your graph.

with tf.Session() as sess:
    # All calls to tf.run() or linear_model.get_weights() go in this clause.

Why are there two sessions?

The first is your own sess object, which is not very mysterious.

The second is implicitly created by your call to get_weights(), which will create a new session instance for you if TensorFlow's default session is not set. Because you're using sess outside a with clause, you haven't set the default session, and get_weights() silently creates a new session for you. When you set up a tf.Session() in a with clause, it does set the default session in tf, and get_weights() will silently (and more helpfully) re-use your session object.

In case you're super-curious, the actual function that sneakily creates the other session for you is (in keras within tensorflow) keras.backend.get_session().

Upvotes: 1

Related Questions