Convolution2D gives wrong output shape in Keras





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







0















I'm following tutorial from here and have stuck with simple model creation



model = Sequential()
model.add(Convolution2D(32, kernel_size=(3, 3), activation='relu', input_shape=(1, 28, 28)))
model.output_shape


This should give (None, 32, 26, 26) output but instead I get (None, -1, 26, 32). I guess something wrong with parameters provided or I need to make additional reshape. Maybe something has been changed for Convolution2D and Conv2D in Keras? I'm running 2.2.4 with Theano backend










share|improve this question

























  • I get InvalidArgumentError with your code.

    – Tzomas
    Nov 16 '18 at 12:13











  • @Tzomas check keras version you are running, probably you have old one

    – Most Wanted
    Nov 16 '18 at 15:04


















0















I'm following tutorial from here and have stuck with simple model creation



model = Sequential()
model.add(Convolution2D(32, kernel_size=(3, 3), activation='relu', input_shape=(1, 28, 28)))
model.output_shape


This should give (None, 32, 26, 26) output but instead I get (None, -1, 26, 32). I guess something wrong with parameters provided or I need to make additional reshape. Maybe something has been changed for Convolution2D and Conv2D in Keras? I'm running 2.2.4 with Theano backend










share|improve this question

























  • I get InvalidArgumentError with your code.

    – Tzomas
    Nov 16 '18 at 12:13











  • @Tzomas check keras version you are running, probably you have old one

    – Most Wanted
    Nov 16 '18 at 15:04














0












0








0








I'm following tutorial from here and have stuck with simple model creation



model = Sequential()
model.add(Convolution2D(32, kernel_size=(3, 3), activation='relu', input_shape=(1, 28, 28)))
model.output_shape


This should give (None, 32, 26, 26) output but instead I get (None, -1, 26, 32). I guess something wrong with parameters provided or I need to make additional reshape. Maybe something has been changed for Convolution2D and Conv2D in Keras? I'm running 2.2.4 with Theano backend










share|improve this question
















I'm following tutorial from here and have stuck with simple model creation



model = Sequential()
model.add(Convolution2D(32, kernel_size=(3, 3), activation='relu', input_shape=(1, 28, 28)))
model.output_shape


This should give (None, 32, 26, 26) output but instead I get (None, -1, 26, 32). I guess something wrong with parameters provided or I need to make additional reshape. Maybe something has been changed for Convolution2D and Conv2D in Keras? I'm running 2.2.4 with Theano backend







python machine-learning keras theano convolution






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 12:09









Mihai Alexandru-Ionut

30.7k64375




30.7k64375










asked Nov 16 '18 at 12:06









Most WantedMost Wanted

1,14721733




1,14721733













  • I get InvalidArgumentError with your code.

    – Tzomas
    Nov 16 '18 at 12:13











  • @Tzomas check keras version you are running, probably you have old one

    – Most Wanted
    Nov 16 '18 at 15:04



















  • I get InvalidArgumentError with your code.

    – Tzomas
    Nov 16 '18 at 12:13











  • @Tzomas check keras version you are running, probably you have old one

    – Most Wanted
    Nov 16 '18 at 15:04

















I get InvalidArgumentError with your code.

– Tzomas
Nov 16 '18 at 12:13





I get InvalidArgumentError with your code.

– Tzomas
Nov 16 '18 at 12:13













@Tzomas check keras version you are running, probably you have old one

– Most Wanted
Nov 16 '18 at 15:04





@Tzomas check keras version you are running, probably you have old one

– Most Wanted
Nov 16 '18 at 15:04












3 Answers
3






active

oldest

votes


















1














It seems that the channels are in the first axis of input data. Therefore, you need to set data_format argument to "channels_first":



model = Sequential()
model.add(Convolution2D(32, kernel_size=(3, 3),
data_format='channels_first',
activation='relu', input_shape=(1, 28, 28)))
model.output_shape


Which gives:



(None, 32, 26, 26)





share|improve this answer
























  • perfect, works like a charm

    – Most Wanted
    Nov 16 '18 at 15:03



















0














The problem is that it uses Theano as a backend and probably you are using tensorflow (you should check your setting once again). You can change it with:



import os
os.environ['KERAS_BACKEND'] = 'theano'


This should be done before importing keras






share|improve this answer































    0














    Well like I wrote in my comment. I got a error with your code. So see that it looks like you have bad shape on input, if you change to input_shape=(28,28,1) then output_shape returns (None, 26, 26, 32)



    I don't know if this is what you are exactly looking for.






    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%2f53337586%2fconvolution2d-gives-wrong-output-shape-in-keras%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      It seems that the channels are in the first axis of input data. Therefore, you need to set data_format argument to "channels_first":



      model = Sequential()
      model.add(Convolution2D(32, kernel_size=(3, 3),
      data_format='channels_first',
      activation='relu', input_shape=(1, 28, 28)))
      model.output_shape


      Which gives:



      (None, 32, 26, 26)





      share|improve this answer
























      • perfect, works like a charm

        – Most Wanted
        Nov 16 '18 at 15:03
















      1














      It seems that the channels are in the first axis of input data. Therefore, you need to set data_format argument to "channels_first":



      model = Sequential()
      model.add(Convolution2D(32, kernel_size=(3, 3),
      data_format='channels_first',
      activation='relu', input_shape=(1, 28, 28)))
      model.output_shape


      Which gives:



      (None, 32, 26, 26)





      share|improve this answer
























      • perfect, works like a charm

        – Most Wanted
        Nov 16 '18 at 15:03














      1












      1








      1







      It seems that the channels are in the first axis of input data. Therefore, you need to set data_format argument to "channels_first":



      model = Sequential()
      model.add(Convolution2D(32, kernel_size=(3, 3),
      data_format='channels_first',
      activation='relu', input_shape=(1, 28, 28)))
      model.output_shape


      Which gives:



      (None, 32, 26, 26)





      share|improve this answer













      It seems that the channels are in the first axis of input data. Therefore, you need to set data_format argument to "channels_first":



      model = Sequential()
      model.add(Convolution2D(32, kernel_size=(3, 3),
      data_format='channels_first',
      activation='relu', input_shape=(1, 28, 28)))
      model.output_shape


      Which gives:



      (None, 32, 26, 26)






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 16 '18 at 12:44









      todaytoday

      11.6k22239




      11.6k22239













      • perfect, works like a charm

        – Most Wanted
        Nov 16 '18 at 15:03



















      • perfect, works like a charm

        – Most Wanted
        Nov 16 '18 at 15:03

















      perfect, works like a charm

      – Most Wanted
      Nov 16 '18 at 15:03





      perfect, works like a charm

      – Most Wanted
      Nov 16 '18 at 15:03













      0














      The problem is that it uses Theano as a backend and probably you are using tensorflow (you should check your setting once again). You can change it with:



      import os
      os.environ['KERAS_BACKEND'] = 'theano'


      This should be done before importing keras






      share|improve this answer




























        0














        The problem is that it uses Theano as a backend and probably you are using tensorflow (you should check your setting once again). You can change it with:



        import os
        os.environ['KERAS_BACKEND'] = 'theano'


        This should be done before importing keras






        share|improve this answer


























          0












          0








          0







          The problem is that it uses Theano as a backend and probably you are using tensorflow (you should check your setting once again). You can change it with:



          import os
          os.environ['KERAS_BACKEND'] = 'theano'


          This should be done before importing keras






          share|improve this answer













          The problem is that it uses Theano as a backend and probably you are using tensorflow (you should check your setting once again). You can change it with:



          import os
          os.environ['KERAS_BACKEND'] = 'theano'


          This should be done before importing keras







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 12:17









          GabeGabe

          322210




          322210























              0














              Well like I wrote in my comment. I got a error with your code. So see that it looks like you have bad shape on input, if you change to input_shape=(28,28,1) then output_shape returns (None, 26, 26, 32)



              I don't know if this is what you are exactly looking for.






              share|improve this answer




























                0














                Well like I wrote in my comment. I got a error with your code. So see that it looks like you have bad shape on input, if you change to input_shape=(28,28,1) then output_shape returns (None, 26, 26, 32)



                I don't know if this is what you are exactly looking for.






                share|improve this answer


























                  0












                  0








                  0







                  Well like I wrote in my comment. I got a error with your code. So see that it looks like you have bad shape on input, if you change to input_shape=(28,28,1) then output_shape returns (None, 26, 26, 32)



                  I don't know if this is what you are exactly looking for.






                  share|improve this answer













                  Well like I wrote in my comment. I got a error with your code. So see that it looks like you have bad shape on input, if you change to input_shape=(28,28,1) then output_shape returns (None, 26, 26, 32)



                  I don't know if this is what you are exactly looking for.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 12:22









                  TzomasTzomas

                  517314




                  517314






























                      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%2f53337586%2fconvolution2d-gives-wrong-output-shape-in-keras%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