如何从 Tensorflow 中的字符串张量中随机删除空格

How to randomly remove spaces from a tensor of strings in Tensorflow

有一个字符串张量,对于每个字符串,我想随机删除 n 个空格,其中 n <= 该字符串中的空格数。

import tensorflow as tf

strings_with_spaces = tf.constant([b'A B C D E F', b'1 2 3 4 5 6'])
remove_spaces_randomly = None
T = tf.map_fn(remove_spaces_randomly, strings_with_spaces)

I expect the output to be something like:
['A BC D EF', '1 2 34 5 6']

你可以试试这样:

import tensorflow as tf

strings_with_spaces = tf.constant([b'A B C D E F', b'1 2 3 4 5 6'])

def remove_spaces_randomly(x, n):
  split_string = tf.strings.unicode_split(x, 'UTF-8')
  indices = tf.where(tf.equal(split_string, ' '))
  n = tf.cond(tf.less_equal(n, tf.shape(indices)[0]), lambda: n, lambda: tf.shape(indices)[0])
  output = tf.tensor_scatter_nd_update(split_string, tf.random.shuffle(indices)[:n], tf.repeat([''], repeats=n))
  return tf.strings.join(output)

n = 3
T = tf.map_fn(lambda x: remove_spaces_randomly(x, n), strings_with_spaces)
print(T)
tf.Tensor([b'A BCDE F' b'1 23 456'], shape=(2,), dtype=string)