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?
python-3.x chainer cupy
add a comment |
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
add a comment |
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
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
python-3.x chainer cupy
edited Nov 21 '18 at 16:41
talonmies
59.7k17129199
59.7k17129199
asked Nov 21 '18 at 15:24
Gurpreet.SGurpreet.S
626
626
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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]))
add a comment |
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]
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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]))
add a comment |
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]))
add a comment |
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]))
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]))
answered Nov 22 '18 at 3:21
tostos
462
462
add a comment |
add a comment |
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]
add a comment |
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]
add a comment |
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]
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]
edited Nov 23 '18 at 7:44
answered Nov 22 '18 at 7:12
Yuki HashimotoYuki Hashimoto
6159
6159
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 22 '18 at 1:04
corochanncorochann
1,2051619
1,2051619
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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