I have the map function below (runnable example), which inputs a string
and outputs a string
and an integer
.
in tf.data.Dataset.from_tensor_slices
I named the original input 'filenames'
. But when I return the values from the map function map_element_counts
I can only return a tuple (returning a dictionary generates an exception).
Is there a way to name the 2 elements returned from my map_element_counts
function?
import tensorflow as tf
filelist = ['fileA_6', 'fileB_10', 'fileC_7']
def map_element_counts(fname):
# perform operations outside of tensorflow
return 'test', 10
ds = tf.data.Dataset.from_tensor_slices({'filenames': filelist})
ds = ds.map(map_func=lambda x: tf.py_func(
func=map_element_counts, inp=[x['filenames']], Tout=[tf.string, tf.int64]
))
element = ds.make_one_shot_iterator().get_next()
with tf.Session() as sess:
print(sess.run(element))
Result:
(b'test', 10)
Desired Result:
{'elementA': b'test', 'elementB': 10)
Added detail:
When I do return {'elementA': 'test', 'elementB': 10}
I get this exception:
tensorflow.python.framework.errors_impl.UnimplementedError: Unsupported object type dict
I'm posing a final solution to this question for posterity sake. The code below is a copy/paste example that works under the most complex conditions this question addresses (note that the other two answers aren't copy/pastable code samples):
The goal of the code is:
Copy/pastable working sample for Tensorflow 1.5 / Python 3.x
Output:
There's no need for
tf.py_func
in this case, becausemap_func
ofDataset#map
works with dictionaries and other structures:Here's an example:
Output:
Applying
tf.py_func
insideds.map
works.I created a very simple file as example. Where I just write 10 inside.
dummy_file.txt:
Here for the script:
Output: