-
Notifications
You must be signed in to change notification settings - Fork 432
Is there a way to convert a 1x1 2D Tensor to scalar? #19
Comments
You can not direct read scalar from GPU tensor. Currently, one thing you can possibly do is store the result to a Tensor2D and copy to CPU to read it. There was a performance reason why we did not support summing over all the elements in GPU. If you are implementing neural nets, usually copy to CPU to evaluate the statistics won't harm the overall performance, because this was a cheap operation |
Hi tq, for(unsigned int i = 0 ; i < outgrad.shape[1] ; i ++){ Thanks a lot ! |
If it was a 2D tensor, remember C++ index starts from 0, so it should be shape[1] and shape[0].. If you use master version of mshadow.
The shape convention in master was reversed from numpy and a bit weird. There is going to be a major refactor to solve the problem |
so shape[0] is the number of columns in a 2D tensor what does it mean by "the lowest dimension" Thanks! |
Sorry about the confusion, this is a deprecated convention that will be changed. For temporal solution, you can use tensor.size(i), updated in master, which follows the same convention as numpy. I have updated the example |
Got it. Thanks ! |
you are welcome |
Hi,
My code looks something like this which is computing the square error of NN.
real_t = (sumall_except_dim<2>(sum_rows(F(outgrad)) / (real_t) pred.shape[1];
however, this seems not working with the following error msg.
error: cannot convert 'mshadow::expr::BinaryMapExp<mshadow::op::div, mshadow::expr::ReduceTo1DExp<mshadow::expr::ReduceTo1DExp<mshadow::expr::UnaryMapExp<square, mshadow::Tensor<mshadow::cpu, 2>, 1>, mshadow::red::sum, 0>, mshadow::red::sum, 2>, mshadow::expr::ScalarExp, 3>' to 'mshadow::real_t {aka float}' in initialization
real_t err = (sumall_except_dim<2>(sum_rows(F(outgrad))) / (real_t)pred.shape[1]);
which basically says the type is different.
I've also tried
(sumall_except_dim<2>(sum_rows(F(outgrad))[0]
but it says there is no operator[] defined on such data type.
It seems that I need to explicit go over the tensor and sum the result(which could be slow compared to optimized vectorized code)?
Thank you.
The text was updated successfully, but these errors were encountered: