keras Bidirectional layer using 4 dimension data












0















I'am designing keras model for classification based on article data.



I have data with 4 dimension as follows



[batch, article_num, word_num, word embedding size]


and i want to feed each (word_num, word embedding) data into keras Bidirectional layer



in order to get result with 3 dimension as follows.



[batch, article_num, bidirectional layer output size]


when i tried to feed 4 dimension data for testing like this



inp = Input(shape=(article_num, word_num, ))
# dims = [batch, article_num, word_num]

x = Reshape((article_num * word_num, ), input_shape = (article_num, word_num))(inp)
# dims = [batch, article_num * word_num]

x = Embedding(word_num, word_embedding_size, input_length = article_num * word_num)(x)
# dims = [batch, article_num * word_num, word_embedding_size]

x = Reshape((article_num , word_num, word_embedding_size),
input_shape = (article_num * word_num, word_embedding_size))(x)
# dims = [batch, article_num, word_num, word_embedding_size]

x = Bidirectional(CuDNNLSTM(50, return_sequences = True),
input_shape=(article_num , word_num, word_embedding_size))(x)


and i got the error



ValueError: Input 0 is incompatible with layer bidirectional_12: expected ndim=3, found ndim=4


how can i achieve this?










share|improve this question





























    0















    I'am designing keras model for classification based on article data.



    I have data with 4 dimension as follows



    [batch, article_num, word_num, word embedding size]


    and i want to feed each (word_num, word embedding) data into keras Bidirectional layer



    in order to get result with 3 dimension as follows.



    [batch, article_num, bidirectional layer output size]


    when i tried to feed 4 dimension data for testing like this



    inp = Input(shape=(article_num, word_num, ))
    # dims = [batch, article_num, word_num]

    x = Reshape((article_num * word_num, ), input_shape = (article_num, word_num))(inp)
    # dims = [batch, article_num * word_num]

    x = Embedding(word_num, word_embedding_size, input_length = article_num * word_num)(x)
    # dims = [batch, article_num * word_num, word_embedding_size]

    x = Reshape((article_num , word_num, word_embedding_size),
    input_shape = (article_num * word_num, word_embedding_size))(x)
    # dims = [batch, article_num, word_num, word_embedding_size]

    x = Bidirectional(CuDNNLSTM(50, return_sequences = True),
    input_shape=(article_num , word_num, word_embedding_size))(x)


    and i got the error



    ValueError: Input 0 is incompatible with layer bidirectional_12: expected ndim=3, found ndim=4


    how can i achieve this?










    share|improve this question



























      0












      0








      0








      I'am designing keras model for classification based on article data.



      I have data with 4 dimension as follows



      [batch, article_num, word_num, word embedding size]


      and i want to feed each (word_num, word embedding) data into keras Bidirectional layer



      in order to get result with 3 dimension as follows.



      [batch, article_num, bidirectional layer output size]


      when i tried to feed 4 dimension data for testing like this



      inp = Input(shape=(article_num, word_num, ))
      # dims = [batch, article_num, word_num]

      x = Reshape((article_num * word_num, ), input_shape = (article_num, word_num))(inp)
      # dims = [batch, article_num * word_num]

      x = Embedding(word_num, word_embedding_size, input_length = article_num * word_num)(x)
      # dims = [batch, article_num * word_num, word_embedding_size]

      x = Reshape((article_num , word_num, word_embedding_size),
      input_shape = (article_num * word_num, word_embedding_size))(x)
      # dims = [batch, article_num, word_num, word_embedding_size]

      x = Bidirectional(CuDNNLSTM(50, return_sequences = True),
      input_shape=(article_num , word_num, word_embedding_size))(x)


      and i got the error



      ValueError: Input 0 is incompatible with layer bidirectional_12: expected ndim=3, found ndim=4


      how can i achieve this?










      share|improve this question
















      I'am designing keras model for classification based on article data.



      I have data with 4 dimension as follows



      [batch, article_num, word_num, word embedding size]


      and i want to feed each (word_num, word embedding) data into keras Bidirectional layer



      in order to get result with 3 dimension as follows.



      [batch, article_num, bidirectional layer output size]


      when i tried to feed 4 dimension data for testing like this



      inp = Input(shape=(article_num, word_num, ))
      # dims = [batch, article_num, word_num]

      x = Reshape((article_num * word_num, ), input_shape = (article_num, word_num))(inp)
      # dims = [batch, article_num * word_num]

      x = Embedding(word_num, word_embedding_size, input_length = article_num * word_num)(x)
      # dims = [batch, article_num * word_num, word_embedding_size]

      x = Reshape((article_num , word_num, word_embedding_size),
      input_shape = (article_num * word_num, word_embedding_size))(x)
      # dims = [batch, article_num, word_num, word_embedding_size]

      x = Bidirectional(CuDNNLSTM(50, return_sequences = True),
      input_shape=(article_num , word_num, word_embedding_size))(x)


      and i got the error



      ValueError: Input 0 is incompatible with layer bidirectional_12: expected ndim=3, found ndim=4


      how can i achieve this?







      machine-learning keras lstm rnn word-embedding






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 '18 at 13:55









      Daniel Möller

      35.5k667104




      35.5k667104










      asked Nov 26 '18 at 13:13









      김동규김동규

      313




      313
























          1 Answer
          1






          active

          oldest

          votes


















          0














          If you don't want it to touch the article_num dimension, you can try using a TimeDistributed wrapper. But I'm not certain that it will be compatible with bidirectional and other stuff.



          inp = Input(shape=(article_num, word_num))    

          x = TimeDistributed(Embedding(word_num, word_embedding_size)(x))

          #option 1
          #x1 shape : (batch, article_num, word_num, 50)
          x1 = TimeDistributed(Bidirectional(CuDNNLSTM(50, return_sequences = True)))(x)

          #option 2
          #x2 shape : (batch, article_num, 50)
          x2 = TimeDistributed(Bidirectional(CuDNNLSTM(50)))(x)


          Hints:




          • Don't use input_shape everywhere, you only need it at the Input tensor.

          • You probably don't need any of the reshapes if you also use a TimeDistributed in the embedding.

          • If you don't want word_num in the final dimension, use return_sequences=False.






          share|improve this answer


























          • Thank you so much for quick and accurate answer! I achieved what i want with TimeDistributed wrapper even though i am unclear how it works.

            – 김동규
            Nov 26 '18 at 14:03











          • Well.... it just ignores the article_num dimension (first dimension after the batch size). As if each article were an individual sample in the batch.

            – Daniel Möller
            Nov 26 '18 at 14:04













          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%2f53481899%2fkeras-bidirectional-layer-using-4-dimension-data%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














          If you don't want it to touch the article_num dimension, you can try using a TimeDistributed wrapper. But I'm not certain that it will be compatible with bidirectional and other stuff.



          inp = Input(shape=(article_num, word_num))    

          x = TimeDistributed(Embedding(word_num, word_embedding_size)(x))

          #option 1
          #x1 shape : (batch, article_num, word_num, 50)
          x1 = TimeDistributed(Bidirectional(CuDNNLSTM(50, return_sequences = True)))(x)

          #option 2
          #x2 shape : (batch, article_num, 50)
          x2 = TimeDistributed(Bidirectional(CuDNNLSTM(50)))(x)


          Hints:




          • Don't use input_shape everywhere, you only need it at the Input tensor.

          • You probably don't need any of the reshapes if you also use a TimeDistributed in the embedding.

          • If you don't want word_num in the final dimension, use return_sequences=False.






          share|improve this answer


























          • Thank you so much for quick and accurate answer! I achieved what i want with TimeDistributed wrapper even though i am unclear how it works.

            – 김동규
            Nov 26 '18 at 14:03











          • Well.... it just ignores the article_num dimension (first dimension after the batch size). As if each article were an individual sample in the batch.

            – Daniel Möller
            Nov 26 '18 at 14:04


















          0














          If you don't want it to touch the article_num dimension, you can try using a TimeDistributed wrapper. But I'm not certain that it will be compatible with bidirectional and other stuff.



          inp = Input(shape=(article_num, word_num))    

          x = TimeDistributed(Embedding(word_num, word_embedding_size)(x))

          #option 1
          #x1 shape : (batch, article_num, word_num, 50)
          x1 = TimeDistributed(Bidirectional(CuDNNLSTM(50, return_sequences = True)))(x)

          #option 2
          #x2 shape : (batch, article_num, 50)
          x2 = TimeDistributed(Bidirectional(CuDNNLSTM(50)))(x)


          Hints:




          • Don't use input_shape everywhere, you only need it at the Input tensor.

          • You probably don't need any of the reshapes if you also use a TimeDistributed in the embedding.

          • If you don't want word_num in the final dimension, use return_sequences=False.






          share|improve this answer


























          • Thank you so much for quick and accurate answer! I achieved what i want with TimeDistributed wrapper even though i am unclear how it works.

            – 김동규
            Nov 26 '18 at 14:03











          • Well.... it just ignores the article_num dimension (first dimension after the batch size). As if each article were an individual sample in the batch.

            – Daniel Möller
            Nov 26 '18 at 14:04
















          0












          0








          0







          If you don't want it to touch the article_num dimension, you can try using a TimeDistributed wrapper. But I'm not certain that it will be compatible with bidirectional and other stuff.



          inp = Input(shape=(article_num, word_num))    

          x = TimeDistributed(Embedding(word_num, word_embedding_size)(x))

          #option 1
          #x1 shape : (batch, article_num, word_num, 50)
          x1 = TimeDistributed(Bidirectional(CuDNNLSTM(50, return_sequences = True)))(x)

          #option 2
          #x2 shape : (batch, article_num, 50)
          x2 = TimeDistributed(Bidirectional(CuDNNLSTM(50)))(x)


          Hints:




          • Don't use input_shape everywhere, you only need it at the Input tensor.

          • You probably don't need any of the reshapes if you also use a TimeDistributed in the embedding.

          • If you don't want word_num in the final dimension, use return_sequences=False.






          share|improve this answer















          If you don't want it to touch the article_num dimension, you can try using a TimeDistributed wrapper. But I'm not certain that it will be compatible with bidirectional and other stuff.



          inp = Input(shape=(article_num, word_num))    

          x = TimeDistributed(Embedding(word_num, word_embedding_size)(x))

          #option 1
          #x1 shape : (batch, article_num, word_num, 50)
          x1 = TimeDistributed(Bidirectional(CuDNNLSTM(50, return_sequences = True)))(x)

          #option 2
          #x2 shape : (batch, article_num, 50)
          x2 = TimeDistributed(Bidirectional(CuDNNLSTM(50)))(x)


          Hints:




          • Don't use input_shape everywhere, you only need it at the Input tensor.

          • You probably don't need any of the reshapes if you also use a TimeDistributed in the embedding.

          • If you don't want word_num in the final dimension, use return_sequences=False.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 26 '18 at 13:51

























          answered Nov 26 '18 at 13:45









          Daniel MöllerDaniel Möller

          35.5k667104




          35.5k667104













          • Thank you so much for quick and accurate answer! I achieved what i want with TimeDistributed wrapper even though i am unclear how it works.

            – 김동규
            Nov 26 '18 at 14:03











          • Well.... it just ignores the article_num dimension (first dimension after the batch size). As if each article were an individual sample in the batch.

            – Daniel Möller
            Nov 26 '18 at 14:04





















          • Thank you so much for quick and accurate answer! I achieved what i want with TimeDistributed wrapper even though i am unclear how it works.

            – 김동규
            Nov 26 '18 at 14:03











          • Well.... it just ignores the article_num dimension (first dimension after the batch size). As if each article were an individual sample in the batch.

            – Daniel Möller
            Nov 26 '18 at 14:04



















          Thank you so much for quick and accurate answer! I achieved what i want with TimeDistributed wrapper even though i am unclear how it works.

          – 김동규
          Nov 26 '18 at 14:03





          Thank you so much for quick and accurate answer! I achieved what i want with TimeDistributed wrapper even though i am unclear how it works.

          – 김동규
          Nov 26 '18 at 14:03













          Well.... it just ignores the article_num dimension (first dimension after the batch size). As if each article were an individual sample in the batch.

          – Daniel Möller
          Nov 26 '18 at 14:04







          Well.... it just ignores the article_num dimension (first dimension after the batch size). As if each article were an individual sample in the batch.

          – Daniel Möller
          Nov 26 '18 at 14:04






















          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%2f53481899%2fkeras-bidirectional-layer-using-4-dimension-data%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

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)