tensorflow有tf.string_split函数,可以将密集张量分割为SparseTensor,但不提供相反的函数。
谁知道怎么做?感谢〜
下面是一个方法,但我不知道如何在计算slice_input_list时动态分配num_split的值
import tensorflow as tf
import numpy as np
input_dense = tf.placeholder(dtype=tf.string, shape=[None], name='input_sentences')
split_input_dense = tf.string_split(input_dense)
# slice_input_list = tf.sparse_split(sp_input=split_input_dense, num_split=split_input_dense.get_shape()[0], axis=0)
# slice_input_list = tf.sparse_split(sp_input=split_input_dense, num_split=split_input_dense.dense_shape[0], axis=0)
slice_input_list = tf.sparse_split(sp_input=split_input_dense, num_split=3, axis=0)
join_input_list = []
for slice_input in slice_input_list:
slice_input_value = slice_input.values
join_input = tf.reduce_join(slice_input_value, reduction_indices=0, separator=' ')
join_input_list.append(join_input)
output_dense = tf.stack(join_input_list)
with tf.Session() as sess:
feed_dict = {input_dense: np.array(["a b c", "d e", "f g h i"])}
print sess.run(output_dense, feed_dict=feed_dict)