如何在张量流中加入张量张量的张量?

时间:2017-09-28 04:19:14

标签: python 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).

2 个答案:

答案 0 :(得分:2)

由于tf.string_join函数期望输入是可迭代的,因此可以先将张量分割为各个元素。

num_or_size_splits属性定义从tf.split()返回的张量的数量。

value = tf.constant(['a','b'])
split = tf.split(value, num_or_size_splits = value.shape[0], axis = 0)
string = tf.string_join(split)

答案 1 :(得分:2)

更简单的方法可能是使用reduce_join

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