cgt
cgt copied to clipboard
-1 as an index for the last element in an axis doesn't work
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)]
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.
It's bad that it fails without throwing an exception.
From my point of view -1 is usual python way to take the last element. I suggest to support it.
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.)