7

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)
Community
  • 1
  • 1
O.rka
  • 29,847
  • 68
  • 194
  • 309

4 Answers4

18

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).

mrry
  • 125,488
  • 26
  • 399
  • 400
3

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.

Ian Goodfellow
  • 2,584
  • 2
  • 19
  • 20
3

I know this is not an immediate answer and don't want this to remain hidden in the comments.

If you'd like to incorporate an operation that isn't covered by the existing library, you can create a custom Op. To incorporate your custom Op, you'll need to:

  • Register the new Op in a C++ file. The Op registration is independent of the implementation, and describes the semantics of how the Op is invoked. For example, it defines the Op name, and specifies its inputs and outputs.
  • Implement the Op in C++. This implementation is called a "kernel", and there can be multiple kernels for different architectures (e.g. CPUs, GPUs) or input / output types.
  • Create a Python wrapper. This wrapper is the public API to create the Op. A default wrapper is generated from the Op registration, which can be used directly or added to.
  • Optionally, write a function to compute gradients for the Op.
  • Optionally, write a function that describes the input and output shapes for the Op. This allows shape inference to work with your Op.
  • Test the Op, typically in Python. If you define gradients, you can verify them with the Python GradientChecker.

What you asked if very relevant and will probably become one of the higher Google search results in the future for using string type with TensorFlow; as such this avenue to a solution needs to made available so that others are aware it exist.

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
2

There is not a way to do this at the present time, AFAIK.

Recognizing that this is a disappointing answer, I've filed issue #701 on GitHub to track this request.

dga
  • 21,757
  • 3
  • 44
  • 51
  • Can the OP add a new [TensorFlow op](https://www.tensorflow.org/versions/master/how_tos/adding_an_op/index.html)? – Guy Coder Jan 05 '16 at 22:57
  • 1
    Yes, that would be a way to do it. We'd welcome a contribution - this is clearly something that multiple people will want. – dga Jan 06 '16 at 21:13
  • 1
    I'm tempted but it would not be for a few months as I still have to get through the basic TensorFlow examples, the GPU examples, and then get my C++ up to speed. I am currently translating [Neural Networks and Deep Learning](http://neuralnetworksanddeeplearning.com/) to F# to leverage my F# knowledge into learning Deep Learning and Python. But I will keep it high on the list now that you confirmed I am starting to grasp TensorFlow. Thanks. – Guy Coder Jan 06 '16 at 21:42
  • dga in regards to @Ian Goodfellow's answer, is there a way to do it with the sparse_concat? – O.rka Jan 07 '16 at 01:43
  • I don't believe so. The elements of the sparse tensor are still individual strings, and there's no way to concatenate the string objects themselves. – dga Jan 07 '16 at 20:23