Tensorflow 使用大数据集-Keras在Google Colab TPU上训练seq2seq模型
我正在尝试在Google Colab TPU上使用Keras训练一个序列到序列的机器翻译模型。 我有一个数据集,我可以加载到内存中,但我必须对其进行预处理,以将其提供给模型。特别是,我需要将目标字转换为一个热向量,并且有许多示例,我无法将整个转换加载到内存中,因此我需要批量生成数据 我将此函数用作批处理生成器:Tensorflow 使用大数据集-Keras在Google Colab TPU上训练seq2seq模型,tensorflow,keras,google-colaboratory,seq2seq,google-cloud-tpu,Tensorflow,Keras,Google Colaboratory,Seq2seq,Google Cloud Tpu,我正在尝试在Google Colab TPU上使用Keras训练一个序列到序列的机器翻译模型。 我有一个数据集,我可以加载到内存中,但我必须对其进行预处理,以将其提供给模型。特别是,我需要将目标字转换为一个热向量,并且有许多示例,我无法将整个转换加载到内存中,因此我需要批量生成数据 我将此函数用作批处理生成器: def generate_batch_bert(X_ids, X_masks, y, batch_size = 1024): ''' Generate a batch of da
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.
不确定出了什么问题以及我如何解决这个问题
编辑
我试着在内存中加载更少的数据,这样转换到目标字的一个热编码就不会使内核崩溃,而且它实际上可以工作。因此,我生成批处理的方式显然有问题。由于您没有提供您的模型,很难判断问题出在哪里 定义或任何样本数据。然而,我相当肯定你 遇到同样的问题 我最近被咬了 解决方法是使用非常有效的
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, ...)
让发电机与TPU一起工作有很多麻烦。我没能让发电机工作。总是使用文件你对文件是什么意思?我正在从文件加载信息它在CPU/GPU上工作吗?它在CPU/GPU上工作吗