Keras: input with size x*x generates unwanted output y*x












0















I have the following neural network in Keras:



inp = layers.Input((3,))
#Middle layers omitted
out_prop = layers.Dense(units=3, activation='softmax')(inp)
out_value = layers.Dense(units=1, activation = 'linear')(inp)


Then I prepared a pseudo-input to test my network:



inpu = np.array([[1,2,3],[4,5,6],[7,8,9]])


When I try to predict, this happens:



In [45]:nn.network.predict(inpu)
Out[45]:
[array([[0.257513 , 0.41672954, 0.32575747],
[0.20175152, 0.4763418 , 0.32190666],
[0.15986516, 0.53449154, 0.30564335]], dtype=float32),
array([[-0.24281949],
[-0.10461146],
[ 0.11201331]], dtype=float32)]


So, as you can see above, I wanted two output: one should have been an array with size 3, the other should have been a normal value. Instead, I get a 3x3 matrix, and an array with 3 elements. What am I doing wrong?










share|improve this question





























    0















    I have the following neural network in Keras:



    inp = layers.Input((3,))
    #Middle layers omitted
    out_prop = layers.Dense(units=3, activation='softmax')(inp)
    out_value = layers.Dense(units=1, activation = 'linear')(inp)


    Then I prepared a pseudo-input to test my network:



    inpu = np.array([[1,2,3],[4,5,6],[7,8,9]])


    When I try to predict, this happens:



    In [45]:nn.network.predict(inpu)
    Out[45]:
    [array([[0.257513 , 0.41672954, 0.32575747],
    [0.20175152, 0.4763418 , 0.32190666],
    [0.15986516, 0.53449154, 0.30564335]], dtype=float32),
    array([[-0.24281949],
    [-0.10461146],
    [ 0.11201331]], dtype=float32)]


    So, as you can see above, I wanted two output: one should have been an array with size 3, the other should have been a normal value. Instead, I get a 3x3 matrix, and an array with 3 elements. What am I doing wrong?










    share|improve this question



























      0












      0








      0








      I have the following neural network in Keras:



      inp = layers.Input((3,))
      #Middle layers omitted
      out_prop = layers.Dense(units=3, activation='softmax')(inp)
      out_value = layers.Dense(units=1, activation = 'linear')(inp)


      Then I prepared a pseudo-input to test my network:



      inpu = np.array([[1,2,3],[4,5,6],[7,8,9]])


      When I try to predict, this happens:



      In [45]:nn.network.predict(inpu)
      Out[45]:
      [array([[0.257513 , 0.41672954, 0.32575747],
      [0.20175152, 0.4763418 , 0.32190666],
      [0.15986516, 0.53449154, 0.30564335]], dtype=float32),
      array([[-0.24281949],
      [-0.10461146],
      [ 0.11201331]], dtype=float32)]


      So, as you can see above, I wanted two output: one should have been an array with size 3, the other should have been a normal value. Instead, I get a 3x3 matrix, and an array with 3 elements. What am I doing wrong?










      share|improve this question
















      I have the following neural network in Keras:



      inp = layers.Input((3,))
      #Middle layers omitted
      out_prop = layers.Dense(units=3, activation='softmax')(inp)
      out_value = layers.Dense(units=1, activation = 'linear')(inp)


      Then I prepared a pseudo-input to test my network:



      inpu = np.array([[1,2,3],[4,5,6],[7,8,9]])


      When I try to predict, this happens:



      In [45]:nn.network.predict(inpu)
      Out[45]:
      [array([[0.257513 , 0.41672954, 0.32575747],
      [0.20175152, 0.4763418 , 0.32190666],
      [0.15986516, 0.53449154, 0.30564335]], dtype=float32),
      array([[-0.24281949],
      [-0.10461146],
      [ 0.11201331]], dtype=float32)]


      So, as you can see above, I wanted two output: one should have been an array with size 3, the other should have been a normal value. Instead, I get a 3x3 matrix, and an array with 3 elements. What am I doing wrong?







      python machine-learning keras neural-network keras-layer






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 12:22









      today

      11.2k22139




      11.2k22139










      asked Nov 21 '18 at 11:58









      Federico DoratoFederico Dorato

      4211




      4211
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You are passing three input samples to the network:



          >>> inpu.shape
          (3,3) # three samples of size 3


          And you have two output layers: one of them outputs a vector of size 3 for each sample and the other outputs a vector of size one (i.e. scalar), again for each sample. As a result the output shapes would be (3, 3) and (3, 1).



          Update: If you want your network to accept an input sample of shape (3,3) and outputs vectors of size 3 and 1, and you want to only use Dense layers in your network, then you must use a Flatten layer somewhere in the model. One possible option is to use it right after the input layer:



          inp = layers.Input((3,3))  # don't forget to set the correct input shape
          x = Flatten()(inp)
          # pass x to other Dense layers


          Alternatively, you could flatten your data to have a shape of (num_samples, 9) and then pass it to your network without using a Flatten layer.



          Update 2: As @Mete correctly pointed out in the comments, make sure the input array have a shape of (num_samples, 3, 3) if each input sample has a shape of (3,3).






          share|improve this answer


























          • I don't want to have in input 3 samples of size 3. I want to have one sample of size 3x3. Could you tell me how to do that?

            – Federico Dorato
            Nov 21 '18 at 12:22











          • @FedericoDorato Please see my updated answer.

            – today
            Nov 21 '18 at 12:28











          • inp = layers.Input((3,3))means every sample is 3x3 inpu = np.array([[[1,2,3],[4,5,6],[7,8,9]]]) (note shape is 1x3x3) means you have 1 sample of 3x3 data

            – Mete Han Kahraman
            Nov 21 '18 at 12:29











          • @FedericoDorato Please see the second update.

            – today
            Nov 21 '18 at 12:35











          • @today i am forced to follow this: "Alternatively, you could flatten your data to have a shape of (num_samples, 9) and then pass it to your network without using a Flatten layer." Because otherwise there is this error: 'Tensor' object has no attribute 'lower'

            – Federico Dorato
            Nov 30 '18 at 15:05











          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%2f53411566%2fkeras-input-with-size-xx-generates-unwanted-output-yx%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









          0














          You are passing three input samples to the network:



          >>> inpu.shape
          (3,3) # three samples of size 3


          And you have two output layers: one of them outputs a vector of size 3 for each sample and the other outputs a vector of size one (i.e. scalar), again for each sample. As a result the output shapes would be (3, 3) and (3, 1).



          Update: If you want your network to accept an input sample of shape (3,3) and outputs vectors of size 3 and 1, and you want to only use Dense layers in your network, then you must use a Flatten layer somewhere in the model. One possible option is to use it right after the input layer:



          inp = layers.Input((3,3))  # don't forget to set the correct input shape
          x = Flatten()(inp)
          # pass x to other Dense layers


          Alternatively, you could flatten your data to have a shape of (num_samples, 9) and then pass it to your network without using a Flatten layer.



          Update 2: As @Mete correctly pointed out in the comments, make sure the input array have a shape of (num_samples, 3, 3) if each input sample has a shape of (3,3).






          share|improve this answer


























          • I don't want to have in input 3 samples of size 3. I want to have one sample of size 3x3. Could you tell me how to do that?

            – Federico Dorato
            Nov 21 '18 at 12:22











          • @FedericoDorato Please see my updated answer.

            – today
            Nov 21 '18 at 12:28











          • inp = layers.Input((3,3))means every sample is 3x3 inpu = np.array([[[1,2,3],[4,5,6],[7,8,9]]]) (note shape is 1x3x3) means you have 1 sample of 3x3 data

            – Mete Han Kahraman
            Nov 21 '18 at 12:29











          • @FedericoDorato Please see the second update.

            – today
            Nov 21 '18 at 12:35











          • @today i am forced to follow this: "Alternatively, you could flatten your data to have a shape of (num_samples, 9) and then pass it to your network without using a Flatten layer." Because otherwise there is this error: 'Tensor' object has no attribute 'lower'

            – Federico Dorato
            Nov 30 '18 at 15:05
















          0














          You are passing three input samples to the network:



          >>> inpu.shape
          (3,3) # three samples of size 3


          And you have two output layers: one of them outputs a vector of size 3 for each sample and the other outputs a vector of size one (i.e. scalar), again for each sample. As a result the output shapes would be (3, 3) and (3, 1).



          Update: If you want your network to accept an input sample of shape (3,3) and outputs vectors of size 3 and 1, and you want to only use Dense layers in your network, then you must use a Flatten layer somewhere in the model. One possible option is to use it right after the input layer:



          inp = layers.Input((3,3))  # don't forget to set the correct input shape
          x = Flatten()(inp)
          # pass x to other Dense layers


          Alternatively, you could flatten your data to have a shape of (num_samples, 9) and then pass it to your network without using a Flatten layer.



          Update 2: As @Mete correctly pointed out in the comments, make sure the input array have a shape of (num_samples, 3, 3) if each input sample has a shape of (3,3).






          share|improve this answer


























          • I don't want to have in input 3 samples of size 3. I want to have one sample of size 3x3. Could you tell me how to do that?

            – Federico Dorato
            Nov 21 '18 at 12:22











          • @FedericoDorato Please see my updated answer.

            – today
            Nov 21 '18 at 12:28











          • inp = layers.Input((3,3))means every sample is 3x3 inpu = np.array([[[1,2,3],[4,5,6],[7,8,9]]]) (note shape is 1x3x3) means you have 1 sample of 3x3 data

            – Mete Han Kahraman
            Nov 21 '18 at 12:29











          • @FedericoDorato Please see the second update.

            – today
            Nov 21 '18 at 12:35











          • @today i am forced to follow this: "Alternatively, you could flatten your data to have a shape of (num_samples, 9) and then pass it to your network without using a Flatten layer." Because otherwise there is this error: 'Tensor' object has no attribute 'lower'

            – Federico Dorato
            Nov 30 '18 at 15:05














          0












          0








          0







          You are passing three input samples to the network:



          >>> inpu.shape
          (3,3) # three samples of size 3


          And you have two output layers: one of them outputs a vector of size 3 for each sample and the other outputs a vector of size one (i.e. scalar), again for each sample. As a result the output shapes would be (3, 3) and (3, 1).



          Update: If you want your network to accept an input sample of shape (3,3) and outputs vectors of size 3 and 1, and you want to only use Dense layers in your network, then you must use a Flatten layer somewhere in the model. One possible option is to use it right after the input layer:



          inp = layers.Input((3,3))  # don't forget to set the correct input shape
          x = Flatten()(inp)
          # pass x to other Dense layers


          Alternatively, you could flatten your data to have a shape of (num_samples, 9) and then pass it to your network without using a Flatten layer.



          Update 2: As @Mete correctly pointed out in the comments, make sure the input array have a shape of (num_samples, 3, 3) if each input sample has a shape of (3,3).






          share|improve this answer















          You are passing three input samples to the network:



          >>> inpu.shape
          (3,3) # three samples of size 3


          And you have two output layers: one of them outputs a vector of size 3 for each sample and the other outputs a vector of size one (i.e. scalar), again for each sample. As a result the output shapes would be (3, 3) and (3, 1).



          Update: If you want your network to accept an input sample of shape (3,3) and outputs vectors of size 3 and 1, and you want to only use Dense layers in your network, then you must use a Flatten layer somewhere in the model. One possible option is to use it right after the input layer:



          inp = layers.Input((3,3))  # don't forget to set the correct input shape
          x = Flatten()(inp)
          # pass x to other Dense layers


          Alternatively, you could flatten your data to have a shape of (num_samples, 9) and then pass it to your network without using a Flatten layer.



          Update 2: As @Mete correctly pointed out in the comments, make sure the input array have a shape of (num_samples, 3, 3) if each input sample has a shape of (3,3).







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 '18 at 12:33

























          answered Nov 21 '18 at 12:20









          todaytoday

          11.2k22139




          11.2k22139













          • I don't want to have in input 3 samples of size 3. I want to have one sample of size 3x3. Could you tell me how to do that?

            – Federico Dorato
            Nov 21 '18 at 12:22











          • @FedericoDorato Please see my updated answer.

            – today
            Nov 21 '18 at 12:28











          • inp = layers.Input((3,3))means every sample is 3x3 inpu = np.array([[[1,2,3],[4,5,6],[7,8,9]]]) (note shape is 1x3x3) means you have 1 sample of 3x3 data

            – Mete Han Kahraman
            Nov 21 '18 at 12:29











          • @FedericoDorato Please see the second update.

            – today
            Nov 21 '18 at 12:35











          • @today i am forced to follow this: "Alternatively, you could flatten your data to have a shape of (num_samples, 9) and then pass it to your network without using a Flatten layer." Because otherwise there is this error: 'Tensor' object has no attribute 'lower'

            – Federico Dorato
            Nov 30 '18 at 15:05



















          • I don't want to have in input 3 samples of size 3. I want to have one sample of size 3x3. Could you tell me how to do that?

            – Federico Dorato
            Nov 21 '18 at 12:22











          • @FedericoDorato Please see my updated answer.

            – today
            Nov 21 '18 at 12:28











          • inp = layers.Input((3,3))means every sample is 3x3 inpu = np.array([[[1,2,3],[4,5,6],[7,8,9]]]) (note shape is 1x3x3) means you have 1 sample of 3x3 data

            – Mete Han Kahraman
            Nov 21 '18 at 12:29











          • @FedericoDorato Please see the second update.

            – today
            Nov 21 '18 at 12:35











          • @today i am forced to follow this: "Alternatively, you could flatten your data to have a shape of (num_samples, 9) and then pass it to your network without using a Flatten layer." Because otherwise there is this error: 'Tensor' object has no attribute 'lower'

            – Federico Dorato
            Nov 30 '18 at 15:05

















          I don't want to have in input 3 samples of size 3. I want to have one sample of size 3x3. Could you tell me how to do that?

          – Federico Dorato
          Nov 21 '18 at 12:22





          I don't want to have in input 3 samples of size 3. I want to have one sample of size 3x3. Could you tell me how to do that?

          – Federico Dorato
          Nov 21 '18 at 12:22













          @FedericoDorato Please see my updated answer.

          – today
          Nov 21 '18 at 12:28





          @FedericoDorato Please see my updated answer.

          – today
          Nov 21 '18 at 12:28













          inp = layers.Input((3,3))means every sample is 3x3 inpu = np.array([[[1,2,3],[4,5,6],[7,8,9]]]) (note shape is 1x3x3) means you have 1 sample of 3x3 data

          – Mete Han Kahraman
          Nov 21 '18 at 12:29





          inp = layers.Input((3,3))means every sample is 3x3 inpu = np.array([[[1,2,3],[4,5,6],[7,8,9]]]) (note shape is 1x3x3) means you have 1 sample of 3x3 data

          – Mete Han Kahraman
          Nov 21 '18 at 12:29













          @FedericoDorato Please see the second update.

          – today
          Nov 21 '18 at 12:35





          @FedericoDorato Please see the second update.

          – today
          Nov 21 '18 at 12:35













          @today i am forced to follow this: "Alternatively, you could flatten your data to have a shape of (num_samples, 9) and then pass it to your network without using a Flatten layer." Because otherwise there is this error: 'Tensor' object has no attribute 'lower'

          – Federico Dorato
          Nov 30 '18 at 15:05





          @today i am forced to follow this: "Alternatively, you could flatten your data to have a shape of (num_samples, 9) and then pass it to your network without using a Flatten layer." Because otherwise there is this error: 'Tensor' object has no attribute 'lower'

          – Federico Dorato
          Nov 30 '18 at 15:05




















          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%2f53411566%2fkeras-input-with-size-xx-generates-unwanted-output-yx%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?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

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