What is the job of “RepeatVector” and “TimeDistributed”?












0












$begingroup$


I read about them in Keras documentation and other websites, but I couldn't exactly understand what do exactly they do and how should we use them in designing many-to-many or encoder-decoder LSTM networks?



May someone explains what do they do in this code:



model = Sequential()  
model.add(LSTM(input_dim=1, output_dim=hidden_neurons, return_sequences=False))
model.add(RepeatVector(10))
model.add(LSTM(output_dim=hidden_neurons, return_sequences=True))
model.add(TimeDistributed(Dense(1)))
model.add(Activation('linear'))
model.compile(loss='mean_squared_error', optimizer='rmsprop', metrics=['accuracy'])


It's solution of this problem here(https://github.com/keras-team/keras/issues/6063).
Thank you in advanced!










share|improve this question











$endgroup$

















    0












    $begingroup$


    I read about them in Keras documentation and other websites, but I couldn't exactly understand what do exactly they do and how should we use them in designing many-to-many or encoder-decoder LSTM networks?



    May someone explains what do they do in this code:



    model = Sequential()  
    model.add(LSTM(input_dim=1, output_dim=hidden_neurons, return_sequences=False))
    model.add(RepeatVector(10))
    model.add(LSTM(output_dim=hidden_neurons, return_sequences=True))
    model.add(TimeDistributed(Dense(1)))
    model.add(Activation('linear'))
    model.compile(loss='mean_squared_error', optimizer='rmsprop', metrics=['accuracy'])


    It's solution of this problem here(https://github.com/keras-team/keras/issues/6063).
    Thank you in advanced!










    share|improve this question











    $endgroup$















      0












      0








      0





      $begingroup$


      I read about them in Keras documentation and other websites, but I couldn't exactly understand what do exactly they do and how should we use them in designing many-to-many or encoder-decoder LSTM networks?



      May someone explains what do they do in this code:



      model = Sequential()  
      model.add(LSTM(input_dim=1, output_dim=hidden_neurons, return_sequences=False))
      model.add(RepeatVector(10))
      model.add(LSTM(output_dim=hidden_neurons, return_sequences=True))
      model.add(TimeDistributed(Dense(1)))
      model.add(Activation('linear'))
      model.compile(loss='mean_squared_error', optimizer='rmsprop', metrics=['accuracy'])


      It's solution of this problem here(https://github.com/keras-team/keras/issues/6063).
      Thank you in advanced!










      share|improve this question











      $endgroup$




      I read about them in Keras documentation and other websites, but I couldn't exactly understand what do exactly they do and how should we use them in designing many-to-many or encoder-decoder LSTM networks?



      May someone explains what do they do in this code:



      model = Sequential()  
      model.add(LSTM(input_dim=1, output_dim=hidden_neurons, return_sequences=False))
      model.add(RepeatVector(10))
      model.add(LSTM(output_dim=hidden_neurons, return_sequences=True))
      model.add(TimeDistributed(Dense(1)))
      model.add(Activation('linear'))
      model.compile(loss='mean_squared_error', optimizer='rmsprop', metrics=['accuracy'])


      It's solution of this problem here(https://github.com/keras-team/keras/issues/6063).
      Thank you in advanced!







      lstm






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 15 hours ago







      user145959

















      asked yesterday









      user145959user145959

      1027




      1027






















          1 Answer
          1






          active

          oldest

          votes


















          1












          $begingroup$

          tf.keras.layers.RepeatVector



          According to the docs :




          Repeats the input n times.




          They have also provided an example :



          model = Sequential()
          model.add(Dense(32, input_dim=32))
          # now: model.output_shape == (None, 32)
          # note: `None` is the batch dimension
          model.add(RepeatVector(3))
          # now: model.output_shape == (None, 3, 32)


          In the above example, the RepeatVector layer repeats the incoming inputs a specific number of time. The shape of the input in the above example was ( 32 , ). But the output shape of the RepeatVector was ( 3 , 32 ), since the inputs were repeated 3 times.



          tf.keras.layers.TimeDistributed()



          According to the docs :




          This wrapper allows to apply a layer to every temporal slice of an input.
          The input should be at least 3D, and the dimension of index one will be considered to be the temporal dimension.




          You can refer to the example at their website.



          TimeDistributed layer applies a specific layer such as Dense to every sample it receives as an input. Suppose the input size is ( 13 , 10 , 6 ). Now, I need to apply a Dense layer to every slice of shape ( 10 , 6 ). Then I would wrap the Dense layer in a TimeDistributed layer.



          model.add( TimeDistributed( Dense( 12 , input_shape=( 10 , 6 ) )) )


          The output shape of such a layer would be ( 13 , 10 , 12 ). Hence, the operation of the Dense layer was applied to each temporal slice as mentioned.






          share|improve this answer









          $endgroup$













          • $begingroup$
            Thanks Shubham. I edited my question and added a new question. Please help me on that too.
            $endgroup$
            – user145959
            15 hours ago











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "557"
          };
          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: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2fdatascience.stackexchange.com%2fquestions%2f46491%2fwhat-is-the-job-of-repeatvector-and-timedistributed%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












          $begingroup$

          tf.keras.layers.RepeatVector



          According to the docs :




          Repeats the input n times.




          They have also provided an example :



          model = Sequential()
          model.add(Dense(32, input_dim=32))
          # now: model.output_shape == (None, 32)
          # note: `None` is the batch dimension
          model.add(RepeatVector(3))
          # now: model.output_shape == (None, 3, 32)


          In the above example, the RepeatVector layer repeats the incoming inputs a specific number of time. The shape of the input in the above example was ( 32 , ). But the output shape of the RepeatVector was ( 3 , 32 ), since the inputs were repeated 3 times.



          tf.keras.layers.TimeDistributed()



          According to the docs :




          This wrapper allows to apply a layer to every temporal slice of an input.
          The input should be at least 3D, and the dimension of index one will be considered to be the temporal dimension.




          You can refer to the example at their website.



          TimeDistributed layer applies a specific layer such as Dense to every sample it receives as an input. Suppose the input size is ( 13 , 10 , 6 ). Now, I need to apply a Dense layer to every slice of shape ( 10 , 6 ). Then I would wrap the Dense layer in a TimeDistributed layer.



          model.add( TimeDistributed( Dense( 12 , input_shape=( 10 , 6 ) )) )


          The output shape of such a layer would be ( 13 , 10 , 12 ). Hence, the operation of the Dense layer was applied to each temporal slice as mentioned.






          share|improve this answer









          $endgroup$













          • $begingroup$
            Thanks Shubham. I edited my question and added a new question. Please help me on that too.
            $endgroup$
            – user145959
            15 hours ago
















          1












          $begingroup$

          tf.keras.layers.RepeatVector



          According to the docs :




          Repeats the input n times.




          They have also provided an example :



          model = Sequential()
          model.add(Dense(32, input_dim=32))
          # now: model.output_shape == (None, 32)
          # note: `None` is the batch dimension
          model.add(RepeatVector(3))
          # now: model.output_shape == (None, 3, 32)


          In the above example, the RepeatVector layer repeats the incoming inputs a specific number of time. The shape of the input in the above example was ( 32 , ). But the output shape of the RepeatVector was ( 3 , 32 ), since the inputs were repeated 3 times.



          tf.keras.layers.TimeDistributed()



          According to the docs :




          This wrapper allows to apply a layer to every temporal slice of an input.
          The input should be at least 3D, and the dimension of index one will be considered to be the temporal dimension.




          You can refer to the example at their website.



          TimeDistributed layer applies a specific layer such as Dense to every sample it receives as an input. Suppose the input size is ( 13 , 10 , 6 ). Now, I need to apply a Dense layer to every slice of shape ( 10 , 6 ). Then I would wrap the Dense layer in a TimeDistributed layer.



          model.add( TimeDistributed( Dense( 12 , input_shape=( 10 , 6 ) )) )


          The output shape of such a layer would be ( 13 , 10 , 12 ). Hence, the operation of the Dense layer was applied to each temporal slice as mentioned.






          share|improve this answer









          $endgroup$













          • $begingroup$
            Thanks Shubham. I edited my question and added a new question. Please help me on that too.
            $endgroup$
            – user145959
            15 hours ago














          1












          1








          1





          $begingroup$

          tf.keras.layers.RepeatVector



          According to the docs :




          Repeats the input n times.




          They have also provided an example :



          model = Sequential()
          model.add(Dense(32, input_dim=32))
          # now: model.output_shape == (None, 32)
          # note: `None` is the batch dimension
          model.add(RepeatVector(3))
          # now: model.output_shape == (None, 3, 32)


          In the above example, the RepeatVector layer repeats the incoming inputs a specific number of time. The shape of the input in the above example was ( 32 , ). But the output shape of the RepeatVector was ( 3 , 32 ), since the inputs were repeated 3 times.



          tf.keras.layers.TimeDistributed()



          According to the docs :




          This wrapper allows to apply a layer to every temporal slice of an input.
          The input should be at least 3D, and the dimension of index one will be considered to be the temporal dimension.




          You can refer to the example at their website.



          TimeDistributed layer applies a specific layer such as Dense to every sample it receives as an input. Suppose the input size is ( 13 , 10 , 6 ). Now, I need to apply a Dense layer to every slice of shape ( 10 , 6 ). Then I would wrap the Dense layer in a TimeDistributed layer.



          model.add( TimeDistributed( Dense( 12 , input_shape=( 10 , 6 ) )) )


          The output shape of such a layer would be ( 13 , 10 , 12 ). Hence, the operation of the Dense layer was applied to each temporal slice as mentioned.






          share|improve this answer









          $endgroup$



          tf.keras.layers.RepeatVector



          According to the docs :




          Repeats the input n times.




          They have also provided an example :



          model = Sequential()
          model.add(Dense(32, input_dim=32))
          # now: model.output_shape == (None, 32)
          # note: `None` is the batch dimension
          model.add(RepeatVector(3))
          # now: model.output_shape == (None, 3, 32)


          In the above example, the RepeatVector layer repeats the incoming inputs a specific number of time. The shape of the input in the above example was ( 32 , ). But the output shape of the RepeatVector was ( 3 , 32 ), since the inputs were repeated 3 times.



          tf.keras.layers.TimeDistributed()



          According to the docs :




          This wrapper allows to apply a layer to every temporal slice of an input.
          The input should be at least 3D, and the dimension of index one will be considered to be the temporal dimension.




          You can refer to the example at their website.



          TimeDistributed layer applies a specific layer such as Dense to every sample it receives as an input. Suppose the input size is ( 13 , 10 , 6 ). Now, I need to apply a Dense layer to every slice of shape ( 10 , 6 ). Then I would wrap the Dense layer in a TimeDistributed layer.



          model.add( TimeDistributed( Dense( 12 , input_shape=( 10 , 6 ) )) )


          The output shape of such a layer would be ( 13 , 10 , 12 ). Hence, the operation of the Dense layer was applied to each temporal slice as mentioned.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 19 hours ago









          Shubham PanchalShubham Panchal

          2114




          2114












          • $begingroup$
            Thanks Shubham. I edited my question and added a new question. Please help me on that too.
            $endgroup$
            – user145959
            15 hours ago


















          • $begingroup$
            Thanks Shubham. I edited my question and added a new question. Please help me on that too.
            $endgroup$
            – user145959
            15 hours ago
















          $begingroup$
          Thanks Shubham. I edited my question and added a new question. Please help me on that too.
          $endgroup$
          – user145959
          15 hours ago




          $begingroup$
          Thanks Shubham. I edited my question and added a new question. Please help me on that too.
          $endgroup$
          – user145959
          15 hours ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Data Science Stack Exchange!


          • 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.


          Use MathJax to format equations. MathJax reference.


          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%2fdatascience.stackexchange.com%2fquestions%2f46491%2fwhat-is-the-job-of-repeatvector-and-timedistributed%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

          Callistus I

          Tabula Rosettana

          How to label and detect the document text images