Difference in the array type using numpy and cupy












2















I am using chainer library for my model and facing the below issue:
Say I have a file of test data having 3 features and a label (last column) with them. It is imported in the form of a list.
e.g.



test_set = [[1,0,9,1],[7,0,8,1],[7,0,2,0],[8,0,1,0]]


Then I take the labels by converting the data into a numpy array and taking the labels column,
which I later convert into a list for comparison with the predicted labels say y_pred = [1,1,1,0]. i.e.



import numpy as np
y_true_np = list(np.array(test_set)[:,3])
print(y_true_np)
[1, 1, 0, 0]


My concern is when I run my model in GPU, it uses Cuda.cupy instead of numpy as I am using chainer library, and when I fetch the true labels I receive them as:



There by in cupy:



import cupy as cp
y_true_cp = list(cp.array(test_set)[:,3]) Or
y_true_cp = list(cuda.cp.array(test_set)[:,3])


Both returns a list of array:



y_true_cp: [array(1), array(1), array(0), array(0)]


As a workaround, I am using numpy in that specific place. Am I doing something wrong while using cupy, due to which I am not getting the values correctly?










share|improve this question





























    2















    I am using chainer library for my model and facing the below issue:
    Say I have a file of test data having 3 features and a label (last column) with them. It is imported in the form of a list.
    e.g.



    test_set = [[1,0,9,1],[7,0,8,1],[7,0,2,0],[8,0,1,0]]


    Then I take the labels by converting the data into a numpy array and taking the labels column,
    which I later convert into a list for comparison with the predicted labels say y_pred = [1,1,1,0]. i.e.



    import numpy as np
    y_true_np = list(np.array(test_set)[:,3])
    print(y_true_np)
    [1, 1, 0, 0]


    My concern is when I run my model in GPU, it uses Cuda.cupy instead of numpy as I am using chainer library, and when I fetch the true labels I receive them as:



    There by in cupy:



    import cupy as cp
    y_true_cp = list(cp.array(test_set)[:,3]) Or
    y_true_cp = list(cuda.cp.array(test_set)[:,3])


    Both returns a list of array:



    y_true_cp: [array(1), array(1), array(0), array(0)]


    As a workaround, I am using numpy in that specific place. Am I doing something wrong while using cupy, due to which I am not getting the values correctly?










    share|improve this question



























      2












      2








      2








      I am using chainer library for my model and facing the below issue:
      Say I have a file of test data having 3 features and a label (last column) with them. It is imported in the form of a list.
      e.g.



      test_set = [[1,0,9,1],[7,0,8,1],[7,0,2,0],[8,0,1,0]]


      Then I take the labels by converting the data into a numpy array and taking the labels column,
      which I later convert into a list for comparison with the predicted labels say y_pred = [1,1,1,0]. i.e.



      import numpy as np
      y_true_np = list(np.array(test_set)[:,3])
      print(y_true_np)
      [1, 1, 0, 0]


      My concern is when I run my model in GPU, it uses Cuda.cupy instead of numpy as I am using chainer library, and when I fetch the true labels I receive them as:



      There by in cupy:



      import cupy as cp
      y_true_cp = list(cp.array(test_set)[:,3]) Or
      y_true_cp = list(cuda.cp.array(test_set)[:,3])


      Both returns a list of array:



      y_true_cp: [array(1), array(1), array(0), array(0)]


      As a workaround, I am using numpy in that specific place. Am I doing something wrong while using cupy, due to which I am not getting the values correctly?










      share|improve this question
















      I am using chainer library for my model and facing the below issue:
      Say I have a file of test data having 3 features and a label (last column) with them. It is imported in the form of a list.
      e.g.



      test_set = [[1,0,9,1],[7,0,8,1],[7,0,2,0],[8,0,1,0]]


      Then I take the labels by converting the data into a numpy array and taking the labels column,
      which I later convert into a list for comparison with the predicted labels say y_pred = [1,1,1,0]. i.e.



      import numpy as np
      y_true_np = list(np.array(test_set)[:,3])
      print(y_true_np)
      [1, 1, 0, 0]


      My concern is when I run my model in GPU, it uses Cuda.cupy instead of numpy as I am using chainer library, and when I fetch the true labels I receive them as:



      There by in cupy:



      import cupy as cp
      y_true_cp = list(cp.array(test_set)[:,3]) Or
      y_true_cp = list(cuda.cp.array(test_set)[:,3])


      Both returns a list of array:



      y_true_cp: [array(1), array(1), array(0), array(0)]


      As a workaround, I am using numpy in that specific place. Am I doing something wrong while using cupy, due to which I am not getting the values correctly?







      python-3.x chainer cupy






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 16:41









      talonmies

      59.7k17129199




      59.7k17129199










      asked Nov 21 '18 at 15:24









      Gurpreet.SGurpreet.S

      626




      626
























          3 Answers
          3






          active

          oldest

          votes


















          3














          While NumPy converts 0-dimensional arrays to scalars, CuPy does not.
          https://docs-cupy.chainer.org/en/stable/reference/difference.html#zero-dimensional-array



          In the result [array(1), array(1), array(0), array(0)], each data of arrays is on GPU. I'd use cupy.asnumpy if an efficient CPU array is needed.



          y_true_cp = list(cp.asnumpy(cp.array(test_set)[:,3]))






          share|improve this answer































            1














            There is no necessity to go through numpy.



            Input



            import cupy as cp

            test_set = [[1,0,9,1],[7,0,8,1],[7,0,2,0],[8,0,1,0]]
            test_set = cp.array(test_set)

            x_true = test_set[:, :3]
            y_true = test_set[:, 3]
            print("x_true:n".format(x_true))
            print("y_true:n".format(y_true))


            Output



            x_true:
            [[1 0 9]
            [7 0 8]
            [7 0 2]
            [8 0 1]]
            y_true:
            [1 1 0 0]





            share|improve this answer

































              0














              As you wrote, it seems the behavior when you wrap by list is different



              import numpy as np
              import cupy as cp

              print(list(np.arange(3)) # --> [0, 1, 2]
              print(list(cp.arange(3)) # --> [array(0), array(1), array(2)]


              However in your case, I think you can just use numpy array or cupy array without converting list.



              y_true = test_set[:, 3]  # it should work for both numpy & cupy
              y_true_np = cuda.to_cpu(y_true) # If you want to convert the array to numpy





              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%2f53415284%2fdifference-in-the-array-type-using-numpy-and-cupy%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                3














                While NumPy converts 0-dimensional arrays to scalars, CuPy does not.
                https://docs-cupy.chainer.org/en/stable/reference/difference.html#zero-dimensional-array



                In the result [array(1), array(1), array(0), array(0)], each data of arrays is on GPU. I'd use cupy.asnumpy if an efficient CPU array is needed.



                y_true_cp = list(cp.asnumpy(cp.array(test_set)[:,3]))






                share|improve this answer




























                  3














                  While NumPy converts 0-dimensional arrays to scalars, CuPy does not.
                  https://docs-cupy.chainer.org/en/stable/reference/difference.html#zero-dimensional-array



                  In the result [array(1), array(1), array(0), array(0)], each data of arrays is on GPU. I'd use cupy.asnumpy if an efficient CPU array is needed.



                  y_true_cp = list(cp.asnumpy(cp.array(test_set)[:,3]))






                  share|improve this answer


























                    3












                    3








                    3







                    While NumPy converts 0-dimensional arrays to scalars, CuPy does not.
                    https://docs-cupy.chainer.org/en/stable/reference/difference.html#zero-dimensional-array



                    In the result [array(1), array(1), array(0), array(0)], each data of arrays is on GPU. I'd use cupy.asnumpy if an efficient CPU array is needed.



                    y_true_cp = list(cp.asnumpy(cp.array(test_set)[:,3]))






                    share|improve this answer













                    While NumPy converts 0-dimensional arrays to scalars, CuPy does not.
                    https://docs-cupy.chainer.org/en/stable/reference/difference.html#zero-dimensional-array



                    In the result [array(1), array(1), array(0), array(0)], each data of arrays is on GPU. I'd use cupy.asnumpy if an efficient CPU array is needed.



                    y_true_cp = list(cp.asnumpy(cp.array(test_set)[:,3]))







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 22 '18 at 3:21









                    tostos

                    462




                    462

























                        1














                        There is no necessity to go through numpy.



                        Input



                        import cupy as cp

                        test_set = [[1,0,9,1],[7,0,8,1],[7,0,2,0],[8,0,1,0]]
                        test_set = cp.array(test_set)

                        x_true = test_set[:, :3]
                        y_true = test_set[:, 3]
                        print("x_true:n".format(x_true))
                        print("y_true:n".format(y_true))


                        Output



                        x_true:
                        [[1 0 9]
                        [7 0 8]
                        [7 0 2]
                        [8 0 1]]
                        y_true:
                        [1 1 0 0]





                        share|improve this answer






























                          1














                          There is no necessity to go through numpy.



                          Input



                          import cupy as cp

                          test_set = [[1,0,9,1],[7,0,8,1],[7,0,2,0],[8,0,1,0]]
                          test_set = cp.array(test_set)

                          x_true = test_set[:, :3]
                          y_true = test_set[:, 3]
                          print("x_true:n".format(x_true))
                          print("y_true:n".format(y_true))


                          Output



                          x_true:
                          [[1 0 9]
                          [7 0 8]
                          [7 0 2]
                          [8 0 1]]
                          y_true:
                          [1 1 0 0]





                          share|improve this answer




























                            1












                            1








                            1







                            There is no necessity to go through numpy.



                            Input



                            import cupy as cp

                            test_set = [[1,0,9,1],[7,0,8,1],[7,0,2,0],[8,0,1,0]]
                            test_set = cp.array(test_set)

                            x_true = test_set[:, :3]
                            y_true = test_set[:, 3]
                            print("x_true:n".format(x_true))
                            print("y_true:n".format(y_true))


                            Output



                            x_true:
                            [[1 0 9]
                            [7 0 8]
                            [7 0 2]
                            [8 0 1]]
                            y_true:
                            [1 1 0 0]





                            share|improve this answer















                            There is no necessity to go through numpy.



                            Input



                            import cupy as cp

                            test_set = [[1,0,9,1],[7,0,8,1],[7,0,2,0],[8,0,1,0]]
                            test_set = cp.array(test_set)

                            x_true = test_set[:, :3]
                            y_true = test_set[:, 3]
                            print("x_true:n".format(x_true))
                            print("y_true:n".format(y_true))


                            Output



                            x_true:
                            [[1 0 9]
                            [7 0 8]
                            [7 0 2]
                            [8 0 1]]
                            y_true:
                            [1 1 0 0]






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 23 '18 at 7:44

























                            answered Nov 22 '18 at 7:12









                            Yuki HashimotoYuki Hashimoto

                            6159




                            6159























                                0














                                As you wrote, it seems the behavior when you wrap by list is different



                                import numpy as np
                                import cupy as cp

                                print(list(np.arange(3)) # --> [0, 1, 2]
                                print(list(cp.arange(3)) # --> [array(0), array(1), array(2)]


                                However in your case, I think you can just use numpy array or cupy array without converting list.



                                y_true = test_set[:, 3]  # it should work for both numpy & cupy
                                y_true_np = cuda.to_cpu(y_true) # If you want to convert the array to numpy





                                share|improve this answer




























                                  0














                                  As you wrote, it seems the behavior when you wrap by list is different



                                  import numpy as np
                                  import cupy as cp

                                  print(list(np.arange(3)) # --> [0, 1, 2]
                                  print(list(cp.arange(3)) # --> [array(0), array(1), array(2)]


                                  However in your case, I think you can just use numpy array or cupy array without converting list.



                                  y_true = test_set[:, 3]  # it should work for both numpy & cupy
                                  y_true_np = cuda.to_cpu(y_true) # If you want to convert the array to numpy





                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    As you wrote, it seems the behavior when you wrap by list is different



                                    import numpy as np
                                    import cupy as cp

                                    print(list(np.arange(3)) # --> [0, 1, 2]
                                    print(list(cp.arange(3)) # --> [array(0), array(1), array(2)]


                                    However in your case, I think you can just use numpy array or cupy array without converting list.



                                    y_true = test_set[:, 3]  # it should work for both numpy & cupy
                                    y_true_np = cuda.to_cpu(y_true) # If you want to convert the array to numpy





                                    share|improve this answer













                                    As you wrote, it seems the behavior when you wrap by list is different



                                    import numpy as np
                                    import cupy as cp

                                    print(list(np.arange(3)) # --> [0, 1, 2]
                                    print(list(cp.arange(3)) # --> [array(0), array(1), array(2)]


                                    However in your case, I think you can just use numpy array or cupy array without converting list.



                                    y_true = test_set[:, 3]  # it should work for both numpy & cupy
                                    y_true_np = cuda.to_cpu(y_true) # If you want to convert the array to numpy






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 22 '18 at 1:04









                                    corochanncorochann

                                    1,2051619




                                    1,2051619






























                                        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%2f53415284%2fdifference-in-the-array-type-using-numpy-and-cupy%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







                                        這個網誌中的熱門文章

                                        Xamarin.form Move up view when keyboard appear

                                        Post-Redirect-Get with Spring WebFlux and Thymeleaf

                                        Anylogic : not able to use stopDelay()