How do I add the None Dimension back to a Tensor?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I've made some transformation in a Lambda Layer and now I have shape (1,), how do I get back to (None, 1)?



Here are my operations



def function_lambda(x):
import keras.backend

aux_array = keras.backend.sign(x)
#the shape is (?, 11) here OK

aux_array = keras.backend.relu(aux_array)
#the shape is (?, 11) here still OK

aux_array = keras.backend.any(aux_array)
#the shape is () here not OK

aux_array = keras.backend.reshape(aux_array, [-1])
#now the shape is (1,) almost OK

return aux_array


How do I reshape and put back the None Batch Dimension?










share|improve this question























  • did you figure it out?

    – enjal
    Apr 1 at 21:43




















0















I've made some transformation in a Lambda Layer and now I have shape (1,), how do I get back to (None, 1)?



Here are my operations



def function_lambda(x):
import keras.backend

aux_array = keras.backend.sign(x)
#the shape is (?, 11) here OK

aux_array = keras.backend.relu(aux_array)
#the shape is (?, 11) here still OK

aux_array = keras.backend.any(aux_array)
#the shape is () here not OK

aux_array = keras.backend.reshape(aux_array, [-1])
#now the shape is (1,) almost OK

return aux_array


How do I reshape and put back the None Batch Dimension?










share|improve this question























  • did you figure it out?

    – enjal
    Apr 1 at 21:43
















0












0








0








I've made some transformation in a Lambda Layer and now I have shape (1,), how do I get back to (None, 1)?



Here are my operations



def function_lambda(x):
import keras.backend

aux_array = keras.backend.sign(x)
#the shape is (?, 11) here OK

aux_array = keras.backend.relu(aux_array)
#the shape is (?, 11) here still OK

aux_array = keras.backend.any(aux_array)
#the shape is () here not OK

aux_array = keras.backend.reshape(aux_array, [-1])
#now the shape is (1,) almost OK

return aux_array


How do I reshape and put back the None Batch Dimension?










share|improve this question














I've made some transformation in a Lambda Layer and now I have shape (1,), how do I get back to (None, 1)?



Here are my operations



def function_lambda(x):
import keras.backend

aux_array = keras.backend.sign(x)
#the shape is (?, 11) here OK

aux_array = keras.backend.relu(aux_array)
#the shape is (?, 11) here still OK

aux_array = keras.backend.any(aux_array)
#the shape is () here not OK

aux_array = keras.backend.reshape(aux_array, [-1])
#now the shape is (1,) almost OK

return aux_array


How do I reshape and put back the None Batch Dimension?







python tensorflow keras reshape






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 8:17









ViniciusVinicius

158




158













  • did you figure it out?

    – enjal
    Apr 1 at 21:43





















  • did you figure it out?

    – enjal
    Apr 1 at 21:43



















did you figure it out?

– enjal
Apr 1 at 21:43







did you figure it out?

– enjal
Apr 1 at 21:43














2 Answers
2






active

oldest

votes


















0














If you have a fully-defined shape (like (1,)) then you don't need None dimensions, because you know exactly how many elements your tensor has. But you can reshape to have two dimensions replacing you reshaping with:



aux_array = keras.backend.reshape(aux_array, [-1, 1])


This will give you an array with shape (1, 1), which is compatible with the shape (None, 1), so it should be okay. Note that -1 in reshape means "however many elements are necessary to accommodate the tensor in this shape", but in this case you know you only have one element, so it would be the same to use:



aux_array = keras.backend.reshape(aux_array, [1, 1])


Because, again, the shape is fully defined and you know what the size of each dimension should be exactly. However, using -1 is convenient because it works whether you know the shape fully or not, and Keras/TensorFlow will work out what the size should be (the dimension will have the necessary size if it can be computed or None if part of the shape is unknown).






share|improve this answer































    0














    The tf.reshape has a limitation that it cannot work with "None" dimension. So far as I know, the only place where we can define a "None" dimension is in tf.placeholder. The following should work:



    def function_lambda(x):
    import keras.backend

    aux_array = keras.backend.sign(x)
    #the shape is (?, 11) here OK

    aux_array = keras.backend.relu(aux_array)
    #the shape is (?, 11) here still OK

    aux_array = keras.backend.any(aux_array)
    #the shape is () here not OK

    aux_array = keras.backend.placeholder_with_default(aux_array, [None,1])
    #now the shape is (?,1) OK

    return aux_array


    Additional note: creating a new placeholder for reintroducing the "None" dimension is useful when trying to use Google Machine Learning Engine. MLE requires that the first dimension (batch) always remain "None" or unknown.






    share|improve this answer
























      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%2f53442887%2fhow-do-i-add-the-none-dimension-back-to-a-tensor%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      If you have a fully-defined shape (like (1,)) then you don't need None dimensions, because you know exactly how many elements your tensor has. But you can reshape to have two dimensions replacing you reshaping with:



      aux_array = keras.backend.reshape(aux_array, [-1, 1])


      This will give you an array with shape (1, 1), which is compatible with the shape (None, 1), so it should be okay. Note that -1 in reshape means "however many elements are necessary to accommodate the tensor in this shape", but in this case you know you only have one element, so it would be the same to use:



      aux_array = keras.backend.reshape(aux_array, [1, 1])


      Because, again, the shape is fully defined and you know what the size of each dimension should be exactly. However, using -1 is convenient because it works whether you know the shape fully or not, and Keras/TensorFlow will work out what the size should be (the dimension will have the necessary size if it can be computed or None if part of the shape is unknown).






      share|improve this answer




























        0














        If you have a fully-defined shape (like (1,)) then you don't need None dimensions, because you know exactly how many elements your tensor has. But you can reshape to have two dimensions replacing you reshaping with:



        aux_array = keras.backend.reshape(aux_array, [-1, 1])


        This will give you an array with shape (1, 1), which is compatible with the shape (None, 1), so it should be okay. Note that -1 in reshape means "however many elements are necessary to accommodate the tensor in this shape", but in this case you know you only have one element, so it would be the same to use:



        aux_array = keras.backend.reshape(aux_array, [1, 1])


        Because, again, the shape is fully defined and you know what the size of each dimension should be exactly. However, using -1 is convenient because it works whether you know the shape fully or not, and Keras/TensorFlow will work out what the size should be (the dimension will have the necessary size if it can be computed or None if part of the shape is unknown).






        share|improve this answer


























          0












          0








          0







          If you have a fully-defined shape (like (1,)) then you don't need None dimensions, because you know exactly how many elements your tensor has. But you can reshape to have two dimensions replacing you reshaping with:



          aux_array = keras.backend.reshape(aux_array, [-1, 1])


          This will give you an array with shape (1, 1), which is compatible with the shape (None, 1), so it should be okay. Note that -1 in reshape means "however many elements are necessary to accommodate the tensor in this shape", but in this case you know you only have one element, so it would be the same to use:



          aux_array = keras.backend.reshape(aux_array, [1, 1])


          Because, again, the shape is fully defined and you know what the size of each dimension should be exactly. However, using -1 is convenient because it works whether you know the shape fully or not, and Keras/TensorFlow will work out what the size should be (the dimension will have the necessary size if it can be computed or None if part of the shape is unknown).






          share|improve this answer













          If you have a fully-defined shape (like (1,)) then you don't need None dimensions, because you know exactly how many elements your tensor has. But you can reshape to have two dimensions replacing you reshaping with:



          aux_array = keras.backend.reshape(aux_array, [-1, 1])


          This will give you an array with shape (1, 1), which is compatible with the shape (None, 1), so it should be okay. Note that -1 in reshape means "however many elements are necessary to accommodate the tensor in this shape", but in this case you know you only have one element, so it would be the same to use:



          aux_array = keras.backend.reshape(aux_array, [1, 1])


          Because, again, the shape is fully defined and you know what the size of each dimension should be exactly. However, using -1 is convenient because it works whether you know the shape fully or not, and Keras/TensorFlow will work out what the size should be (the dimension will have the necessary size if it can be computed or None if part of the shape is unknown).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 12:08









          jdehesajdehesa

          27.8k43759




          27.8k43759

























              0














              The tf.reshape has a limitation that it cannot work with "None" dimension. So far as I know, the only place where we can define a "None" dimension is in tf.placeholder. The following should work:



              def function_lambda(x):
              import keras.backend

              aux_array = keras.backend.sign(x)
              #the shape is (?, 11) here OK

              aux_array = keras.backend.relu(aux_array)
              #the shape is (?, 11) here still OK

              aux_array = keras.backend.any(aux_array)
              #the shape is () here not OK

              aux_array = keras.backend.placeholder_with_default(aux_array, [None,1])
              #now the shape is (?,1) OK

              return aux_array


              Additional note: creating a new placeholder for reintroducing the "None" dimension is useful when trying to use Google Machine Learning Engine. MLE requires that the first dimension (batch) always remain "None" or unknown.






              share|improve this answer




























                0














                The tf.reshape has a limitation that it cannot work with "None" dimension. So far as I know, the only place where we can define a "None" dimension is in tf.placeholder. The following should work:



                def function_lambda(x):
                import keras.backend

                aux_array = keras.backend.sign(x)
                #the shape is (?, 11) here OK

                aux_array = keras.backend.relu(aux_array)
                #the shape is (?, 11) here still OK

                aux_array = keras.backend.any(aux_array)
                #the shape is () here not OK

                aux_array = keras.backend.placeholder_with_default(aux_array, [None,1])
                #now the shape is (?,1) OK

                return aux_array


                Additional note: creating a new placeholder for reintroducing the "None" dimension is useful when trying to use Google Machine Learning Engine. MLE requires that the first dimension (batch) always remain "None" or unknown.






                share|improve this answer


























                  0












                  0








                  0







                  The tf.reshape has a limitation that it cannot work with "None" dimension. So far as I know, the only place where we can define a "None" dimension is in tf.placeholder. The following should work:



                  def function_lambda(x):
                  import keras.backend

                  aux_array = keras.backend.sign(x)
                  #the shape is (?, 11) here OK

                  aux_array = keras.backend.relu(aux_array)
                  #the shape is (?, 11) here still OK

                  aux_array = keras.backend.any(aux_array)
                  #the shape is () here not OK

                  aux_array = keras.backend.placeholder_with_default(aux_array, [None,1])
                  #now the shape is (?,1) OK

                  return aux_array


                  Additional note: creating a new placeholder for reintroducing the "None" dimension is useful when trying to use Google Machine Learning Engine. MLE requires that the first dimension (batch) always remain "None" or unknown.






                  share|improve this answer













                  The tf.reshape has a limitation that it cannot work with "None" dimension. So far as I know, the only place where we can define a "None" dimension is in tf.placeholder. The following should work:



                  def function_lambda(x):
                  import keras.backend

                  aux_array = keras.backend.sign(x)
                  #the shape is (?, 11) here OK

                  aux_array = keras.backend.relu(aux_array)
                  #the shape is (?, 11) here still OK

                  aux_array = keras.backend.any(aux_array)
                  #the shape is () here not OK

                  aux_array = keras.backend.placeholder_with_default(aux_array, [None,1])
                  #now the shape is (?,1) OK

                  return aux_array


                  Additional note: creating a new placeholder for reintroducing the "None" dimension is useful when trying to use Google Machine Learning Engine. MLE requires that the first dimension (batch) always remain "None" or unknown.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 31 at 3:01









                  rahullakrahullak

                  12




                  12






























                      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%2f53442887%2fhow-do-i-add-the-none-dimension-back-to-a-tensor%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

                      How to change which sound is reproduced for terminal bell?

                      Can I use Tabulator js library in my java Spring + Thymeleaf project?

                      Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents