O.r*_*rka 7 python string concatenation tensorflow
我处理大量dtype="str"数据.我一直在尝试构建一个简单的图形,如https://www.tensorflow.org/versions/master/api_docs/python/train.html#SummaryWriter.
对于一个简单的操作,我想使用placeholderas(如何为占位符提供?)将字符串连接在一起?
有谁知道如何将字符串张量合并在一起?
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)
Run Code Online (Sandbox Code Playgroud)
mrr*_*rry 17
感谢您的问题,我们优先在TensorFlow中添加对字符串连接的支持,并在此提交中添加它.字符串连接使用现有tf.add()运算符实现,以匹配NumPy add运算符(包括广播)的行为.
要实现您的示例,您可以编写:
complete = left + middle + right
Run Code Online (Sandbox Code Playgroud)
......或者,等价,但如果你想命名结果张量:
complete = tf.add(tf.add(left, middle), right, name="COMPLETE")
Run Code Online (Sandbox Code Playgroud)
我们还没有添加对tf.add_n()(或类似的操作tf.reduce_sum())字符串的支持,但如果有用例,它会考虑这个.
注意:要立即使用此功能,您需要从源代码构建TensorFlow.新的操作系统将在下一版TensorFlow(0.7.0)中提供.
我相信sparse_concat操作就是您正在寻找的: https: //www.tensorflow.org/versions/master/api_docs/python/sparse_ops.html#sparse_concat
add_n 将数值相加。