tensorflow有tf.string_split函数,可以将密集张量分割为SparseTensor,但不提供相反的函数。
谁知道怎么做?感谢〜
例如: SparseTensor:
[["a", "b", "c"]
["d", "e"]
["f", "g", "h", "i"]]
将SparseTensor与分隔符“”连接到密集张量:
["a b c",
"d e",
"f g h i"]
tensorflow中SparseTensor格式:
# indices = tf.constant([[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [2, 0], [2, 1], [2, 2], [2, 3]], dtype=tf.int64)
# values = tf.constant(["a", "b", "c", "d", "e", "f", "g", "h", "i"], dtype=tf.string)
# dense_shape = tf.constant([3, 4], dtype=tf.int64)
# tf.SparseTensor(indices=indices, values=values, dense_shape=dense_shape)
更新问题:
join_words_list = []
slice_words_list = tf.sparse_split(sp_input=sparse_words, num_split=3, axis=0)
# slice_words_list = tf.sparse_split(sp_input=sparse_words, num_split=sparse_words.dense_shape[0], axis=0)
for slice_words in slice_words_list:
slice_words = slice_words.values
join_words = tf.reduce_join(slice_words, reduction_indices=0, separator=" ")
join_words_list.append(join_words)
join_str = tf.stack(join_words_list)
现在,这会产生新问题,tensorflow, split tf.string SparseTensor to the list of dense Tensor in dim 0
答案 0 :(得分:1)
这是一个sparse_string_join
实现,可以实现您所需要的:
def sparse_string_join(input_sp):
"""Concats each row of SparseTensor `input_sp` and outputs them as a 1-D string tensor."""
# convert the `SparseTensor` to a dense `Tensor`
dense_input = tf.sparse_to_dense(input_sp.indices, input_sp.dense_shape, input_sp.values, default_value='')
# concat each row of strings.
strings = tf.reduce_join(dense_input, axis=1, separator=' ')
# remove extra spaces.
return tf.string_strip(strings)
答案 1 :(得分:0)
output = tf.string_join([input0, input1])