在tensorflow中分割字符串

时间:2019-01-09 11:16:33

标签: python tensorflow

我有一个二维张量的字符串,其维度为[None, None]。我想使用tf.string_split()函数拆分每个字符串。我尝试使用以下代码

sentences = tf.placeholder(shape=[None, None], dtype=tf.string)
# remove punctuation
normalized_sentences = tf.regex_replace(input=sentences, pattern=r"\pP", rewrite="")        
tokens = tf.map_fn(lambda x: tf.string_split(x, delimiter=" "), normalized_sentences, dtype=tf.string)

但是错误引发了我一个错误。

TypeError: Failed to convert object of type <class 'tensorflow.python.framework.sparse_tensor.SparseTensor'> to Tensor. Contents: SparseTensor(indices=Tensor("map/while/StringSplit:0", shape=(?, 2), dtype=int64), values=Tensor("map/while/StringSplit:1", shape=(?,), dtype=string), dense_shape=Tensor("map/while/StringSplit:2", shape=(2,), dtype=int64)). Consider casting elements to a supported type.

1 个答案:

答案 0 :(得分:0)

使用tf.string_split(x).values代替tf.string_split(x)

import tensorflow as tf

tf.enable_eager_execution()

print(tf.string_split(['this is example sentence']).values)
# tf.Tensor([b'this' b'is' b'example' b'sentence'], shape=(4,), dtype=string)