-
Notifications
You must be signed in to change notification settings - Fork 74k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tf.test.TestCase not working properly with tf.map_fn #17694
Comments
I've discovered a solution, which is always wrap your array of strings as a np.array, e.g. np.array(['123', 'abc']) instead of ['123', 'abc'] Would appreciate some clarity though on why one works and not the other. Thanks! |
In the code snippet provided, import tensorflow as tf
def identity_map(input):
return input
# Using tf.Session() and sess.run(). This runs without errors.
input = ['123', 'abc']
input_tensor = tf.placeholder(tf.string)
x = tf.map_fn(identity_map, input_tensor, dtype=tf.string)
x = tf.stack(x)
x = tf.reshape(tensor=x, shape=[-1])
with tf.Session() as sess:
result = sess.run(x, {input_tensor: input})
print(result) Writing the test the same way works as well: class SimpleTest(tf.test.TestCase):
def testMapString(self):
input = ['123', 'abc']
with self.test_session():
# Run map function
input_tensor = tf.placeholder(tf.string)
x = tf.map_fn(identity_map, input_tensor, dtype=tf.string)
x = tf.stack(x)
x = tf.reshape(tensor=x, shape=[-1])
result = x.eval(feed_dict={input_tensor:input})
print(result) The behavior you see in the test should be consistent with the behavior you see outside the test. Please do let me know if I'm mistaken. That said, coming to why you get an error with: tf.map_fn(identity_map, ['123', '456'], dtype=tf.string) but don't get the error with: tf.map_fn(identity_map, tf.convert_to_tensor(['123', '456']), dtype=tf.string) or tf.map_fn(identity_map, np.array(['123', '456']), dtype=tf.string) This is because in the latter two cases, the In the erroneous case, The error message could certainly be improved, and I'll try to do that. But hopefully the response makes sense? |
Fixes tensorflow#17694 Prior to this change, when tf.map_fn was provided with scalars, the error would be something like: Traceback (most recent call last): File "/tensorflow/python/kernel_tests/functional_ops_test.py", line 165, in testMapOverScalarErrors functional_ops.map_fn(lambda x: x, [1, 2]) File "/tensorflow/python/ops/functional_ops.py", line 368, in map_fn n = elems_flat[0].shape[0].value or array_ops.shape(elems_flat[0])[0] File "/tensorflow/python/framework/tensor_shape.py", line 609, in __getitem__ return self._dims[key] IndexError: list index out of range PiperOrigin-RevId: 191465183
Thanks, I think I got it. Basically, passing a list directly into sess.run() in a test_session() is treated as sequences of elements rather than a tensor of elements. |
System information
Describe the problem
When trying to run tf.test.TestCase with a tensor of strings, tf.map_fn() throws errors that a normal tf.Session.run() does not encounter.
I am trying to create unit tests with tf.test.TestCase, but could not find a way to test tf.map_fn without Tensorflow returning an error. (This occurs in both TF 1.4 and 1.5.) My guess is it has to do with how strings are treated by map_fn as tensors...
Source code / logs
Here is a very simple example:
Now I create a unit test and try to run it:
I get this output. Any idea what went wrong?
The text was updated successfully, but these errors were encountered: