I am trying to perform the opposite of what tf.decode_raw does.
An example would be given a tensor of dtype=tf.float32, I would like to have a function encode_raw() that takes in a float tensor and returns a Tensor of type string.
This is useful because then I can use tf.write_file to write the file.
Does anyone know how to create such a function in Tensorflow using existing functions?
I would recommend writing numbers as text with tf.as_string
. If you really want to write them as a binary string, however, it turns out to be possible:
import tensorflow as tf
with tf.Graph().as_default():
character_lookup = tf.constant([chr(i) for i in range(256)])
starting_dtype = tf.float32
starting_tensor = tf.random_normal(shape=[10, 10], stddev=1e5,
dtype=starting_dtype)
as_string = tf.reduce_join(
tf.gather(character_lookup,
tf.cast(tf.bitcast(starting_tensor, tf.uint8), tf.int32)))
back_to_tensor = tf.reshape(tf.decode_raw(as_string, starting_dtype),
[10, 10]) # Shape information is lost
with tf.Session() as session:
before, after = session.run([starting_tensor, back_to_tensor])
print(before - after)
This for me prints an array of all zeros.
For those working with Python 3:
chr() has a different behavior in Python 3 that changes the byte output obtained with the code from previous answer. Replacing this code line
character_lookup = tf.constant([chr(i) for i in range(256)])
with
character_lookup = tf.constant([i.tobytes() for i in np.arange(256, dtype=np.uint8)])
fixes this issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With