Сплит струны в тензорном потоке - PullRequest
0 голосов
/ 09 января 2019

У меня есть двумерный тензор строк, имеющий размерность [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 голосов
/ 15 января 2019

Вместо tf.string_split(x) используйте tf.string_split(x).values:

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)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...