Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow - Retrieve each character in a string tensor

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

like image 898
Dheeraj Peri Avatar asked Jun 27 '17 01:06

Dheeraj Peri


People also ask

Can tensor be string?

Tensors often contain floats and ints, but have many other types, including: complex numbers. strings.

Does TensorFlow work with strings?

string data type. The basic TensorFlow tf. string dtype allows you to build tensors of byte strings. Unicode strings are utf-8 encoded by default.

What is TF constant?

tf. constant is useful for asserting that the value can be embedded that way. If the argument dtype is not specified, then the type is inferred from the type of value . # Constant 1-D Tensor from a python list. tf.


1 Answers

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())
like image 117
garciparedes Avatar answered Nov 03 '22 01:11

garciparedes