2017-09-28 184 views
1

我很確定我錯過了一些明顯的東西,但是否不可能在張量流中加入一維張量張量?該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

由於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) 
+0

這工作只要'value'不是一個佔位符,但如果是,你需要使用'tf.shape(價值)[0]'這是一個dtype int的張量,但不是'int32',所以不能與'tf.split()'一起使用。 – src

1

更簡單的方法可以是使用reduce_join

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