TensorFlow strings: what they are and how to work with them TensorFlow strings: what they are and how to work with them numpy numpy

TensorFlow strings: what they are and how to work with them


Unlike Python, where a string can be treated as a list of characters for the purposes of slicing and such, TensorFlow's tf.strings are indivisible values. For instance, x below is a Tensor with shape (2,) whose each element is a variable length string.

x = tf.constant(["This is a string", "This is another string"])

However, to achieve what you want, TensorFlow provides the tf.decode_raw operator. It takes a tf.string tensor as input, but can decode the string into any other primitive data type. For instance, to interpret the string as a tensor of characters, you can do the following :

x = tf.constant("This is string")x = tf.decode_raw(x, tf.uint8)y = x[:4]sess = tf.InteractiveSession()print(y.eval())# prints [ 84 104 105 115]