tensorflow placeholder shape [None] [None,1] difference












0















I am new to tensorflow. I experimented a DQN algorithm with a section involving



a = tf.placeholder(tf.int32, shape = [None],name='A')
q = tf.reduce_sum(critic_q * tf.one_hot(a,n_outputs),axis=1,keepdims=True,name='Q')#Q value for chosen action

y = tf.placeholder(tf.float32, shape = [None],name='Y')

learning_rate = 1e-4
cost = tf.reduce_mean(tf.square(y-q))#mean squared error
global_step = tf.Variable(0,trainable=False,name='global_step')
optimizer = tf.train.AdamOptimizer(learning_rate)
training_op = optimizer.minimize(cost,global_step=global_step)


and initialized the input y with y_batch=np.zeros(nbatch). The network hardly trained at all.



Then, I switched to defining y as



y = tf.placeholder(tf.float32, shape = [None,1],name='Y')


and initialized the input with y_batch=np.zeros(nbatch).reshape(-1,1), which worked nicely.



What was happening in the first implementation?










share|improve this question



























    0















    I am new to tensorflow. I experimented a DQN algorithm with a section involving



    a = tf.placeholder(tf.int32, shape = [None],name='A')
    q = tf.reduce_sum(critic_q * tf.one_hot(a,n_outputs),axis=1,keepdims=True,name='Q')#Q value for chosen action

    y = tf.placeholder(tf.float32, shape = [None],name='Y')

    learning_rate = 1e-4
    cost = tf.reduce_mean(tf.square(y-q))#mean squared error
    global_step = tf.Variable(0,trainable=False,name='global_step')
    optimizer = tf.train.AdamOptimizer(learning_rate)
    training_op = optimizer.minimize(cost,global_step=global_step)


    and initialized the input y with y_batch=np.zeros(nbatch). The network hardly trained at all.



    Then, I switched to defining y as



    y = tf.placeholder(tf.float32, shape = [None,1],name='Y')


    and initialized the input with y_batch=np.zeros(nbatch).reshape(-1,1), which worked nicely.



    What was happening in the first implementation?










    share|improve this question

























      0












      0








      0








      I am new to tensorflow. I experimented a DQN algorithm with a section involving



      a = tf.placeholder(tf.int32, shape = [None],name='A')
      q = tf.reduce_sum(critic_q * tf.one_hot(a,n_outputs),axis=1,keepdims=True,name='Q')#Q value for chosen action

      y = tf.placeholder(tf.float32, shape = [None],name='Y')

      learning_rate = 1e-4
      cost = tf.reduce_mean(tf.square(y-q))#mean squared error
      global_step = tf.Variable(0,trainable=False,name='global_step')
      optimizer = tf.train.AdamOptimizer(learning_rate)
      training_op = optimizer.minimize(cost,global_step=global_step)


      and initialized the input y with y_batch=np.zeros(nbatch). The network hardly trained at all.



      Then, I switched to defining y as



      y = tf.placeholder(tf.float32, shape = [None,1],name='Y')


      and initialized the input with y_batch=np.zeros(nbatch).reshape(-1,1), which worked nicely.



      What was happening in the first implementation?










      share|improve this question














      I am new to tensorflow. I experimented a DQN algorithm with a section involving



      a = tf.placeholder(tf.int32, shape = [None],name='A')
      q = tf.reduce_sum(critic_q * tf.one_hot(a,n_outputs),axis=1,keepdims=True,name='Q')#Q value for chosen action

      y = tf.placeholder(tf.float32, shape = [None],name='Y')

      learning_rate = 1e-4
      cost = tf.reduce_mean(tf.square(y-q))#mean squared error
      global_step = tf.Variable(0,trainable=False,name='global_step')
      optimizer = tf.train.AdamOptimizer(learning_rate)
      training_op = optimizer.minimize(cost,global_step=global_step)


      and initialized the input y with y_batch=np.zeros(nbatch). The network hardly trained at all.



      Then, I switched to defining y as



      y = tf.placeholder(tf.float32, shape = [None,1],name='Y')


      and initialized the input with y_batch=np.zeros(nbatch).reshape(-1,1), which worked nicely.



      What was happening in the first implementation?







      python tensorflow






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 1 at 6:57









      user15988user15988

      32




      32
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Every tensor has a rank (number of dimensions) and a set of dimensions.



          A placeholder with shape [1] is a placeholder with rank 1 and the dimension in position 0 of 1.



          A placeholder with shape [None, 1] is a placeholder with rank 2, hence it has 2 dimensions. The first dimension (index 0) has unknown size (it will be resolved at runtime) while the second dimension (index 1) has the known size of 1.



          In order to be compatible, tensors must have the same rank a dimensions.



          You can read a more complete assessment about the tensors shape here: https://pgaleone.eu/tensorflow/2018/07/28/understanding-tensorflow-tensors-shape-static-dynamic/#tensors-the-basic






          share|improve this answer
























          • I suppose you are implying that q has a dimension [None,1] as well. What happens when you make an operation of y-q, when y has dimension [None]?

            – user15988
            Jan 1 at 18:40











          • Tensorflow operations use the broadcasting semantics, that's well explained here: docs.scipy.org/doc/numpy-1.13.0/user/basics.broadcasting.html (it uses the same semantics of numpy). Hence, if there's a 1 dimension, the data of one operand is duplicated and the operation is repeated

            – nessuno
            Jan 1 at 19:51











          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%2f53993596%2ftensorflow-placeholder-shape-none-none-1-difference%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Every tensor has a rank (number of dimensions) and a set of dimensions.



          A placeholder with shape [1] is a placeholder with rank 1 and the dimension in position 0 of 1.



          A placeholder with shape [None, 1] is a placeholder with rank 2, hence it has 2 dimensions. The first dimension (index 0) has unknown size (it will be resolved at runtime) while the second dimension (index 1) has the known size of 1.



          In order to be compatible, tensors must have the same rank a dimensions.



          You can read a more complete assessment about the tensors shape here: https://pgaleone.eu/tensorflow/2018/07/28/understanding-tensorflow-tensors-shape-static-dynamic/#tensors-the-basic






          share|improve this answer
























          • I suppose you are implying that q has a dimension [None,1] as well. What happens when you make an operation of y-q, when y has dimension [None]?

            – user15988
            Jan 1 at 18:40











          • Tensorflow operations use the broadcasting semantics, that's well explained here: docs.scipy.org/doc/numpy-1.13.0/user/basics.broadcasting.html (it uses the same semantics of numpy). Hence, if there's a 1 dimension, the data of one operand is duplicated and the operation is repeated

            – nessuno
            Jan 1 at 19:51
















          1














          Every tensor has a rank (number of dimensions) and a set of dimensions.



          A placeholder with shape [1] is a placeholder with rank 1 and the dimension in position 0 of 1.



          A placeholder with shape [None, 1] is a placeholder with rank 2, hence it has 2 dimensions. The first dimension (index 0) has unknown size (it will be resolved at runtime) while the second dimension (index 1) has the known size of 1.



          In order to be compatible, tensors must have the same rank a dimensions.



          You can read a more complete assessment about the tensors shape here: https://pgaleone.eu/tensorflow/2018/07/28/understanding-tensorflow-tensors-shape-static-dynamic/#tensors-the-basic






          share|improve this answer
























          • I suppose you are implying that q has a dimension [None,1] as well. What happens when you make an operation of y-q, when y has dimension [None]?

            – user15988
            Jan 1 at 18:40











          • Tensorflow operations use the broadcasting semantics, that's well explained here: docs.scipy.org/doc/numpy-1.13.0/user/basics.broadcasting.html (it uses the same semantics of numpy). Hence, if there's a 1 dimension, the data of one operand is duplicated and the operation is repeated

            – nessuno
            Jan 1 at 19:51














          1












          1








          1







          Every tensor has a rank (number of dimensions) and a set of dimensions.



          A placeholder with shape [1] is a placeholder with rank 1 and the dimension in position 0 of 1.



          A placeholder with shape [None, 1] is a placeholder with rank 2, hence it has 2 dimensions. The first dimension (index 0) has unknown size (it will be resolved at runtime) while the second dimension (index 1) has the known size of 1.



          In order to be compatible, tensors must have the same rank a dimensions.



          You can read a more complete assessment about the tensors shape here: https://pgaleone.eu/tensorflow/2018/07/28/understanding-tensorflow-tensors-shape-static-dynamic/#tensors-the-basic






          share|improve this answer













          Every tensor has a rank (number of dimensions) and a set of dimensions.



          A placeholder with shape [1] is a placeholder with rank 1 and the dimension in position 0 of 1.



          A placeholder with shape [None, 1] is a placeholder with rank 2, hence it has 2 dimensions. The first dimension (index 0) has unknown size (it will be resolved at runtime) while the second dimension (index 1) has the known size of 1.



          In order to be compatible, tensors must have the same rank a dimensions.



          You can read a more complete assessment about the tensors shape here: https://pgaleone.eu/tensorflow/2018/07/28/understanding-tensorflow-tensors-shape-static-dynamic/#tensors-the-basic







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 1 at 13:01









          nessunonessuno

          14.7k34348




          14.7k34348













          • I suppose you are implying that q has a dimension [None,1] as well. What happens when you make an operation of y-q, when y has dimension [None]?

            – user15988
            Jan 1 at 18:40











          • Tensorflow operations use the broadcasting semantics, that's well explained here: docs.scipy.org/doc/numpy-1.13.0/user/basics.broadcasting.html (it uses the same semantics of numpy). Hence, if there's a 1 dimension, the data of one operand is duplicated and the operation is repeated

            – nessuno
            Jan 1 at 19:51



















          • I suppose you are implying that q has a dimension [None,1] as well. What happens when you make an operation of y-q, when y has dimension [None]?

            – user15988
            Jan 1 at 18:40











          • Tensorflow operations use the broadcasting semantics, that's well explained here: docs.scipy.org/doc/numpy-1.13.0/user/basics.broadcasting.html (it uses the same semantics of numpy). Hence, if there's a 1 dimension, the data of one operand is duplicated and the operation is repeated

            – nessuno
            Jan 1 at 19:51

















          I suppose you are implying that q has a dimension [None,1] as well. What happens when you make an operation of y-q, when y has dimension [None]?

          – user15988
          Jan 1 at 18:40





          I suppose you are implying that q has a dimension [None,1] as well. What happens when you make an operation of y-q, when y has dimension [None]?

          – user15988
          Jan 1 at 18:40













          Tensorflow operations use the broadcasting semantics, that's well explained here: docs.scipy.org/doc/numpy-1.13.0/user/basics.broadcasting.html (it uses the same semantics of numpy). Hence, if there's a 1 dimension, the data of one operand is duplicated and the operation is repeated

          – nessuno
          Jan 1 at 19:51





          Tensorflow operations use the broadcasting semantics, that's well explained here: docs.scipy.org/doc/numpy-1.13.0/user/basics.broadcasting.html (it uses the same semantics of numpy). Hence, if there's a 1 dimension, the data of one operand is duplicated and the operation is repeated

          – nessuno
          Jan 1 at 19:51




















          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%2f53993596%2ftensorflow-placeholder-shape-none-none-1-difference%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

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith