簡體   English   中英

如何在張量流中加入字符串張量的張量?

[英]How to join a tensor of string tensors in 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).

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

一種更簡單的方法是使用reduce_join

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM