python - 在 TensorFlow 中合并字符串张量

标签 python string concatenation tensorflow

我处理大量的 dtype="str" 数据。我一直在尝试构建一个简单的图表,如 https://www.tensorflow.org/versions/master/api_docs/python/train.html#SummaryWriter .

对于一个简单的操作,我想使用 placeholder 将字符串连接在一起,如 ( How to feed a placeholder? )

有谁知道如何将字符串张量合并在一起?

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)

最佳答案

感谢您的提问,我们优先在 TensorFlow 中添加了对字符串连接的支持,并将其添加到 this commit 中。 .字符串连接是使用现有的 tf.add() 实现的运算符,以匹配 NumPy's add operator 的行为(包括广播)。

要实现您的示例,您可以编写:

complete = left + middle + right

…或者,等价地,但是如果你想命名结果张量:

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

我们尚未在 tf.add_n()(或相关操作,如 tf.reduce_sum())中添加对字符串的支持,但如果有用,我们会考虑这一点案例。

注意:要立即使用此功能,您需要 build TensorFlow from source .新操作将在下一版本的 TensorFlow (0.7.0) 中提供。

关于python - 在 TensorFlow 中合并字符串张量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34247374/

相关文章:

python - pyenv-virtualenvwrapper 的调试安装

python - 如何在 Airflow 中使用 EmailOperator 发送多封电子邮件

python - 无法看到在 celery-rabbitMQ 组合中执行的任务的输出消息

string - 在 UNIX 文件的每一行中第 N 次出现后替换所有出现的字符

在循环中连接字符+整数

Excel:通过评估其他列中的数据与第三列中的匹配数据来编辑列数据

python - 作为 Django 包维护者,我如何处理迁移?

c - 使用指向字符的指针作为 strtok 的参数

c++ - 使用istringstream处理一个变长的内存块

python - Concat DataFrames 对角线