1

I'm facing "slice indices must be integers" error while using tf.map_fn when I use the index variable to reference an image array in tensorflow. If I don't use index variable and hardcode the value, the result comes alright. Following are the details:

print(img_x.shape)
xm = tf.map_fn(lambda i: img_x[i:i+end_range], tf.range(100), dtype=tf.float32)

output:

(4800,)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-62-15b6c15c4e0f> in <module>()
     28 img_x = np.squeeze(img_x)
     29 print(img_x.shape)
---> 30 xm = tf.map_fn(lambda i: img_x[i:i+end_range], tf.range(100), dtype=tf.float32)
     31 # sess2 = tf.Session()
     32 # xm.eval(session=sess2)

~/anaconda/envs/py36/lib/python3.6/site-packages/tensorflow/python/ops/functional_ops.py in map_fn(fn, elems, dtype, parallel_iterations, back_prop, swap_memory, infer_shape, name)
    411         parallel_iterations=parallel_iterations,
    412         back_prop=back_prop,
--> 413         swap_memory=swap_memory)
    414     results_flat = [r.stack() for r in r_a]
    415 

~/anaconda/envs/py36/lib/python3.6/site-packages/tensorflow/python/ops/control_flow_ops.py in while_loop(cond, body, loop_vars, shape_invariants, parallel_iterations, back_prop, swap_memory, name, maximum_iterations)
   3094         swap_memory=swap_memory)
   3095     ops.add_to_collection(ops.GraphKeys.WHILE_CONTEXT, loop_context)
-> 3096     result = loop_context.BuildLoop(cond, body, loop_vars, shape_invariants)
   3097     if maximum_iterations is not None:
   3098       return result[1]

~/anaconda/envs/py36/lib/python3.6/site-packages/tensorflow/python/ops/control_flow_ops.py in BuildLoop(self, pred, body, loop_vars, shape_invariants)
   2872       self.Enter()
   2873       original_body_result, exit_vars = self._BuildLoop(
-> 2874           pred, body, original_loop_vars, loop_vars, shape_invariants)
   2875     finally:
   2876       self.Exit()

~/anaconda/envs/py36/lib/python3.6/site-packages/tensorflow/python/ops/control_flow_ops.py in _BuildLoop(self, pred, body, original_loop_vars, loop_vars, shape_invariants)
   2812         flat_sequence=vars_for_body_with_tensor_arrays)
   2813     pre_summaries = ops.get_collection(ops.GraphKeys._SUMMARY_COLLECTION)  # pylint: disable=protected-access
-> 2814     body_result = body(*packed_vars_for_body)
   2815     post_summaries = ops.get_collection(ops.GraphKeys._SUMMARY_COLLECTION)  # pylint: disable=protected-access
   2816     if not nest.is_sequence(body_result):

~/anaconda/envs/py36/lib/python3.6/site-packages/tensorflow/python/ops/functional_ops.py in compute(i, tas)
    401       """
    402       packed_values = input_pack([elem_ta.read(i) for elem_ta in elems_ta])
--> 403       packed_fn_values = fn(packed_values)
    404       nest.assert_same_structure(dtype or elems, packed_fn_values)
    405       flat_fn_values = output_flatten(packed_fn_values)

<ipython-input-62-15b6c15c4e0f> in <lambda>(i)
     28 img_x = np.squeeze(img_x)
     29 print(img_x.shape)
---> 30 xm = tf.map_fn(lambda i: img_x[i:i+end_range], tf.range(100), dtype=tf.float32)
     31 # sess2 = tf.Session()
     32 # xm.eval(session=sess2)

TypeError: slice indices must be integers or None or have an __index__ method

However, if I hardcode the i variable, the program doesn't crash:

xm = tf.map_fn(lambda i: img_x[0:0+end_range], tf.range(100), dtype=tf.float32)

I'd appreciate any help. Thanks.

EDIT it seems img_x was a numpy array and that was the reason it wasn't working. I converted img_x to a tensor using tf.convert_to_tensor and things worked nicely. Thanks.

Ahsan
  • 551
  • 2
  • 5
  • 18
  • Yes, right, but does range not pass integers? I'm assuming dtype=tf.float32 in tf.map_fn is the type of the contents of the return value? – Ahsan Jul 29 '18 at 10:41
  • I think you're right, not sure why it doesn't work...can you shrink the example down to something self-contained (e.g. by providing an example `img_x`)? – John Zwinck Jul 29 '18 at 10:49
  • No, `i` is a `tf.Tensor` as you will find if you attempt to cast through `tf.map_fn(lambda i: img_x[int(i):int(i)+end_range], tf.range(100), dtype=tf.float32)`. – fuglede Jul 29 '18 at 10:50
  • @fuglede just tried this and yes you are right it's a tensor, so then what's the solution to this? Can a Tensor be used as an index to reference an array? – Ahsan Jul 29 '18 at 10:59
  • 1
    @Ahsan: Does it have to be a TensorFlow operation? If you can live with this being produced in NumPy, one solution would be that given in [this answer](https://stackoverflow.com/a/22972886/5085211) (which in your case would boil down to `subsequences(img_x, end_range)[:, :100]` or simply `as_strided(img_x, shape=(5, 100), strides=(8, 8))`). – fuglede Jul 29 '18 at 11:00
  • Or, rather, `shape=(end_range, 100)`, and `strides=(img_x.itemsize, img_x.itemsize)` in case you aren't dealing with doubles. – fuglede Jul 29 '18 at 11:06

0 Answers0