The Python Oracle

How do you use TensorFlow Graphkeys to get all weights?

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Unforgiving Himalayas Looping

--

Chapters
00:00 How Do You Use Tensorflow Graphkeys To Get All Weights?
01:00 Answer 1 Score 3
01:15 Accepted Answer Score 6
01:42 Thank you

--

Full question
https://stackoverflow.com/questions/4525...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #machinelearning #tensorflow

#avk47



ACCEPTED ANSWER

Score 6


By default all variables are bounded to the tf.GraphKeys.GLOBAL_VARIABLES collection. The convenient method is to set each weight to the collection tf.GraphKeys.WEIGHTS like this:

In [2]: w = tf.Variable([1,2,3], collections=[tf.GraphKeys.WEIGHTS], dtype=tf.float32)
In [3]: w2 = tf.Variable([11,22,32], collections=[tf.GraphKeys.WEIGHTS], dtype=tf.float32)

Then you can fetch them by:

tf.get_collection_ref(tf.GraphKeys.WEIGHTS)

And this is the weights:

[<tf.Variable 'Variable:0' shape=(3,) dtype=float32_ref>,
 <tf.Variable 'Variable_1:0' shape=(3,) dtype=float32_ref>]



ANSWER 2

Score 3


From the docs:

The following standard keys are defined, but their collections are not automatically populated as many of the others are:

  • WEIGHTS
  • BIASES
  • ACTIVATIONS