Hello everyone,
I am working on Informer for Prediction model from this link; Informer
I also attached small part of my training data. In the csv file, first column is timestamp and second column is my target. Other 82 column is my input data for every timestamp, my goal is using 10 timestamp for every prediction. My code to create and train informer model is here;
from transformers import InformerConfig
config = InformerConfig(
d_model=128, # dimensionality of the model
n_heads=8, # number of attention heads
encoder_layers=3, # number of encoder layers
decoder_layers=3, # number of decoder layers
input_size=81, # number of input features
context_length=100, # number of past timesteps
prediction_length=1 # number of future timesteps to predict
)
import torch
from torch.optim import Adam
from transformers import InformerForPrediction
# Setup device
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Creating tensor from data
#data = torch.tensor(data)
# Create a tensor with all ones
past_observed_mask = torch.ones(10)
# Initialize the model
model = InformerForPrediction(config).to(device)
# Define loss and optimizer
criterion = torch.nn.MSELoss() # Mean Squared Error Loss for numerical prediction
optimizer = Adam(model.parameters(), lr=0.001)
# Training loop
for epoch in range(2): # num_epochs is the number of epochs you want to train
model.train()
total_loss = 0
for batch in dataloader: # assuming dataloader is defined as before
optimizer.zero_grad()
# Get input and targets and move to GPU if available
sequences, labels = batch
sequences, labels = sequences.to(device), labels.to(device)
past_values = sequences[:, :, 2:] # Assuming the last column is the future value
past_time_features = sequences[:, :, :2] # Replace with the correct slice for time features
past_observed_mask = torch.ones_like(past_values, dtype=torch.float)
print(past_values, 'target', labels.unsqueeze(-1), 'past_time', past_time_features, 'mask', past_observed_mask)
if torch.isnan(past_values).any() or torch.isnan(labels).any() or torch.isnan(past_time_features).any() or torch.isnan(past_observed_mask).any():
print("past_time_features has empty or missing values.")
else:
print("past_time_features does not have empty or missing values.")
# Forward pass
outputs = model(
past_values=past_values,
future_values=labels, # may need to adjust shape
past_time_features=past_time_features,
past_observed_mask=past_observed_mask
)
loss = criterion(outputs.last_hidden_state, labels)
# Backward pass and optimize
loss.backward()
optimizer.step()
total_loss += loss.item()
print(f"Epoch {epoch+1}/{2}, Loss: {total_loss/len(dataloader)}")
But even though there is no None value in any tensor, I am getting this error;
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-25-0943f1126085> in <cell line: 22>()
42
43 # Forward pass
---> 44 outputs = model(
45 past_values=past_values,
46 future_values=labels, # may need to adjust shape
6 frames
/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py in _wrapped_call_impl(self, *args, **kwargs)
1516 return self._compiled_call_impl(*args, **kwargs) # type: ignore[misc]
1517 else:
-> 1518 return self._call_impl(*args, **kwargs)
1519
1520 def _call_impl(self, *args, **kwargs):
/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py in _call_impl(self, *args, **kwargs)
1525 or _global_backward_pre_hooks or _global_backward_hooks
1526 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1527 return forward_call(*args, **kwargs)
1528
1529 try:
/usr/local/lib/python3.10/dist-packages/transformers/models/informer/modeling_informer.py in forward(self, past_values, past_time_features, past_observed_mask, static_categorical_features, static_real_features, future_values, future_time_features, future_observed_mask, decoder_attention_mask, head_mask, decoder_head_mask, cross_attn_head_mask, encoder_outputs, past_key_values, output_hidden_states, output_attentions, use_cache, return_dict)
1818 use_cache = False
1819
-> 1820 outputs = self.model(
1821 past_values=past_values,
1822 past_time_features=past_time_features,
/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py in _wrapped_call_impl(self, *args, **kwargs)
1516 return self._compiled_call_impl(*args, **kwargs) # type: ignore[misc]
1517 else:
-> 1518 return self._call_impl(*args, **kwargs)
1519
1520 def _call_impl(self, *args, **kwargs):
/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py in _call_impl(self, *args, **kwargs)
1525 or _global_backward_pre_hooks or _global_backward_hooks
1526 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1527 return forward_call(*args, **kwargs)
1528
1529 try:
/usr/local/lib/python3.10/dist-packages/transformers/models/informer/modeling_informer.py in forward(self, past_values, past_time_features, past_observed_mask, static_categorical_features, static_real_features, future_values, future_time_features, decoder_attention_mask, head_mask, decoder_head_mask, cross_attn_head_mask, encoder_outputs, past_key_values, output_hidden_states, output_attentions, use_cache, return_dict)
1640 return_dict = return_dict if return_dict is not None else self.config.use_return_dict
1641
-> 1642 transformer_inputs, loc, scale, static_feat = self.create_network_inputs(
1643 past_values=past_values,
1644 past_time_features=past_time_features,
/usr/local/lib/python3.10/dist-packages/transformers/models/informer/modeling_informer.py in create_network_inputs(self, past_values, past_time_features, static_categorical_features, static_real_features, past_observed_mask, future_values, future_time_features)
1514 # time feature
1515 time_feat = (
-> 1516 torch.cat(
1517 (
1518 past_time_features[:, self._past_length - self.config.context_length :, ...],
TypeError: expected Tensor as element 1 in argument 0, but got NoneType
Can someone help me about this please? What is my mistake and how can I fix this?