Tensorflow - How To Convert Int32 To String (using Python API For Tensorflow)
Simple question really but cannot seem to find a function in the TensorFlow docs or by googling. How can I convert a tensor of type tf.int32 to one of type tf.string? I tried simpl
Solution 1:
tf.as_string() Converts each entry in the given tensor to strings. Supports many numeric
Solution 2:
You can do that with the newly added (v1.12.0) tf.strings.format
:
import tensorflow as tf
x = tf.constant([1, 2, 3], dtype=tf.int32)
x_as_string = tf.map_fn(lambda xi: tf.strings.format('{}', xi), x, dtype=tf.string)
with tf.Session() as sess:
res = sess.run(x_as_string)
print(res)
# [b'1' b'2' b'3']
For Tensorflow v2,
import tensorflow as tf
x = tf.constant([1, 2, 3], dtype=tf.int32)
x_as_string = tf.map_fn(lambda xi: tf.strings.format('{}', xi), x, dtype=tf.string)
print(x_as_string.numpy())
# [b'1' b'2' b'3']
Post a Comment for "Tensorflow - How To Convert Int32 To String (using Python API For Tensorflow)"