TensorFlow TextVectorization 在从 pickle 加载后生成没有填充的 Ragged Tensor

TensorFlow TextVectorization producing Ragged Tensor with no padding after loading it from pickle

我有一个名为“eng_vectorization”的 TensorFlow TextVectorization 层:

vocab_size = 15000
sequence_length = 20

eng_vectorization = TextVectorization(max_tokens = vocab_size,
                                  output_mode = 'int',
                                  output_sequence_length = sequence_length)

train_eng_texts = [pair[0] for pair in text_pairs]  # Where text_pairs is my english-spanish text data.
eng_vectorization.adapt(train_eng_texts)

然后我使用以下代码将其保存在泡菜文件中:

pickle.dump({'config': eng_vectorization.get_config(),
             'weights': eng_vectorization.get_weights()},
             open("english_vocab.pkl", "wb"))

然后我正确加载泡菜文件 new_eng_vectorization:

from_disk = pickle.load(open("english_vocab.pkl", "rb"))

new_eng_vectorization = TextVectorization.from_config(from_disk['config'])
new_eng_vectorization.adapt(tf.data.Dataset.from_tensor_slices(["xyz"]))
new_eng_vectorization.set_weights(from_disk['weights'])

现在我期待,以前的矢量化 eng_vectorization 和新加载的矢量化 new_eng_vectorization 工作相同,但它们不是。

原始向量化的输出,eng_vectorization(['Hello people'])是张量:

<tf.Tensor: shape=(1, 20), dtype=int64, numpy=
array([[1800,  110,    0,    0,    0,    0,    0,    0,    0,    0,    0,
           0,    0,    0,    0,    0,    0,    0,    0,    0]])>

pickled 向量化的输出,new_eng_vectorization(['Hello people']) 是一个不规则张量。

<tf.RaggedTensor [[1800, 110]]>

eng_vectorizationnew_eng_vectorization 具有相同的配置:

{'batch_input_shape': (None,),
 'dtype': 'string',
 'idf_weights': None,
 'max_tokens': 15000,
 'name': 'text_vectorization',
 'ngrams': None,
 'output_mode': 'int',
 'output_sequence_length': 20,
 'pad_to_max_tokens': False,
 'ragged': False,
 'sparse': False,
 'split': 'whitespace',
 'standardize': 'lower_and_strip_punctuation',
 'trainable': True,
 'vocabulary': None}

我认为我保存矢量化的方式有问题,我该如何解决?我正在使用它进行部署,这就是为什么我希望 pickled vectorization 像前一个一样工作。

这是可重现代码的 Google Colab link - [点击此处]

该问题与最近的 bug 有关,其中来自已保存配置的 output_mode 设置不正确。

这个有效:

pickle.dump({'config': eng_vectorization.get_config(),
             'weights': eng_vectorization.get_weights()},
             open("english_vocab.pkl", "wb"))

from_disk = pickle.load(open("english_vocab.pkl", "rb"))

new_eng_vectorization = TextVectorization(max_tokens=from_disk['config']['max_tokens'],
                                          output_mode='int',
                                          output_sequence_length=from_disk['config']['output_sequence_length'])

new_eng_vectorization.adapt(tf.data.Dataset.from_tensor_slices(["xyz"]))
new_eng_vectorization.set_weights(from_disk['weights'])
new_eng_vectorization(['Hello people'])
<tf.Tensor: shape=(1, 20), dtype=int64, numpy=
array([[1800,  110,    0,    0,    0,    0,    0,    0,    0,    0,    0,
           0,    0,    0,    0,    0,    0,    0,    0,    0]])>

目前无法正常工作:

pickle.dump({'config': eng_vectorization.get_config(),
             'weights': eng_vectorization.get_weights()},
             open("english_vocab.pkl", "wb"))

from_disk = pickle.load(open("english_vocab.pkl", "rb"))
new_eng_vectorization = TextVectorization(max_tokens=from_disk['config']['max_tokens'],
                                          output_mode=from_disk['config']['output_mode'],
                                          output_sequence_length=from_disk['config']['output_sequence_length'])

new_eng_vectorization.adapt(tf.data.Dataset.from_tensor_slices(["xyz"]))
new_eng_vectorization.set_weights(from_disk['weights'])
new_eng_vectorization(['Hello people'])
<tf.RaggedTensor [[1800, 110]]>

即使 'int'from_disk['config']['output_mode'] 相等且数据类型相同。无论如何,您现在可以使用解决方法。