user18764
user18764

Reputation: 253

string concatenation in tensorflow

I have a tf.string tensor, chars, with shape chars[Batch][None] where None denotes a dynamic shaped tensor (output from a variable length sequence).

If this tensor's shape were known (e.g. chars[Batch][Time]), then I could achieve concatenation of strings along the last dimension as:

chars = tf.split(chars,chars.shape[-1],axis=-1)
words = tf.squeeze(tf.strings.join(chars))

However, since the shape is unknown until runtime, I cannot use split.

Is there another way to accomplish this for a dynamic shaped string tensor?

In other words, I would like the string analogy of

words = tf.reduce_sum(chars,axis=-1)

along a dynamic shaped dimension.

Upvotes: 2

Views: 697

Answers (1)

user18764
user18764

Reputation: 253

Update 23/07/2022: Now you can use tf.strings.reduce_join to join all strings into a single string, or joins along an axis

words = tf.strings.reduce_join(chars, axis=-1)

This can be accomplished via:

words = tf.reduce_join(chars,axis=-1)

Upvotes: 2

Related Questions