How can I access the dynamic variable from tensorflow graph?










0















I know there are related questions out there, and I have absolutely looked at them... but for some reason it's not making sense to me, so I need to ask for some help here, as I have spent too long on this. I've written a simple code example. Quite simply I have a placeholder value, which I want to pass to a function which creates some tensorflow ops... then later I want to call another function which is able to access the values from those ops, without passing around actual variables. My goal was to access these variables via custom collection names. My example defines variables via "z1" = tf.Variable and "z2" = tf.get_variable, as well as assigning the calculation directly to "y". I'm obviously missing something very basic, as the only value which I see dynamically changing is in "y", where I have directly assigned the value.



import tensorflow as tf
import numpy as np

x = tf.placeholder(tf.float32, shape=(None, )+ (12,))
z1 = tf.Variable(0.0, dtype=tf.float32,
validate_shape=False,
trainable=False,
collections = [
tf.GraphKeys.GLOBAL_VARIABLES,
"scale"
])
z2 = tf.get_variable("z2", , tf.float32,
tf.initializers.zeros(),
collections = [
tf.GraphKeys.GLOBAL_VARIABLES,
"scale"
],
trainable=False)

def build_graph(x):
z1_op = tf.assign(z1, tf.reduce_mean(x))
z2_op = tf.assign(z2, tf.reduce_mean(x))
y = tf.reduce_mean(x)
return y

def rescale():
q = tf.get_collection("scale")
return q

def global_vars():
q = tf.global_variables()
return q

y = build_graph(x)
q1 = rescale()
q2 = global_vars()

data = np.random.rand(500, 12)

init_op = tf.global_variables_initializer()
with tf.Session() as sess:

sess.run(init_op)
for n in range(5):
batch_idx = np.random.choice(list(range(500)), np.random.randint(1, 400, 1))
temp_data = data[batch_idx,:]
output = sess.run([y, q1, q2], feed_dict=x : temp_data )
print(output)


The output of this is:



[0.5078107, [0.0, 0.0], [0.0, 0.0]]
[0.50185573, [0.0, 0.0], [0.0, 0.0]]
[0.4966678, [0.0, 0.0], [0.0, 0.0]]
[0.50407946, [0.0, 0.0], [0.0, 0.0]]
[0.49476147, [0.0, 0.0], [0.0, 0.0]]


I would appreciate any guidance provided....










share|improve this question


























    0















    I know there are related questions out there, and I have absolutely looked at them... but for some reason it's not making sense to me, so I need to ask for some help here, as I have spent too long on this. I've written a simple code example. Quite simply I have a placeholder value, which I want to pass to a function which creates some tensorflow ops... then later I want to call another function which is able to access the values from those ops, without passing around actual variables. My goal was to access these variables via custom collection names. My example defines variables via "z1" = tf.Variable and "z2" = tf.get_variable, as well as assigning the calculation directly to "y". I'm obviously missing something very basic, as the only value which I see dynamically changing is in "y", where I have directly assigned the value.



    import tensorflow as tf
    import numpy as np

    x = tf.placeholder(tf.float32, shape=(None, )+ (12,))
    z1 = tf.Variable(0.0, dtype=tf.float32,
    validate_shape=False,
    trainable=False,
    collections = [
    tf.GraphKeys.GLOBAL_VARIABLES,
    "scale"
    ])
    z2 = tf.get_variable("z2", , tf.float32,
    tf.initializers.zeros(),
    collections = [
    tf.GraphKeys.GLOBAL_VARIABLES,
    "scale"
    ],
    trainable=False)

    def build_graph(x):
    z1_op = tf.assign(z1, tf.reduce_mean(x))
    z2_op = tf.assign(z2, tf.reduce_mean(x))
    y = tf.reduce_mean(x)
    return y

    def rescale():
    q = tf.get_collection("scale")
    return q

    def global_vars():
    q = tf.global_variables()
    return q

    y = build_graph(x)
    q1 = rescale()
    q2 = global_vars()

    data = np.random.rand(500, 12)

    init_op = tf.global_variables_initializer()
    with tf.Session() as sess:

    sess.run(init_op)
    for n in range(5):
    batch_idx = np.random.choice(list(range(500)), np.random.randint(1, 400, 1))
    temp_data = data[batch_idx,:]
    output = sess.run([y, q1, q2], feed_dict=x : temp_data )
    print(output)


    The output of this is:



    [0.5078107, [0.0, 0.0], [0.0, 0.0]]
    [0.50185573, [0.0, 0.0], [0.0, 0.0]]
    [0.4966678, [0.0, 0.0], [0.0, 0.0]]
    [0.50407946, [0.0, 0.0], [0.0, 0.0]]
    [0.49476147, [0.0, 0.0], [0.0, 0.0]]


    I would appreciate any guidance provided....










    share|improve this question
























      0












      0








      0








      I know there are related questions out there, and I have absolutely looked at them... but for some reason it's not making sense to me, so I need to ask for some help here, as I have spent too long on this. I've written a simple code example. Quite simply I have a placeholder value, which I want to pass to a function which creates some tensorflow ops... then later I want to call another function which is able to access the values from those ops, without passing around actual variables. My goal was to access these variables via custom collection names. My example defines variables via "z1" = tf.Variable and "z2" = tf.get_variable, as well as assigning the calculation directly to "y". I'm obviously missing something very basic, as the only value which I see dynamically changing is in "y", where I have directly assigned the value.



      import tensorflow as tf
      import numpy as np

      x = tf.placeholder(tf.float32, shape=(None, )+ (12,))
      z1 = tf.Variable(0.0, dtype=tf.float32,
      validate_shape=False,
      trainable=False,
      collections = [
      tf.GraphKeys.GLOBAL_VARIABLES,
      "scale"
      ])
      z2 = tf.get_variable("z2", , tf.float32,
      tf.initializers.zeros(),
      collections = [
      tf.GraphKeys.GLOBAL_VARIABLES,
      "scale"
      ],
      trainable=False)

      def build_graph(x):
      z1_op = tf.assign(z1, tf.reduce_mean(x))
      z2_op = tf.assign(z2, tf.reduce_mean(x))
      y = tf.reduce_mean(x)
      return y

      def rescale():
      q = tf.get_collection("scale")
      return q

      def global_vars():
      q = tf.global_variables()
      return q

      y = build_graph(x)
      q1 = rescale()
      q2 = global_vars()

      data = np.random.rand(500, 12)

      init_op = tf.global_variables_initializer()
      with tf.Session() as sess:

      sess.run(init_op)
      for n in range(5):
      batch_idx = np.random.choice(list(range(500)), np.random.randint(1, 400, 1))
      temp_data = data[batch_idx,:]
      output = sess.run([y, q1, q2], feed_dict=x : temp_data )
      print(output)


      The output of this is:



      [0.5078107, [0.0, 0.0], [0.0, 0.0]]
      [0.50185573, [0.0, 0.0], [0.0, 0.0]]
      [0.4966678, [0.0, 0.0], [0.0, 0.0]]
      [0.50407946, [0.0, 0.0], [0.0, 0.0]]
      [0.49476147, [0.0, 0.0], [0.0, 0.0]]


      I would appreciate any guidance provided....










      share|improve this question














      I know there are related questions out there, and I have absolutely looked at them... but for some reason it's not making sense to me, so I need to ask for some help here, as I have spent too long on this. I've written a simple code example. Quite simply I have a placeholder value, which I want to pass to a function which creates some tensorflow ops... then later I want to call another function which is able to access the values from those ops, without passing around actual variables. My goal was to access these variables via custom collection names. My example defines variables via "z1" = tf.Variable and "z2" = tf.get_variable, as well as assigning the calculation directly to "y". I'm obviously missing something very basic, as the only value which I see dynamically changing is in "y", where I have directly assigned the value.



      import tensorflow as tf
      import numpy as np

      x = tf.placeholder(tf.float32, shape=(None, )+ (12,))
      z1 = tf.Variable(0.0, dtype=tf.float32,
      validate_shape=False,
      trainable=False,
      collections = [
      tf.GraphKeys.GLOBAL_VARIABLES,
      "scale"
      ])
      z2 = tf.get_variable("z2", , tf.float32,
      tf.initializers.zeros(),
      collections = [
      tf.GraphKeys.GLOBAL_VARIABLES,
      "scale"
      ],
      trainable=False)

      def build_graph(x):
      z1_op = tf.assign(z1, tf.reduce_mean(x))
      z2_op = tf.assign(z2, tf.reduce_mean(x))
      y = tf.reduce_mean(x)
      return y

      def rescale():
      q = tf.get_collection("scale")
      return q

      def global_vars():
      q = tf.global_variables()
      return q

      y = build_graph(x)
      q1 = rescale()
      q2 = global_vars()

      data = np.random.rand(500, 12)

      init_op = tf.global_variables_initializer()
      with tf.Session() as sess:

      sess.run(init_op)
      for n in range(5):
      batch_idx = np.random.choice(list(range(500)), np.random.randint(1, 400, 1))
      temp_data = data[batch_idx,:]
      output = sess.run([y, q1, q2], feed_dict=x : temp_data )
      print(output)


      The output of this is:



      [0.5078107, [0.0, 0.0], [0.0, 0.0]]
      [0.50185573, [0.0, 0.0], [0.0, 0.0]]
      [0.4966678, [0.0, 0.0], [0.0, 0.0]]
      [0.50407946, [0.0, 0.0], [0.0, 0.0]]
      [0.49476147, [0.0, 0.0], [0.0, 0.0]]


      I would appreciate any guidance provided....







      python tensorflow






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 16:03









      waldrojewaldroje

      12




      12






















          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284946%2fhow-can-i-access-the-dynamic-variable-from-tensorflow-graph%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284946%2fhow-can-i-access-the-dynamic-variable-from-tensorflow-graph%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo