joschu / cgt

Computation Graph Toolkit
Other
628 stars 87 forks source link

-1 as an index for the last element in an axis doesn't work #26

Open avostryakov opened 8 years ago

avostryakov commented 8 years ago

A minimum example to reproduce a bug: import cgt axis = 1 slices = -1 input_var = cgt.tensor3('input', dtype=np.float32) result = input_var[:, slices, :]

alternative variant of indexes

result = input_var[(slice(None),) * axis + (slices,)]

f = cgt.function([input_var], [result]) input = np.ones((3, 4, 5), dtype=np.float32) input[0, 3] = 3 print f(input)

output: [array([[ 0., 0., 0., 0., 0.], [ 3., 3., 3., 3., 3.], [ 1., 1., 1., 1., 1.]], dtype=float32)]

if I set slices = 3 which is the last element in axis=1 I get a correct answer: [array([[ 3., 3., 3., 3., 3.], [ 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1.]], dtype=float32)]

bstadie commented 8 years ago

Hi,

This is not a bug per se. Rather, cgt just doesn't support negative indexing at the moment. There has been discussion about adding it. We'll let you know if/when it makes it in.

joschu commented 8 years ago

It's bad that it fails without throwing an exception.

avostryakov commented 8 years ago

From my point of view -1 is usual python way to take the last element. I suggest to support it.

f0k commented 8 years ago

From my point of view -1 is usual python way to take the last element. I suggest to support it.

+1. (But throwing an exception would probably be enough for a short-term measure.)