deeplearningais / CUV

Matrix library for CUDA in C++ and Python
www.ais.uni-bonn.de
196 stars 48 forks source link

assigning scalar into a column of tensor using tensor_view does not work #4

Closed 0ri0nX closed 11 years ago

0ri0nX commented 11 years ago

Hi, I was trying to use tensor_view to assign specific scalar value into a column of a tensor (matrix). Below is the sample code. It tries to assign scalar 0 into the column 0. Instead, it assigns the value 0 into the row (and the number of values changed is equal to number of rows)

Can you fix it please? :)

Cheers, 0ri0nX

C++ code:

tensor<float,host_memory_space> w(5, 7);
tensor_view<float,host_memory_space> w0(w, indices[index_range()][index_range(0,1)]);
add_rnd_normal(w);

cout << "tView:\n" << w0 << endl;
w0 = 0.0f;
cout << "tView after assigning scalar 0:\n" << w0 << endl;

cout << "tensor after assigning scalar 0:\n" << w << endl;

Output:

tView: [[ -7.47294 ] [ -0.0319584 ] [ 0.140771 ] [ 0.0789871 ] [ -1.42737 ]] tView after assigning scalar 0: [[ 0 ] [ -0.0319584 ] [ 0.140771 ] [ 0.0789871 ] [ -1.42737 ]] tensor after assigning scalar 0: [[ 0 0 0 0 0 -1.33281 -1.32694 ] [ -0.0319584 0.0669318 -0.114206 -0.728161 0.959217 1.4891 0.171093 ] [ 0.140771 -1.6372 0.728912 -2.07714 -0.666776 -0.529197 1.15522 ] [ 0.0789871 1.40708 0.0385843 0.879348 -0.883604 1.20729 0.177587 ] [ -1.42737 0.897383 2.16767 -1.18338 1.24773 0.787216 -1.27652 ]]

temporaer commented 11 years ago

Dear 0ri0nX,

currently, most kernels only work on dense tensors (i.e. C- or Fortran-contiguous). One notable exception is the copy() function, which allows you to extract a subtensor if it is 2D-copyable (innermost dimension is strided).

In your case, you can do

w0 = w[indices[range()][range(0,1)]].copy(); w0 = 0.f; w[indices[range()][range(0,1)]] = w0;

Please refer to the new tutorial for more information:

http://www.ais.uni-bonn.de/deep_learning/tutorial.html

The current state (data structure supports generic strides, but kernels don't) is not satisfactory, but won't be fixed within the next weeks due to lack of time on my part.

-Hannes

----- Ursprüngliche Mail -----

Hi, I was trying to use tensor_view to assign specific scalar value into a column of a tensor (matrix). Below is the sample code. It tries to assign scalar 0 into the column 0. Instead, it assigns the value 0 into the row (and the number of values changed is equal to number of rows)

Can you fix it please? :)

Cheers, 0ri0nX

C++ code:

tensor<float,host_memory_space> w(5, 7);
tensor_view<float,host_memory_space> w0(w,
indices[index_range()][index_range(0,1)]);
add_rnd_normal(w);

cout << "tView:\n" << w0 << endl;
w0 = 0.0f;
cout << "tView after assigning scalar 0:\n" << w0 << endl;

cout << "tensor after assigning scalar 0:\n" << w << endl;

Output:

tView: [[ -7.47294 ] [ -0.0319584 ] [ 0.140771 ] [ 0.0789871 ] [ -1.42737 ]] tView after assigning scalar 0: [[ 0 ] [ -0.0319584 ] [ 0.140771 ] [ 0.0789871 ] [ -1.42737 ]] tensor after assigning scalar 0: [[ 0 0 0 0 0 -1.33281 -1.32694 ] [ -0.0319584 0.0669318 -0.114206 -0.728161 0.959217 1.4891 0.171093 ] [ 0.140771 -1.6372 0.728912 -2.07714 -0.666776 -0.529197 1.15522 ] [ 0.0789871 1.40708 0.0385843 0.879348 -0.883604 1.20729 0.177587 ] [ -1.42737 0.897383 2.16767 -1.18338 1.24773 0.787216 -1.27652 ]]


Reply to this email directly or view it on GitHub: https://github.com/deeplearningais/CUV/issues/4

0ri0nX commented 11 years ago

Than you for you reply.

I've tried it and it worked for w[indices[range()][range(0,2)]].copy(). But for w[indices[range()][range(0,1)]].copy() I got assert:

.../cuv/basics/tensor.hpp:380: cuv::linear_memory<V, M>::const_reference_type cuv::linear_memory<V, M>::operator[](const index_type&) const [with V = int, M = cuv::host_memory_space, cuv::linear_memory<V, M>::const_reference_type = const cuv::reference<int, cuv::host_memory_space, int>, cuv::linear_memory<V, M>::index_type = int]: Assertion `idx>=0' failed.

temporaer commented 11 years ago

ah, my bad. The ranges are exclusive, i.e. range(0,1) is empty. range(0,2) is the first column.

----- Ursprüngliche Mail -----

Than you for you reply.

I've tried it and it worked for w[indices[range()][range(0,2)]].copy(). But for w[indices[range()][range(0,1)]].copy() I got assert:

.../cuv/basics/tensor.hpp:380: cuv::linear_memory<V, M>::const_reference_type cuv::linear_memory<V, M>::operator[](const index_type&) const [with V = int, M = cuv::host_memory_space, cuv::linear_memory<V, M>::const_reference_type = const cuv::reference<int, cuv::host_memory_space, int>, cuv::linear_memory<V, M>::index_type = int]: Assertion `idx>=0' failed.


Reply to this email directly or view it on GitHub: https://github.com/deeplearningais/CUV/issues/4#issuecomment-17073763

0ri0nX commented 11 years ago

No, you were right, it is inclusive exclusive. range(0,1) is [1], range(0,2) is [0,1]. The previous example with range(0,2) resets 2 columns to 0 but with range(0,1) it asserts.

jiri

temporaer commented 11 years ago

I can't read whether you still believe that there is a bug. The ranges are, as you say, inclusive-exclusive, as in matlab or numpy. range(0,1) is invalid, since nothing is left.

However, for a vector [0,1,2,3,4], range(0,1) should be [0], not [1], and range(0,2) should be [0,1] as you say. Does this deviate from your tests?

----- Ursprüngliche Mail -----

No, you were right, it is inclusive exclusive. range(0,1) is [1], range(0,2) is [0,1]. The previous example with range(0,2) resets 2 columns to 0 but with range(0,1) it asserts.

jiri


Reply to this email directly or view it on GitHub: https://github.com/deeplearningais/CUV/issues/4#issuecomment-17074859

0ri0nX commented 11 years ago

It seems to me, there is a bug or I am doing something wrong.

I have a 2-dimensional tensor w. In the code below if I assign 2, 3, 4 .. to the variable X then it resets 2, 3, 4 ... first columns to zero. But 1 should be valid number as well and it should reset the first column to zero but it asserts on .../cuv/basics/tensor.hpp:380 Assertion `idx>=0' failed.

w0 = w[indices[index_range()][index_range(0,X)]].copy(); w0 = 0.f; w[indices[index_range()][index_range(0,X)]] = w0;

temporaer commented 11 years ago

OK, that sounds like a bug. I'll look into it, thanks a lot!

0ri0nX notifications@github.com schrieb:

It seems to me, there is a bug or I am doing something wrong.

I have a 2-dimensional tensor w. In the code below if I assign 2, 3, 4 .. to the variable X then it resets 2, 3, 4 ... first columns to zero. But 1 should be valid number as well and it should reset the first column to zero but it asserts on .../cuv/basics/tensor.hpp:380 Assertion `idx>=0' failed.

w0 = w[indices[index_range()][index_range(0,X)]].copy(); w0 = 0.f; w[indices[index_range()][index_range(0,X)]] = w0;

— Reply to this email directly or view it on GitHub.

temporaer commented 11 years ago

Should be fixed by 01f6d15c7297c17cd45cd114ba07a21604bf5bb8, thanks for the catch!