ValueError:无法创建张量,您可能应该使用“padding=True”激活填充到具有相同长度的批处理张量

问题描述 投票:0回答:0

我正在使用我自己的数据集对 kannada 语言上的 wav2vec2 XLSR 进行微调,我一直遇到这个错误,即使我已经设置了 padding = True,它仍然会抛出错误。

inputs = processor(test_dataset["speech"], sampling_rate=16_000, return_tensors="pt", padding= True)

这是下面的代码,

import torch
import torchaudio
from datasets import load_dataset
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor

# Load the test dataset
test_dataset = dataset['test']

processor = Wav2Vec2Processor.from_pretrained("/content/container_0/ckpts/checkpoint-1800")
model = Wav2Vec2ForCTC.from_pretrained("/content/container_0/ckpts/checkpoint-1800")

resampler = torchaudio.transforms.Resample(48_000, 16_000)

# Preprocessing the datasets.
# We need to read the audio files as arrays
def speech_file_to_array_fn(batch):
    speech_array, sampling_rate = torchaudio.load(batch["path"])
    batch["speech"] = resampler(speech_array).squeeze().numpy()
    return batch

test_dataset = test_dataset.map(speech_file_to_array_fn)
max_length = 35720
inputs = processor(test_dataset["speech"], sampling_rate=16_000, return_tensors="pt", padding= True)

with torch.no_grad():
    logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits

predicted_ids = torch.argmax(logits, dim=-1)

print("Prediction:", processor.batch_decode(predicted_ids))
print("Reference:", test_dataset["text"][:2])
pecial tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.

/usr/local/lib/python3.9/dist-packages/transformers/feature_extraction_utils.py:165: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
  tensor = as_tensor(value)

---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

/usr/local/lib/python3.9/dist-packages/transformers/feature_extraction_utils.py in convert_to_tensors(self, tensor_type)
    164                 if not is_tensor(value):
--> 165                     tensor = as_tensor(value)
    166 

ValueError: could not broadcast input array from shape (2,35720) into shape (2,)


During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)

5 frames

/usr/local/lib/python3.9/dist-packages/transformers/feature_extraction_utils.py in convert_to_tensors(self, tensor_type)
    169                 if key == "overflowing_values":
    170                     raise ValueError("Unable to create tensor returning overflowing values of different lengths. ")
--> 171                 raise ValueError(
    172                     "Unable to create tensor, you should probably activate padding "
    173                     "with 'padding=True' to have batched tensors with the same length."

ValueError: Unable to create tensor, you should probably activate padding with 'padding=True' to have batched tensors with the same length.
python speech-recognition tensor huggingface-transformers torch
© www.soinside.com 2019 - 2024. All rights reserved.