tensorflow data_error ai_generated true

InvalidArgumentError: Cannot batch ragged tensors with different number of rows

ID: tensorflow/ragged-tensor-batch-size-mismatch

Also available as: JSON · Markdown · 中文
85%Fix Rate
84%Confidence
1Evidence
2023-06-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
tensorflow 2.9 active
tensorflow 2.10 active
tensorflow 2.11 active

Root Cause

Attempting to batch multiple RaggedTensors that have different row lengths (first dimension) in a way that requires uniform batch size, e.g., using tf.data.Dataset.batch() without padding.

generic

中文

尝试批处理多个具有不同行长度(第一维)的 RaggedTensor,但使用了需要统一批量大小的方式,例如未填充的 tf.data.Dataset.batch()。

Official Documentation

https://www.tensorflow.org/guide/ragged_tensor#batching_ragged_tensors

Workarounds

  1. 90% success Use padded_batch() instead of batch() on the dataset: dataset = dataset.padded_batch(batch_size, padded_shapes=[None, None])
    Use padded_batch() instead of batch() on the dataset: dataset = dataset.padded_batch(batch_size, padded_shapes=[None, None])
  2. 85% success Pad RaggedTensors manually before batching using ragged_tensor.to_tensor(default_value=0, shape=[None, max_len]) then use regular batch().
    Pad RaggedTensors manually before batching using ragged_tensor.to_tensor(default_value=0, shape=[None, max_len]) then use regular batch().

中文步骤

  1. 在数据集上使用 padded_batch() 代替 batch():dataset = dataset.padded_batch(batch_size, padded_shapes=[None, None])
  2. 在批处理之前手动填充 RaggedTensor:使用 ragged_tensor.to_tensor(default_value=0, shape=[None, max_len]),然后使用常规 batch()。

Dead Ends

Common approaches that don't work:

  1. 90% fail

    drop_remainder only controls whether the last incomplete batch is dropped, but the error occurs within a batch where rows are already unequal.

  2. 75% fail

    While it resolves the batching error, it can cause OOM if ragged dimensions are large; also it changes semantics.