python - tensorflow 中的字符串连接

标签 python tensorflow concatenation

我有一个 tf.string 张量,chars,形状为 chars[Batch][None],其中 None code> 表示动态形状张量(来自可变长度序列的输出)。

如果这个张量的形状已知(例如chars[Batch][Time]),那么我可以实现沿最后一个维度的字符串串联:

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

但是,由于形状在运行时之前是未知的,因此我无法使用 split

是否有另一种方法可以为动态形状的字符串张量实现此目的?

换句话说,我想要字符串类比

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

沿着动态形状的维度。

最佳答案

更新 23/07/2022:现在您可以使用 tf.strings.reduce_join将所有字符串连接成单个字符串,或沿轴连接

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

这可以通过以下方式完成:

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

关于python - tensorflow 中的字符串连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53161875/

相关文章:

TSQL 连接

Python - 如何在 ArangoDB 中创建边

Python 方法放置

tensorflow - 在 GPU 上训练的模型可以在 CPU 上用于推理,反之亦然吗?

tensorflow - 形状必须为 0 级,但为 1 级,parse_single_sequence_example

python - 具有噪声损失函数的 SciPy 优化

c - C 中的字符串连接

C++ 将字符串文字添加到字 rune 字

python - pandas 创建数据框 - 解析 key

python - 如何将 NumPy 链接到 Eigen3,或 OpenBLAS、GotoBLAS2、ATLAS?