Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge string tensors in TensorFlow

I work with a lot of dtype="str" data. I've been trying to build a simple graph as in https://www.tensorflow.org/versions/master/api_docs/python/train.html#SummaryWriter.

For a simple operation, I wanted to concatenate strings together using a placeholder as in (How to feed a placeholder?)

Does anyone know how to merge string tensors together?

import tensorflow as tf
sess = tf.InteractiveSession()

with tf.name_scope("StringSequence") as scope:
    left = tf.constant("aaa",name="LEFT")
    middle = tf.placeholder(dtype=tf.string, name="MIDDLE")
    right = tf.constant("ccc",name="RIGHT")
    complete = tf.add_n([left,middle,right],name="COMPLETE") #fails here
sess.run(complete,feed_dict={middle:"BBB"})
#writer = tf.train.SummaryWriter("/users/mu/test_out/", sess.graph_def)
like image 265
O.rka Avatar asked Dec 13 '15 02:12

O.rka


People also ask

Can tensor be string?

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

Is Join faster than concatenation Python?

String join is significantly faster then concatenation. Why? Strings are immutable and can't be changed in place. To alter one, a new representation needs to be created (a concatenation of the two).


2 Answers

Thanks to your question, we prioritized adding support for string concatenation in TensorFlow, and added it in this commit. String concatenation is implemented using the existing tf.add() operator, to match the behavior of NumPy's add operator (including broadcasting).

To implement your example, you can write:

complete = left + middle + right

…or, equivalently, but if you want to name the resulting tensor:

complete = tf.add(tf.add(left, middle), right, name="COMPLETE")

We have not yet added support for strings in tf.add_n() (or related ops like tf.reduce_sum()) but will consider this if there are use cases for it.

NOTE: To use this functionality immediately, you will need to build TensorFlow from source. The new op will be available in the next release of TensorFlow (0.7.0).

like image 164
mrry Avatar answered Oct 20 '22 00:10

mrry


I believe the sparse_concat op is what you are looking for: https://www.tensorflow.org/versions/master/api_docs/python/sparse_ops.html#sparse_concat

add_n will add numeric values together.

like image 23
Ian Goodfellow Avatar answered Oct 20 '22 00:10

Ian Goodfellow