How to use tf.case to control flow in tensorflow graph to perform differently during training and testing












0















I am trying to control the graph flow using a tf.cond() and tf.case().



I have several networks which yield an output of the same size (networks).



I have an additional network that outputs which of the networks above to use (networks_prob).



During training, I want to stack all of the networks results. During testing, I want to construct a tensor which is composed of the results in networks in the indices where networks_prob is maximal. (So I can avoid evaluating all of the networks and just evaluate the one with the highest probability)



Here is a simple example that I came up with but it doesn't work and I don't understand why.



import tensorflow as tf

networks = tf.constant([[[1], [2], [3]], [[4], [5], [6]]])
networks_prob = tf.constant([[0.2, 0.3, 0.4], [0.8, 0.1, 0.0]])
is_training = tf.placeholder(tf.bool, shape=())

network_idx = tf.argmax(networks_prob)
case_dict = {tf.equal(network_idx, i): lambda: networks[i] for i in range(networks.shape[1])}
output = tf.cond(is_training, lambda: tf.stack(networks), lambda: tf.case(case_dict, default=lambda: 1))

with tf.Session() as sess:
output_val = sess.run(output, feed_dict={is_training: False})

print(output_val)


I get Shape must be rank 0 but is rank 1 for 'cond/case/If_0/Switch' (op: 'Switch') with input shapes: [3], [3]. error.










share|improve this question



























    0















    I am trying to control the graph flow using a tf.cond() and tf.case().



    I have several networks which yield an output of the same size (networks).



    I have an additional network that outputs which of the networks above to use (networks_prob).



    During training, I want to stack all of the networks results. During testing, I want to construct a tensor which is composed of the results in networks in the indices where networks_prob is maximal. (So I can avoid evaluating all of the networks and just evaluate the one with the highest probability)



    Here is a simple example that I came up with but it doesn't work and I don't understand why.



    import tensorflow as tf

    networks = tf.constant([[[1], [2], [3]], [[4], [5], [6]]])
    networks_prob = tf.constant([[0.2, 0.3, 0.4], [0.8, 0.1, 0.0]])
    is_training = tf.placeholder(tf.bool, shape=())

    network_idx = tf.argmax(networks_prob)
    case_dict = {tf.equal(network_idx, i): lambda: networks[i] for i in range(networks.shape[1])}
    output = tf.cond(is_training, lambda: tf.stack(networks), lambda: tf.case(case_dict, default=lambda: 1))

    with tf.Session() as sess:
    output_val = sess.run(output, feed_dict={is_training: False})

    print(output_val)


    I get Shape must be rank 0 but is rank 1 for 'cond/case/If_0/Switch' (op: 'Switch') with input shapes: [3], [3]. error.










    share|improve this question

























      0












      0








      0








      I am trying to control the graph flow using a tf.cond() and tf.case().



      I have several networks which yield an output of the same size (networks).



      I have an additional network that outputs which of the networks above to use (networks_prob).



      During training, I want to stack all of the networks results. During testing, I want to construct a tensor which is composed of the results in networks in the indices where networks_prob is maximal. (So I can avoid evaluating all of the networks and just evaluate the one with the highest probability)



      Here is a simple example that I came up with but it doesn't work and I don't understand why.



      import tensorflow as tf

      networks = tf.constant([[[1], [2], [3]], [[4], [5], [6]]])
      networks_prob = tf.constant([[0.2, 0.3, 0.4], [0.8, 0.1, 0.0]])
      is_training = tf.placeholder(tf.bool, shape=())

      network_idx = tf.argmax(networks_prob)
      case_dict = {tf.equal(network_idx, i): lambda: networks[i] for i in range(networks.shape[1])}
      output = tf.cond(is_training, lambda: tf.stack(networks), lambda: tf.case(case_dict, default=lambda: 1))

      with tf.Session() as sess:
      output_val = sess.run(output, feed_dict={is_training: False})

      print(output_val)


      I get Shape must be rank 0 but is rank 1 for 'cond/case/If_0/Switch' (op: 'Switch') with input shapes: [3], [3]. error.










      share|improve this question














      I am trying to control the graph flow using a tf.cond() and tf.case().



      I have several networks which yield an output of the same size (networks).



      I have an additional network that outputs which of the networks above to use (networks_prob).



      During training, I want to stack all of the networks results. During testing, I want to construct a tensor which is composed of the results in networks in the indices where networks_prob is maximal. (So I can avoid evaluating all of the networks and just evaluate the one with the highest probability)



      Here is a simple example that I came up with but it doesn't work and I don't understand why.



      import tensorflow as tf

      networks = tf.constant([[[1], [2], [3]], [[4], [5], [6]]])
      networks_prob = tf.constant([[0.2, 0.3, 0.4], [0.8, 0.1, 0.0]])
      is_training = tf.placeholder(tf.bool, shape=())

      network_idx = tf.argmax(networks_prob)
      case_dict = {tf.equal(network_idx, i): lambda: networks[i] for i in range(networks.shape[1])}
      output = tf.cond(is_training, lambda: tf.stack(networks), lambda: tf.case(case_dict, default=lambda: 1))

      with tf.Session() as sess:
      output_val = sess.run(output, feed_dict={is_training: False})

      print(output_val)


      I get Shape must be rank 0 but is rank 1 for 'cond/case/If_0/Switch' (op: 'Switch') with input shapes: [3], [3]. error.







      python tensorflow






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 20:32









      itzik Ben Shabatitzik Ben Shabat

      622616




      622616
























          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%2f53327492%2fhow-to-use-tf-case-to-control-flow-in-tensorflow-graph-to-perform-differently-du%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%2f53327492%2fhow-to-use-tf-case-to-control-flow-in-tensorflow-graph-to-perform-differently-du%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

          Florida Star v. B. J. F.

          Danny Elfman

          Lugert, Oklahoma