Row-wise Element Indexing in PyTorch for C++












2















I am using the C++ frontend for PyTorch and am struggling with a relatively basic indexing problem.



I have an 8 by 6 Tensor such as the one below:



[ Variable[CUDAFloatType]{8,6} ] 
0 1 2 3 4 5
0 1.7107e-14 4.0448e-17 4.9708e-06 1.1664e-08 9.9999e-01 2.1857e-20
1 1.8288e-14 5.9356e-17 5.3042e-06 1.2369e-08 9.9999e-01 2.4799e-20
2 2.6828e-04 9.0390e-18 1.7517e-02 1.0529e-03 9.8116e-01 6.7854e-26
3 5.7521e-10 3.1037e-11 1.5021e-03 1.2304e-06 9.9850e-01 1.4888e-17
4 1.7811e-13 1.8383e-15 1.6733e-05 3.8466e-08 9.9998e-01 5.2815e-20
5 9.6191e-06 2.6217e-23 3.1345e-02 2.3024e-04 9.6842e-01 2.9435e-34
6 2.2653e-04 8.4642e-18 1.6085e-02 9.7405e-04 9.8271e-01 6.3059e-26
7 3.8951e-14 2.9903e-16 8.3518e-06 1.7974e-08 9.9999e-01 3.6993e-20


I have another Tensor with just 8 elements in it such as:



[ Variable[CUDALongType]{8} ] 
0
3
4
4
4
4
4
4


I would like to index the rows of my first tensor using the second to produce:



        0           
0 1.7107e-14
1 1.2369e-08
2 9.8116e-01
3 9.9850e-01
4 9.9998e-01
5 9.6842e-01
6 9.8271e-01
7 9.9999e-01


I have tried a few different approaches including index_select but it seems to produce an output that has the same dimensions as the input (8x6).



In Python I think I could index with Python's built-in indexing as discussed here: https://github.com/pytorch/pytorch/issues/1080



Unfortunately, in C++ I can only index a Tensor with a scalar (zero-dimensional Tensor) so I don't think that approach works for me here.



How can I achieve my desired result without resorting to loops?










share|improve this question




















  • 2





    you want to look at pytorch.org/docs/stable/torch.html#torch.gather instead of index_select

    – Shai
    Nov 27 '18 at 22:02
















2















I am using the C++ frontend for PyTorch and am struggling with a relatively basic indexing problem.



I have an 8 by 6 Tensor such as the one below:



[ Variable[CUDAFloatType]{8,6} ] 
0 1 2 3 4 5
0 1.7107e-14 4.0448e-17 4.9708e-06 1.1664e-08 9.9999e-01 2.1857e-20
1 1.8288e-14 5.9356e-17 5.3042e-06 1.2369e-08 9.9999e-01 2.4799e-20
2 2.6828e-04 9.0390e-18 1.7517e-02 1.0529e-03 9.8116e-01 6.7854e-26
3 5.7521e-10 3.1037e-11 1.5021e-03 1.2304e-06 9.9850e-01 1.4888e-17
4 1.7811e-13 1.8383e-15 1.6733e-05 3.8466e-08 9.9998e-01 5.2815e-20
5 9.6191e-06 2.6217e-23 3.1345e-02 2.3024e-04 9.6842e-01 2.9435e-34
6 2.2653e-04 8.4642e-18 1.6085e-02 9.7405e-04 9.8271e-01 6.3059e-26
7 3.8951e-14 2.9903e-16 8.3518e-06 1.7974e-08 9.9999e-01 3.6993e-20


I have another Tensor with just 8 elements in it such as:



[ Variable[CUDALongType]{8} ] 
0
3
4
4
4
4
4
4


I would like to index the rows of my first tensor using the second to produce:



        0           
0 1.7107e-14
1 1.2369e-08
2 9.8116e-01
3 9.9850e-01
4 9.9998e-01
5 9.6842e-01
6 9.8271e-01
7 9.9999e-01


I have tried a few different approaches including index_select but it seems to produce an output that has the same dimensions as the input (8x6).



In Python I think I could index with Python's built-in indexing as discussed here: https://github.com/pytorch/pytorch/issues/1080



Unfortunately, in C++ I can only index a Tensor with a scalar (zero-dimensional Tensor) so I don't think that approach works for me here.



How can I achieve my desired result without resorting to loops?










share|improve this question




















  • 2





    you want to look at pytorch.org/docs/stable/torch.html#torch.gather instead of index_select

    – Shai
    Nov 27 '18 at 22:02














2












2








2








I am using the C++ frontend for PyTorch and am struggling with a relatively basic indexing problem.



I have an 8 by 6 Tensor such as the one below:



[ Variable[CUDAFloatType]{8,6} ] 
0 1 2 3 4 5
0 1.7107e-14 4.0448e-17 4.9708e-06 1.1664e-08 9.9999e-01 2.1857e-20
1 1.8288e-14 5.9356e-17 5.3042e-06 1.2369e-08 9.9999e-01 2.4799e-20
2 2.6828e-04 9.0390e-18 1.7517e-02 1.0529e-03 9.8116e-01 6.7854e-26
3 5.7521e-10 3.1037e-11 1.5021e-03 1.2304e-06 9.9850e-01 1.4888e-17
4 1.7811e-13 1.8383e-15 1.6733e-05 3.8466e-08 9.9998e-01 5.2815e-20
5 9.6191e-06 2.6217e-23 3.1345e-02 2.3024e-04 9.6842e-01 2.9435e-34
6 2.2653e-04 8.4642e-18 1.6085e-02 9.7405e-04 9.8271e-01 6.3059e-26
7 3.8951e-14 2.9903e-16 8.3518e-06 1.7974e-08 9.9999e-01 3.6993e-20


I have another Tensor with just 8 elements in it such as:



[ Variable[CUDALongType]{8} ] 
0
3
4
4
4
4
4
4


I would like to index the rows of my first tensor using the second to produce:



        0           
0 1.7107e-14
1 1.2369e-08
2 9.8116e-01
3 9.9850e-01
4 9.9998e-01
5 9.6842e-01
6 9.8271e-01
7 9.9999e-01


I have tried a few different approaches including index_select but it seems to produce an output that has the same dimensions as the input (8x6).



In Python I think I could index with Python's built-in indexing as discussed here: https://github.com/pytorch/pytorch/issues/1080



Unfortunately, in C++ I can only index a Tensor with a scalar (zero-dimensional Tensor) so I don't think that approach works for me here.



How can I achieve my desired result without resorting to loops?










share|improve this question
















I am using the C++ frontend for PyTorch and am struggling with a relatively basic indexing problem.



I have an 8 by 6 Tensor such as the one below:



[ Variable[CUDAFloatType]{8,6} ] 
0 1 2 3 4 5
0 1.7107e-14 4.0448e-17 4.9708e-06 1.1664e-08 9.9999e-01 2.1857e-20
1 1.8288e-14 5.9356e-17 5.3042e-06 1.2369e-08 9.9999e-01 2.4799e-20
2 2.6828e-04 9.0390e-18 1.7517e-02 1.0529e-03 9.8116e-01 6.7854e-26
3 5.7521e-10 3.1037e-11 1.5021e-03 1.2304e-06 9.9850e-01 1.4888e-17
4 1.7811e-13 1.8383e-15 1.6733e-05 3.8466e-08 9.9998e-01 5.2815e-20
5 9.6191e-06 2.6217e-23 3.1345e-02 2.3024e-04 9.6842e-01 2.9435e-34
6 2.2653e-04 8.4642e-18 1.6085e-02 9.7405e-04 9.8271e-01 6.3059e-26
7 3.8951e-14 2.9903e-16 8.3518e-06 1.7974e-08 9.9999e-01 3.6993e-20


I have another Tensor with just 8 elements in it such as:



[ Variable[CUDALongType]{8} ] 
0
3
4
4
4
4
4
4


I would like to index the rows of my first tensor using the second to produce:



        0           
0 1.7107e-14
1 1.2369e-08
2 9.8116e-01
3 9.9850e-01
4 9.9998e-01
5 9.6842e-01
6 9.8271e-01
7 9.9999e-01


I have tried a few different approaches including index_select but it seems to produce an output that has the same dimensions as the input (8x6).



In Python I think I could index with Python's built-in indexing as discussed here: https://github.com/pytorch/pytorch/issues/1080



Unfortunately, in C++ I can only index a Tensor with a scalar (zero-dimensional Tensor) so I don't think that approach works for me here.



How can I achieve my desired result without resorting to loops?







c++ pytorch






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 19:53







JoshVarty

















asked Nov 27 '18 at 19:45









JoshVartyJoshVarty

5,59633357




5,59633357








  • 2





    you want to look at pytorch.org/docs/stable/torch.html#torch.gather instead of index_select

    – Shai
    Nov 27 '18 at 22:02














  • 2





    you want to look at pytorch.org/docs/stable/torch.html#torch.gather instead of index_select

    – Shai
    Nov 27 '18 at 22:02








2




2





you want to look at pytorch.org/docs/stable/torch.html#torch.gather instead of index_select

– Shai
Nov 27 '18 at 22:02





you want to look at pytorch.org/docs/stable/torch.html#torch.gather instead of index_select

– Shai
Nov 27 '18 at 22:02












1 Answer
1






active

oldest

votes


















2














It turns out you can do this in a couple different ways. One with gather and one with index. From the PyTorch discussions where I asked the same question:



Using torch::gather



auto x = torch::randn({8, 6});
int64_t idx_data[8] = { 0, 3, 4, 4, 4, 4, 4, 4 };
auto idx = x.type().toScalarType(torch::kLong).tensorFromBlob(idx_data, 8);
auto result = x.gather(1, idx.unsqueeze(1));


Using the C++ specific torch::index



auto x = torch::randn({8, 6});
int64_t idx_data[8] = { 0, 3, 4, 4, 4, 4, 4, 4 };
auto idx = x.type().toScalarType(torch::kLong).tensorFromBlob(idx_data, 8);
auto rows = torch::arange(0, x.size(0), torch::kLong);
auto result = x.index({rows, idx});





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%2f53507039%2frow-wise-element-indexing-in-pytorch-for-c%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









    2














    It turns out you can do this in a couple different ways. One with gather and one with index. From the PyTorch discussions where I asked the same question:



    Using torch::gather



    auto x = torch::randn({8, 6});
    int64_t idx_data[8] = { 0, 3, 4, 4, 4, 4, 4, 4 };
    auto idx = x.type().toScalarType(torch::kLong).tensorFromBlob(idx_data, 8);
    auto result = x.gather(1, idx.unsqueeze(1));


    Using the C++ specific torch::index



    auto x = torch::randn({8, 6});
    int64_t idx_data[8] = { 0, 3, 4, 4, 4, 4, 4, 4 };
    auto idx = x.type().toScalarType(torch::kLong).tensorFromBlob(idx_data, 8);
    auto rows = torch::arange(0, x.size(0), torch::kLong);
    auto result = x.index({rows, idx});





    share|improve this answer




























      2














      It turns out you can do this in a couple different ways. One with gather and one with index. From the PyTorch discussions where I asked the same question:



      Using torch::gather



      auto x = torch::randn({8, 6});
      int64_t idx_data[8] = { 0, 3, 4, 4, 4, 4, 4, 4 };
      auto idx = x.type().toScalarType(torch::kLong).tensorFromBlob(idx_data, 8);
      auto result = x.gather(1, idx.unsqueeze(1));


      Using the C++ specific torch::index



      auto x = torch::randn({8, 6});
      int64_t idx_data[8] = { 0, 3, 4, 4, 4, 4, 4, 4 };
      auto idx = x.type().toScalarType(torch::kLong).tensorFromBlob(idx_data, 8);
      auto rows = torch::arange(0, x.size(0), torch::kLong);
      auto result = x.index({rows, idx});





      share|improve this answer


























        2












        2








        2







        It turns out you can do this in a couple different ways. One with gather and one with index. From the PyTorch discussions where I asked the same question:



        Using torch::gather



        auto x = torch::randn({8, 6});
        int64_t idx_data[8] = { 0, 3, 4, 4, 4, 4, 4, 4 };
        auto idx = x.type().toScalarType(torch::kLong).tensorFromBlob(idx_data, 8);
        auto result = x.gather(1, idx.unsqueeze(1));


        Using the C++ specific torch::index



        auto x = torch::randn({8, 6});
        int64_t idx_data[8] = { 0, 3, 4, 4, 4, 4, 4, 4 };
        auto idx = x.type().toScalarType(torch::kLong).tensorFromBlob(idx_data, 8);
        auto rows = torch::arange(0, x.size(0), torch::kLong);
        auto result = x.index({rows, idx});





        share|improve this answer













        It turns out you can do this in a couple different ways. One with gather and one with index. From the PyTorch discussions where I asked the same question:



        Using torch::gather



        auto x = torch::randn({8, 6});
        int64_t idx_data[8] = { 0, 3, 4, 4, 4, 4, 4, 4 };
        auto idx = x.type().toScalarType(torch::kLong).tensorFromBlob(idx_data, 8);
        auto result = x.gather(1, idx.unsqueeze(1));


        Using the C++ specific torch::index



        auto x = torch::randn({8, 6});
        int64_t idx_data[8] = { 0, 3, 4, 4, 4, 4, 4, 4 };
        auto idx = x.type().toScalarType(torch::kLong).tensorFromBlob(idx_data, 8);
        auto rows = torch::arange(0, x.size(0), torch::kLong);
        auto result = x.index({rows, idx});






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 29 '18 at 5:16









        JoshVartyJoshVarty

        5,59633357




        5,59633357
































            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%2f53507039%2frow-wise-element-indexing-in-pytorch-for-c%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

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

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