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

前端 未结 1 1206
醉话见心
醉话见心 2020-12-30 03:21

When I read file with tf.read_file I get something with type tf.string. Documentation says only that it is \"Variable length byte arrays. Each elem

相关标签:
1条回答
  • 2020-12-30 03:42

    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]
    
    0 讨论(0)
提交回复
热议问题