【问题标题】:Training seq2seq model on Google Colab TPU with big dataset - Keras使用大数据集在 Google Colab TPU 上训练 seq2seq 模型 - Keras
【发布时间】:2021-05-17 05:53:47
【问题描述】:

我正在尝试在 Google Colab TPU 上使用 Keras 训练序列到序列模型以进行机器翻译。 我有一个可以加载到内存中的数据集,但我必须对其进行预处理以将其提供给模型。特别是我需要将目标词转换为一个热向量,并且有很多例子我无法将整个转换加载到内存中,所以我需要批量数据。

我将此函数用作批处理生成器:

def generate_batch_bert(X_ids, X_masks, y, batch_size = 1024):
    ''' Generate a batch of data '''
    while True:
        for j in range(0, len(X_ids), batch_size):
          # batch of encoder and decoder data
          encoder_input_data_ids = X_ids[j:j+batch_size]
          encoder_input_data_masks = X_masks[j:j+batch_size]
          y_decoder = y[j:j+batch_size]
          

          # decoder target and input for teacher forcing
          decoder_input_data = y_decoder[:,:-1]
          decoder_target_seq = y_decoder[:,1:]
          
          # batch of decoder target data
          decoder_target_data = to_categorical(decoder_target_seq, vocab_size_fr)
          # keep only with the right amount of instances for training on TPU
          if encoder_input_data_ids.shape[0] == batch_size:
            yield([encoder_input_data_ids, encoder_input_data_masks, decoder_input_data], decoder_target_data)

问题在于,每当我尝试按如下方式运行 fit 函数时:

model.fit(x=generate_batch_bert(X_train_ids, X_train_masks, y_train, batch_size = batch_size),
                    steps_per_epoch = train_samples//batch_size,
                    epochs=epochs,
                    callbacks = callbacks,
                    validation_data = generate_batch_bert(X_val_ids, X_val_masks, y_val, batch_size = batch_size),
                    validation_steps = val_samples//batch_size)

我收到以下错误:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_util.py:445 make_tensor_proto
    raise ValueError("None values not supported.")

ValueError: None values not supported.

不知道出了什么问题以及如何解决这个问题。

编辑

我尝试在内存中加载较少量的数据,以便转换为目标单词的一种热编码不会使内核崩溃并且它确实有效。所以我如何生成批次显然有问题。

【问题讨论】:

  • 让生成器与 TPU github.com/tensorflow/tensorflow/issues/32712 一起工作时遇到了很多麻烦。我没有设法让发电机工作。并且始终使用文件
  • 文件是什么意思?
  • 我正在从文件中加载信息
  • 它可以在 CPU/GPU 上运行吗?
  • 它在 CPU/GPU 上工作

标签: tensorflow keras google-colaboratory seq2seq google-cloud-tpu


【解决方案1】:

由于您没有提供模型,因此很难判断问题所在 定义也没有任何样本数据。但是,我相当肯定你是 遇到同样的事情 TensorFlow bug 我最近被咬了。

解决方法是使用tensorflow.data API,它很有效 使用 TPU 效果更好。像这样:

from tensorflow.data import Dataset
import tensorflow as tf

def map_fn(X_id, X_mask, y):
    decoder_target_data = tf.one_hot(y[1:], vocab_size_fr)
    return (X_id, X_mask, y[:-1]), decoder_target_data
...
X_ids = Dataset.from_tensor_slices(X_ids)
X_masks = Dataset.from_tensor_slices(X_masks)
y = Dataset.from_tensor_slices(y)
ds = Dataset.zip((X_ids, X_masks, y)).map(map_fn).batch(1024)
model.fit(x = ds, ...)

【讨论】:

    猜你喜欢
    • 2020-12-10
    • 2020-03-31
    • 2018-07-24
    • 1970-01-01
    • 2021-03-22
    • 2022-01-25
    • 2020-02-23
    • 2019-05-25
    • 2018-05-01
    相关资源
    最近更新 更多