python - 如何在 tensorflow 中加入字符串张量的张量?

标签 python tensorflow

我很确定我遗漏了一些明显的东西,但是在 tensorflow 中不可能加入字符串张量的一维张量吗? string_join 操作仅采用张量的列表,我找不到将张量转换为列表的方法:

>>> x = tf.string_join(['a', 'b'], '')
>>> sess.run(x)
b'ab'
>>> x = tf.string_join(tf.constant(['a', 'b']), '')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/digitalroots/anaconda2/envs/dl/lib/python3.6/site-packages/tensorflow/python/ops/gen_string_ops.py", line 164, in string_join
    separator=separator, name=name)
  File "/home/digitalroots/anaconda2/envs/dl/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 406, in apply_op
    (input_name, op_type_name, values))
TypeError: Expected list for 'inputs' argument to 'StringJoin' Op, not Tensor("Const:0", shape=(2,), dtype=string).

最佳答案

一个更简单的方法是使用reduce_join:

>>> value = tf.constant(['a','b'])
>>> x = tf.reduce_join(value)
>>> sess.run(x)
b'ab'

关于python - 如何在 tensorflow 中加入字符串张量的张量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46460909/

相关文章:

python - 如何用字典修复多线程/多处理?

python - tensorflow 简单逻辑回归

Python:如何确定正在使用的特定 Linux 发行版?

python - “模块”对象没有属性 'basicConfig'

python - tf.multiply vs tf.matmul 计算点积

python - 值错误 ("Tensor %s is not an element of this graph."% obj)

tensorflow - 如何获得 one-hot 向量的密集表示

python - 使用 Tensorflow batch_norm 函数获得低测试精度

python - django forloop计数器作为变量

python - 使用 PIL 调整 png 图像大小会失去透明度