Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 simply casting it with something like this:

x = tf.constant([1,2,3], dtype=tf.int32)
x_as_string = tf.cast(x, dtype=tf.string) # hoping for this output: [ '1', '2', '3' ]

with tf.Session() as sess:
  res = sess.run(x_as_string)

but hit the error message:

Cast int32 to string is not supported

Is there a simple function somewhere in the documentation that I am missing?


UPDATE:

To clarify: I realise I could 'work around' this issue using a python function with tf.py_func but asking if there is a solution in TensorFlow itself

like image 259
Stewart_R Avatar asked Sep 19 '25 07:09

Stewart_R


1 Answers

tf.as_string() Converts each entry in the given tensor to strings. Supports many numeric

like image 70
bidai Avatar answered Sep 20 '25 21:09

bidai