Unflattening Layer in Keras












0















I would like to create a simple Keras neural network that accepts an input matrix of dimension (rows, columns) = (n, m), flattens the matrix to a dimension (n*m, 1), sends the flattened matrix through a number of arbitrary layers, and in the final layer, once more unflattens the matrix to a dimension of (n, m) before releasing this final matrix as an output.



The issue I'm having is that I haven't found any documentation for an Unflatten layer at the keras.io page, and I'm wondering whether there is a reason that such a seemingly standard common use layer doesn't exist. Is there a much more natural and easy way to do what I'm proposing?










share|improve this question




















  • 1





    did you consider keras.layers.reshape?

    – fr_andres
    Nov 16 '18 at 0:22











  • If the answer resolved your issue, kindly accept it by clicking on the checkmark next to the answer to mark it as "answered" - see What should I do when someone answers my question?

    – today
    Nov 26 '18 at 15:55
















0















I would like to create a simple Keras neural network that accepts an input matrix of dimension (rows, columns) = (n, m), flattens the matrix to a dimension (n*m, 1), sends the flattened matrix through a number of arbitrary layers, and in the final layer, once more unflattens the matrix to a dimension of (n, m) before releasing this final matrix as an output.



The issue I'm having is that I haven't found any documentation for an Unflatten layer at the keras.io page, and I'm wondering whether there is a reason that such a seemingly standard common use layer doesn't exist. Is there a much more natural and easy way to do what I'm proposing?










share|improve this question




















  • 1





    did you consider keras.layers.reshape?

    – fr_andres
    Nov 16 '18 at 0:22











  • If the answer resolved your issue, kindly accept it by clicking on the checkmark next to the answer to mark it as "answered" - see What should I do when someone answers my question?

    – today
    Nov 26 '18 at 15:55














0












0








0








I would like to create a simple Keras neural network that accepts an input matrix of dimension (rows, columns) = (n, m), flattens the matrix to a dimension (n*m, 1), sends the flattened matrix through a number of arbitrary layers, and in the final layer, once more unflattens the matrix to a dimension of (n, m) before releasing this final matrix as an output.



The issue I'm having is that I haven't found any documentation for an Unflatten layer at the keras.io page, and I'm wondering whether there is a reason that such a seemingly standard common use layer doesn't exist. Is there a much more natural and easy way to do what I'm proposing?










share|improve this question
















I would like to create a simple Keras neural network that accepts an input matrix of dimension (rows, columns) = (n, m), flattens the matrix to a dimension (n*m, 1), sends the flattened matrix through a number of arbitrary layers, and in the final layer, once more unflattens the matrix to a dimension of (n, m) before releasing this final matrix as an output.



The issue I'm having is that I haven't found any documentation for an Unflatten layer at the keras.io page, and I'm wondering whether there is a reason that such a seemingly standard common use layer doesn't exist. Is there a much more natural and easy way to do what I'm proposing?







python keras neural-network reshape flatten






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 7:59









today

11.3k22239




11.3k22239










asked Nov 16 '18 at 0:15









TQMTQM

194




194








  • 1





    did you consider keras.layers.reshape?

    – fr_andres
    Nov 16 '18 at 0:22











  • If the answer resolved your issue, kindly accept it by clicking on the checkmark next to the answer to mark it as "answered" - see What should I do when someone answers my question?

    – today
    Nov 26 '18 at 15:55














  • 1





    did you consider keras.layers.reshape?

    – fr_andres
    Nov 16 '18 at 0:22











  • If the answer resolved your issue, kindly accept it by clicking on the checkmark next to the answer to mark it as "answered" - see What should I do when someone answers my question?

    – today
    Nov 26 '18 at 15:55








1




1





did you consider keras.layers.reshape?

– fr_andres
Nov 16 '18 at 0:22





did you consider keras.layers.reshape?

– fr_andres
Nov 16 '18 at 0:22













If the answer resolved your issue, kindly accept it by clicking on the checkmark next to the answer to mark it as "answered" - see What should I do when someone answers my question?

– today
Nov 26 '18 at 15:55





If the answer resolved your issue, kindly accept it by clicking on the checkmark next to the answer to mark it as "answered" - see What should I do when someone answers my question?

– today
Nov 26 '18 at 15:55












1 Answer
1






active

oldest

votes


















0














You can use the Reshape layer for this purpose. It accepts the desired output shape as its argument and would reshape the input tensor to that shape. For example:



from keras.layers import Reshape

rsh_inp = Reshape((n*m, 1))(inp) # if don't want the last axis with dimension 1, you can also use Flatten layer

# rsh_inp goes through a number of arbitrary layers ...

# reshape back the output
out = Reshape((n,m))(out_rsh_inp)





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%2f53329659%2funflattening-layer-in-keras%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 can use the Reshape layer for this purpose. It accepts the desired output shape as its argument and would reshape the input tensor to that shape. For example:



    from keras.layers import Reshape

    rsh_inp = Reshape((n*m, 1))(inp) # if don't want the last axis with dimension 1, you can also use Flatten layer

    # rsh_inp goes through a number of arbitrary layers ...

    # reshape back the output
    out = Reshape((n,m))(out_rsh_inp)





    share|improve this answer




























      0














      You can use the Reshape layer for this purpose. It accepts the desired output shape as its argument and would reshape the input tensor to that shape. For example:



      from keras.layers import Reshape

      rsh_inp = Reshape((n*m, 1))(inp) # if don't want the last axis with dimension 1, you can also use Flatten layer

      # rsh_inp goes through a number of arbitrary layers ...

      # reshape back the output
      out = Reshape((n,m))(out_rsh_inp)





      share|improve this answer


























        0












        0








        0







        You can use the Reshape layer for this purpose. It accepts the desired output shape as its argument and would reshape the input tensor to that shape. For example:



        from keras.layers import Reshape

        rsh_inp = Reshape((n*m, 1))(inp) # if don't want the last axis with dimension 1, you can also use Flatten layer

        # rsh_inp goes through a number of arbitrary layers ...

        # reshape back the output
        out = Reshape((n,m))(out_rsh_inp)





        share|improve this answer













        You can use the Reshape layer for this purpose. It accepts the desired output shape as its argument and would reshape the input tensor to that shape. For example:



        from keras.layers import Reshape

        rsh_inp = Reshape((n*m, 1))(inp) # if don't want the last axis with dimension 1, you can also use Flatten layer

        # rsh_inp goes through a number of arbitrary layers ...

        # reshape back the output
        out = Reshape((n,m))(out_rsh_inp)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 7:57









        todaytoday

        11.3k22239




        11.3k22239
































            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%2f53329659%2funflattening-layer-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