How to create encode_raw tenorflow function?

I am trying to do the opposite of what tf.decode_raw does.

An example is the tensor dtype = tf.float32, I would like to have an encode_raw () function that takes a float tensor and returns a string of type Tensor.

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?

+5
source share
2 answers

I would recommend writing numbers as text using tf.as_string . However, if you really want to write them as a binary string, this is 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.

+2
source

For those working with Python 3:

chr () has a different behavior in Python 3 that modifies the byte output received with code from the previous answer. Replacing this code string

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 problem.

+1
source

Source: https://habr.com/ru/post/1266717/


All Articles