Tensorflow - Retrieve each character in a string tensor

吃可爱长大的小学妹 提交于 2021-01-27 04:01:57

问题


I'm trying to retrieve the characters in a string tensor for character level prediction. The ground truths are words where each character has an id in dictionary. I have a tensor corresponding to the length of the string.

Now, I have to get each character in the string tensor. After checking the related posts, a simple retrieval can be as follows. Example string is "This"

a= tf.constant("This",shape=[1])
b=tf.string_split(a,delimiter="").values  #Sparse tensor has the values array which stores characters

Now I want to make a string with spaces in between the letters "This" i.e " T h i s ". I need spacing at the start and the end too. How do I do this?

I have tried to iterate through the characters like below

for i in xrange(b.dense_shape[1]): # b.dense_shape[1] has the length of string
        x=b.values[i]

But the loop expects an integer rather than a tensor.

Any idea on how to do the above tasks? I couldn't find any documentation related to this (apart from the tf.string_split function). Any suggestions are welcome. Thanks


回答1:


Your problem is that you are trying to iterate over Tensor, that is not iterable. There is some alternatives for this task, such as convert it to numpy array with eval() or use the tf.map_fn.

If you want to threat b as numpy array you only need to add the call .eval() before .values and iterate over the result as follows:

with tf.Session() as sess:
    a = tf.constant("This", shape=[1])
    b = tf.string_split(a, delimiter="").values.eval()

    for i in b:
        print(i)

The second alternative is more appropriate because of it takes advantage of TensorFlow's graph. It is based in the use of a function that "maps" the Tensor. This can be done as follows (where in fn you can define de behavior of the iteration):

with tf.Session() as sess:
    a = tf.constant("This", shape=[1])
    b = tf.string_split(a, delimiter="").values

    fn = lambda i: i

    print(tf.map_fn(fn, b).eval())


来源:https://stackoverflow.com/questions/44770980/tensorflow-retrieve-each-character-in-a-string-tensor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!