I'm trying to operate on a list of tensors in a method using a for loop. My goal is to have this loop work as part of a @tf.function. The loop works as epected when running in eager mode, but I get the following error when decorating the method with @tf.function. I am pretty new to Tensorflow. Any help is apprecaited. Thank you!
Code below is written using TensorFlow 2.14
import tensorflow as tf
a = tf.constant([1.,2.])
b = tf.constant([[3.,4.],[5.,6.],[7.,8.]])
tensor_list = [a,b]
multiplier = 2.
@tf.function
def FiniteCheck(list, x):
isFinite=tf.constant(True)
for i in tf.range(len(list)):
isFinite = tf.math.reduce_all(tf.math.is_finite(tf.multiply(list[i], x)))
if tf.logical_not(isFinite):
break
return isFinite
b_Finite = FiniteCheck(tensor_list, multiplier)
Error being thrown by Tensorflow is here:
File "/Users/xxxx/Desktop/SandBox.py", line 14, in FiniteCheck *
isFinite = tf.math.reduce_all(tf.math.is_finite(tf.multiply(list[i], x)))
TypeError: list indices must be integers or slices, not SymbolicTensor
File "/var/folders/p1/gftjm5s15mxbxy97573_vn_m0000gn/T/__autograph_generated_fileteduovqt.py", line 49, in tf__FiniteCheck
ag__.for_stmt(ag__.converted_call(ag__.ld(tf).range, (ag__.converted_call(ag__.ld(len), (ag__.ld(list),), None, fscope),), None, fscope), extra_test, loop_body, get_state_1, set_state_1, ('isFinite', 'break_'), {'iterate_names': 'i'})
File "/var/folders/p1/gftjm5s15mxbxy97573_vn_m0000gn/T/__autograph_generated_fileteduovqt.py", line 25, in loop_body
isFinite = ag__.converted_call(ag__.ld(tf).math.reduce_all, (ag__.converted_call(ag__.ld(tf).math.is_finite, (ag__.converted_call(ag__.ld(tf).multiply, (ag__.ld(list)[ag__.ld(i)], ag__.ld(x)), None, fscope),), None, fscope),), None, fscope)
TypeError: list indices must be integers or slices, not SymbolicTensor
During handling of the above exception, another exception occurred:
File /var/folders/p1/gftjm5s15mxbxy97573_vn_m0000gn/T/__autograph_generated_fileteduovqt.py", line 25, in loop_body
isFinite = ag__.converted_call(ag__.ld(tf).math.reduce_all, (ag__.converted_call(ag__.ld(tf).math.is_finite, (ag__.converted_call(ag__.ld(tf).multiply, (ag__.ld(list)[ag__.ld(i)], ag__.ld(x)), None, fscope),), None, fscope),), None, fscope)
File "/var/folders/p1/gftjm5s15mxbxy97573_vn_m0000gn/T/__autograph_generated_fileteduovqt.py", line 49, in tf__FiniteCheck
ag__.for_stmt(ag__.converted_call(ag__.ld(tf).range, (ag__.converted_call(ag__.ld(len), (ag__.ld(list),), None, fscope),), None, fscope), extra_test, loop_body, get_state_1, set_state_1, ('isFinite', 'break_'), {'iterate_names': 'i'})
File "/Users/chris/Desktop/SandBox.py", line 21, in <module>
b_Finite = FiniteCheck(tensor_list, multiplier)
TypeError: in user code:
File "/Users/xxxx/Desktop/SandBox.py", line 14, in FiniteCheck *
isFinite = tf.math.reduce_all(tf.math.is_finite(tf.multiply(list[i], x))
)
TypeError: list indices must be integers or slices, not SymbolicTensor
I tried creating a tensor of tensors using tensorflow stacks or ragged tensors and it did not work with different rank tensors. I believe the solution is to get tensor_list to be a tensor of tensors so the index i, which is of type tensor, can be used to access items in the list.