Using expand_dims in pytorch












0















I'm trying to tile a length 18 1 hot vector into a 40x40 grid.



Looking at pytorch docs, expand dims seems to be what i need.



But I cannot get it to work. Any idea what I'm doing wrong?



one_hot = torch.zeros(18).unsqueeze(0)
one_hot[0,1] = 1.0
one_hot
tensor([[0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
one_hot.expand(-1,-1,40,40)
Traceback (most recent call last):
File "<input>", line 1, in <module>
RuntimeError: The expanded size of the tensor (40) must match the existing size (18) at non-singleton dimension 3


I'm expecting a tensor of shape (1, 18, 40,40)










share|improve this question


















  • 1





    How are you mapping 18 values to the 40x40 matrix? What are the values in your expected output?

    – Jacques Kvam
    Dec 30 '18 at 5:02













  • Just copying the one hot. So each cell in the 40x40 matrix is populated with the 18 dimensional 1 hot vector.

    – Duane
    Dec 30 '18 at 6:12


















0















I'm trying to tile a length 18 1 hot vector into a 40x40 grid.



Looking at pytorch docs, expand dims seems to be what i need.



But I cannot get it to work. Any idea what I'm doing wrong?



one_hot = torch.zeros(18).unsqueeze(0)
one_hot[0,1] = 1.0
one_hot
tensor([[0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
one_hot.expand(-1,-1,40,40)
Traceback (most recent call last):
File "<input>", line 1, in <module>
RuntimeError: The expanded size of the tensor (40) must match the existing size (18) at non-singleton dimension 3


I'm expecting a tensor of shape (1, 18, 40,40)










share|improve this question


















  • 1





    How are you mapping 18 values to the 40x40 matrix? What are the values in your expected output?

    – Jacques Kvam
    Dec 30 '18 at 5:02













  • Just copying the one hot. So each cell in the 40x40 matrix is populated with the 18 dimensional 1 hot vector.

    – Duane
    Dec 30 '18 at 6:12
















0












0








0








I'm trying to tile a length 18 1 hot vector into a 40x40 grid.



Looking at pytorch docs, expand dims seems to be what i need.



But I cannot get it to work. Any idea what I'm doing wrong?



one_hot = torch.zeros(18).unsqueeze(0)
one_hot[0,1] = 1.0
one_hot
tensor([[0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
one_hot.expand(-1,-1,40,40)
Traceback (most recent call last):
File "<input>", line 1, in <module>
RuntimeError: The expanded size of the tensor (40) must match the existing size (18) at non-singleton dimension 3


I'm expecting a tensor of shape (1, 18, 40,40)










share|improve this question














I'm trying to tile a length 18 1 hot vector into a 40x40 grid.



Looking at pytorch docs, expand dims seems to be what i need.



But I cannot get it to work. Any idea what I'm doing wrong?



one_hot = torch.zeros(18).unsqueeze(0)
one_hot[0,1] = 1.0
one_hot
tensor([[0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
one_hot.expand(-1,-1,40,40)
Traceback (most recent call last):
File "<input>", line 1, in <module>
RuntimeError: The expanded size of the tensor (40) must match the existing size (18) at non-singleton dimension 3


I'm expecting a tensor of shape (1, 18, 40,40)







pytorch






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 30 '18 at 4:51









DuaneDuane

4911515




4911515








  • 1





    How are you mapping 18 values to the 40x40 matrix? What are the values in your expected output?

    – Jacques Kvam
    Dec 30 '18 at 5:02













  • Just copying the one hot. So each cell in the 40x40 matrix is populated with the 18 dimensional 1 hot vector.

    – Duane
    Dec 30 '18 at 6:12
















  • 1





    How are you mapping 18 values to the 40x40 matrix? What are the values in your expected output?

    – Jacques Kvam
    Dec 30 '18 at 5:02













  • Just copying the one hot. So each cell in the 40x40 matrix is populated with the 18 dimensional 1 hot vector.

    – Duane
    Dec 30 '18 at 6:12










1




1





How are you mapping 18 values to the 40x40 matrix? What are the values in your expected output?

– Jacques Kvam
Dec 30 '18 at 5:02







How are you mapping 18 values to the 40x40 matrix? What are the values in your expected output?

– Jacques Kvam
Dec 30 '18 at 5:02















Just copying the one hot. So each cell in the 40x40 matrix is populated with the 18 dimensional 1 hot vector.

– Duane
Dec 30 '18 at 6:12







Just copying the one hot. So each cell in the 40x40 matrix is populated with the 18 dimensional 1 hot vector.

– Duane
Dec 30 '18 at 6:12














1 Answer
1






active

oldest

votes


















1














expand works along singleton dimensions of the input tensor. In your example, you are trying to expand a 1-by-18 tensor along its (non-existent) third and fourth dimensions - this is why you are getting an error. The only singleton dimension (=dimension with size==1) you have is the first dimension.



fix



one_hot = torch.zeros(1,18,1,1, dtype=torch.float)  # create the tensor with all singleton dimensions in place
one_hot[0,1,0,0] = 1.
one_hot.expand(-1,-1,40,40)





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%2f53975352%2fusing-expand-dims-in-pytorch%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














    expand works along singleton dimensions of the input tensor. In your example, you are trying to expand a 1-by-18 tensor along its (non-existent) third and fourth dimensions - this is why you are getting an error. The only singleton dimension (=dimension with size==1) you have is the first dimension.



    fix



    one_hot = torch.zeros(1,18,1,1, dtype=torch.float)  # create the tensor with all singleton dimensions in place
    one_hot[0,1,0,0] = 1.
    one_hot.expand(-1,-1,40,40)





    share|improve this answer




























      1














      expand works along singleton dimensions of the input tensor. In your example, you are trying to expand a 1-by-18 tensor along its (non-existent) third and fourth dimensions - this is why you are getting an error. The only singleton dimension (=dimension with size==1) you have is the first dimension.



      fix



      one_hot = torch.zeros(1,18,1,1, dtype=torch.float)  # create the tensor with all singleton dimensions in place
      one_hot[0,1,0,0] = 1.
      one_hot.expand(-1,-1,40,40)





      share|improve this answer


























        1












        1








        1







        expand works along singleton dimensions of the input tensor. In your example, you are trying to expand a 1-by-18 tensor along its (non-existent) third and fourth dimensions - this is why you are getting an error. The only singleton dimension (=dimension with size==1) you have is the first dimension.



        fix



        one_hot = torch.zeros(1,18,1,1, dtype=torch.float)  # create the tensor with all singleton dimensions in place
        one_hot[0,1,0,0] = 1.
        one_hot.expand(-1,-1,40,40)





        share|improve this answer













        expand works along singleton dimensions of the input tensor. In your example, you are trying to expand a 1-by-18 tensor along its (non-existent) third and fourth dimensions - this is why you are getting an error. The only singleton dimension (=dimension with size==1) you have is the first dimension.



        fix



        one_hot = torch.zeros(1,18,1,1, dtype=torch.float)  # create the tensor with all singleton dimensions in place
        one_hot[0,1,0,0] = 1.
        one_hot.expand(-1,-1,40,40)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 30 '18 at 5:56









        ShaiShai

        69.5k22136243




        69.5k22136243






























            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%2f53975352%2fusing-expand-dims-in-pytorch%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

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas