-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Description
System Info
transformers 4.53.2, MacOS, python 3.12.10
Who can help?
Information
- The official example scripts
- My own modified scripts
Tasks
- An officially supported task in the
examples
folder (such as GLUE/SQuAD, ...) - My own task or dataset (give details below)
Reproduction
During training of the Autoformer model I get this error:
ValueError: lags cannot go further than history length, found lag 37 while history length is only 70
This exception is raised when calling the create_network_inputs function of the Autoformer class, specifcally when the function call get_lagged_subsequences function (at the time of writing line 1411 of modeling_autoformer.py):
lagged_sequence = self.get_lagged_subsequences(sequence=inputs, subsequences_length=subsequences_length)
The signature of get_lagged_subsequences is:
def get_lagged_subsequences( self, sequence: torch.Tensor, subsequences_length: int, shift: int = 0 ) -> torch.Tensor:
The arguments passed during the above function call has the following values:
- inputs is the concatenation of past and future values
- subsequences_length is the sum between the context length and the prediction length
- shift is kept to 0
Inside get_lagged_subsequences there is this line of code (at the time of writing 1505):
if max(indices) + subsequences_length > sequence_length:
where:
- max(indices) is the maximum value of the lags_sequences
- subsequences_length is the one passed as parameter, thus the sum between the context length and the prediction length
- sequence_length is initialised as sequence_length = sequence.shape[1], thus is equal to the context_length
So in the best case when max(indices) is equal to zero, we get that the if condition became:
0 + (context_length + prediction_length) > context_length
Since the model always predict at least one value the prediction length is always greater than zero, so this condition is always true. The problem is that having this condition always being true block the code since when the if condition is verified an exception is raised:
if reshaped_lagged_sequence.shape[1] != time_feat.shape[1]: raise ValueError( f"input length {reshaped_lagged_sequence.shape[1]} and time feature lengths {time_feat.shape[1]} does not match")
Here is a minimal code that allow to reproduce the error, please note that for every value of lags_sequence the exception is raised here I am using the default ones loaded from huggingface/autoformer-tourism-monthly config:
from datasets import Dataset
import torch
import numpy as np
cfg = AutoConfig.from_pretrained("huggingface/autoformer-tourism-monthly")
model = AutoformerModel(cfg)
X, y, T, M, T_future = (np.random.rand(500, 60), np.random.rand(500, 10), np.random.rand(500, 60, 6), np.random.rand(500, 60), np.random.rand(500, 10, 6))
train_ds = Dataset.from_dict({
'past_values': X,
'future_values': y,
'past_time_features': T,
'past_observed_mask': M,
'future_time_features': T_future
})
training_args = TrainingArguments(
output_dir="./autoformer_checkpoints",
save_steps=100,
logging_steps=50,
num_train_epochs=50,
learning_rate=1e-4,
report_to="none"
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_ds,
)
trainer.train()
Expected behavior
The expected behavior is to have a meaningful if condition or remove the if line if not actually needed, in order to being able to train and use the Autoformer model