Search code examples
python-3.xchainercupy

Difference in the array type using numpy and cupy


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?


Solution

  • 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]))