trainer.train()
Author: ARS1979ie
Pull request: no
Labels: name: bug

### System Info

hello
I still have error as below on distilbert-base-uncased on jupyter!!!!!!
please help me


--------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[90], line 9
      2 trainer = Trainer(
      3     model=model,
      4     args=training_args,
      5     train_dataset=train_dataset,
      6     eval_dataset=val_dataset
      7 )
      8 # شروع آموزش
----> 9 trainer.train()

File ~\anaconda3\Lib\site-packages\transformers\trainer.py:2241, in Trainer.train(self, resume_from_checkpoint, trial, ignore_keys_for_eval, **kwargs)
   2239         hf_hub_utils.enable_progress_bars()
   2240 else:
-> 2241     return inner_training_loop(
   2242         args=args,
   2243         resume_from_checkpoint=resume_from_checkpoint,
   2244         trial=trial,
   2245         ignore_keys_for_eval=ignore_keys_for_eval,
   2246     )

File ~\anaconda3\Lib\site-packages\transformers\trainer.py:2548, in Trainer._inner_training_loop(self, batch_size, args, resume_from_checkpoint, trial, ignore_keys_for_eval)
   2541 context = (
   2542     functools.partial(self.accelerator.no_sync, model=model)
   2543     if i != len(batch_samples) - 1
   2544     and self.accelerator.distributed_type != DistributedType.DEEPSPEED
   2545     else contextlib.nullcontext
   2546 )
   2547 with context():
-> 2548     tr_loss_step = self.training_step(model, inputs, num_items_in_batch)
   2550 if (
   2551     args.logging_nan_inf_filter
   2552     and not is_torch_xla_available()
   2553     and (torch.isnan(tr_loss_step) or torch.isinf(tr_loss_step))
   2554 ):
   2555     # if loss is nan or inf simply add the average of previous logged losses
   2556     tr_loss = tr_loss + tr_loss / (1 + self.state.global_step - self._globalstep_last_logged)

File ~\anaconda3\Lib\site-packages\transformers\trainer.py:3698, in Trainer.training_step(self, model, inputs, num_items_in_batch)
   3695     return loss_mb.reduce_mean().detach().to(self.args.device)
   3697 with self.compute_loss_context_manager():
-> 3698     loss = self.compute_loss(model, inputs, num_items_in_batch=num_items_in_batch)
   3700 del inputs
   3701 if (
   3702     self.args.torch_empty_cache_steps is not None
   3703     and self.state.global_step % self.args.torch_empty_cache_steps == 0
   3704 ):

File ~\anaconda3\Lib\site-packages\transformers\trainer.py:3759, in Trainer.compute_loss(self, model, inputs, return_outputs, num_items_in_batch)
   3757         loss_kwargs["num_items_in_batch"] = num_items_in_batch
   3758     inputs = {**inputs, **loss_kwargs}
-> 3759 outputs = model(**inputs)
   3760 # Save past state if it exists
   3761 # TODO: this needs to be fixed and made cleaner later.
   3762 if self.args.past_index >= 0:

File ~\anaconda3\Lib\site-packages\torch\nn\modules\module.py:1739, in Module._wrapped_call_impl(self, *args, **kwargs)
   1737     return self._compiled_call_impl(*args, **kwargs)  # type: ignore[misc]
   1738 else:
-> 1739     return self._call_impl(*args, **kwargs)

File ~\anaconda3\Lib\site-packages\torch\nn\modules\module.py:1845, in Module._call_impl(self, *args, **kwargs)
   1842     return inner()
   1844 try:
-> 1845     return inner()
   1846 except Exception:
   1847     # run always called hooks if they have not already been run
   1848     # For now only forward hooks have the always_call option but perhaps
   1849     # this functionality should be added to full backward hooks as well.
   1850     for hook_id, hook in _global_forward_hooks.items():

File ~\anaconda3\Lib\site-packages\torch\nn\modules\module.py:1793, in Module._call_impl.<locals>.inner()
   1790     bw_hook = BackwardHook(self, full_backward_hooks, backward_pre_hooks)
   1791     args = bw_hook.setup_input_hook(args)
-> 1793 result = forward_call(*args, **kwargs)
   1794 if _global_forward_hooks or self._forward_hooks:
   1795     for hook_id, hook in (
   1796         *_global_forward_hooks.items(),
   1797         *self._forward_hooks.items(),
   1798     ):
   1799         # mark that always called hook is run

File ~\anaconda3\Lib\site-packages\transformers\models\distilbert\modeling_distilbert.py:977, in DistilBertForSequenceClassification.forward(self, input_ids, attention_mask, head_mask, inputs_embeds, labels, output_attentions, output_hidden_states, return_dict)
    969 r"""
    970 labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
    971     Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
    972     config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
    973     `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
    974 """
    975 return_dict = return_dict if return_dict is not None else self.config.use_return_dict
--> 977 distilbert_output = self.distilbert(
    978     input_ids=input_ids,
    979     attention_mask=attention_mask,
    980     head_mask=head_mask,
    981     inputs_embeds=inputs_embeds,
    982     output_attentions=output_attentions,
    983     output_hidden_states=output_hidden_states,
    984     return_dict=return_dict,
    985 )
    986 hidden_state = distilbert_output[0]  # (bs, seq_len, dim)
    987 pooled_output = hidden_state[:, 0]  # (bs, dim)

File ~\anaconda3\Lib\site-packages\torch\nn\modules\module.py:1739, in Module._wrapped_call_impl(self, *args, **kwargs)
   1737     return self._compiled_call_impl(*args, **kwargs)  # type: ignore[misc]
   1738 else:
-> 1739     return self._call_impl(*args, **kwargs)

File ~\anaconda3\Lib\site-packages\torch\nn\modules\module.py:1845, in Module._call_impl(self, *args, **kwargs)
   1842     return inner()
   1844 try:
-> 1845     return inner()
   1846 except Exception:
   1847     # run always called hooks if they have not already been run
   1848     # For now only forward hooks have the always_call option but perhaps
   1849     # this functionality should be added to full backward hooks as well.
   1850     for hook_id, hook in _global_forward_hooks.items():

File ~\anaconda3\Lib\site-packages\torch\nn\modules\module.py:1793, in Module._call_impl.<locals>.inner()
   1790     bw_hook = BackwardHook(self, full_backward_hooks, backward_pre_hooks)
   1791     args = bw_hook.setup_input_hook(args)
-> 1793 result = forward_call(*args, **kwargs)
   1794 if _global_forward_hooks or self._forward_hooks:
   1795     for hook_id, hook in (
   1796         *_global_forward_hooks.items(),
   1797         *self._forward_hooks.items(),
   1798     ):
   1799         # mark that always called hook is run

File ~\anaconda3\Lib\site-packages\transformers\models\distilbert\modeling_distilbert.py:776, in DistilBertModel.forward(self, input_ids, attention_mask, head_mask, inputs_embeds, output_attentions, output_hidden_states, return_dict)
    774     input_shape = inputs_embeds.size()[:-1]
    775 else:
--> 776     raise ValueError("You have to specify either input_ids or inputs_embeds")
    778 device = input_ids.device if input_ids is not None else inputs_embeds.device
    780 head_mask_is_none = head_mask is None

ValueError: You have to specify either input_ids or inputs_embeds

Click to add a cell.

### Who can help?

_No response_

### 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

from transformers import DistilBertTokenizer, DistilBertForSequenceClassification
import torch
import os

os.environ["HF_HUB_DISABLE_SYMLINKS_WARNING"] = "1"

# بارگذاری توکنایزر و مدل پیش‌آموزش‌یافته
model_name = "distilbert-base-uncased"
tokenizer = DistilBertTokenizer.from_pretrained(model_name)

# بارگذاری مدل
model = DistilBertForSequenceClassification.from_pretrained(model_name, num_labels=2)  # مثال برای دو کلاس

print("مدل و توکنایزر با موفقیت بارگذاری شدند!")


sample_text = "این یک تست است"
tokens = tokenizer(sample_text, truncation=True, padding=True, max_length=64)
print(tokens)

# بارگذاری و آماده‌سازی داده‌ها
file_path = 'C:\\Users\\saada\\ARS\\tasnim_cleaned_label_encoded_label.xlsx'  # مسیر فایل ورودی با داده‌ها

# نمایش چند سطر اول از داده‌ها
print("نمونه‌ای از داده‌ها:")
print(df.head())

# فرض می‌کنیم فایل شامل ستون‌های 'text' و 'label' باشد
texts = df['title'].tolist()  # متن‌ها
labels = df['label'].tolist()  # برچسب‌ها

# تبدیل داده‌ها به Dataset
from datasets import Dataset
train_dataset = Dataset.from_dict({"text": train_texts, "label": train_labels})
val_dataset = Dataset.from_dict({"text": val_texts, "label": val_labels})

# توکنایز کردن متن‌ها
encodings = tokenizer(texts, truncation=True, padding=True, max_length=64, return_tensors="pt")

# نمایش داده‌های توکنایز شده
print("متن‌ها با موفقیت توکنایز شدند!")
print(encodings)

from torch.utils.data import DataLoader, Dataset
import torch

# تعریف کلاس Dataset
class CustomDataset(Dataset):
    def __init__(self, encodings, labels):
        self.encodings = encodings
        self.labels = labels

    def __len__(self):
        return len(self.labels)

    def __getitem__(self, idx):
        return {
            'input_ids': self.encodings['input_ids'][idx],
            'attention_mask': self.encodings['attention_mask'][idx],
            'labels': torch.tensor(self.labels[idx])
        }

# ساخت Dataset سفارشی
dataset = CustomDataset(encodings, labels)
print(dataset[0])  # بررسی اولین نمونه

from sklearn.model_selection import train_test_split

train_texts, val_texts, train_labels, val_labels = train_test_split(
    texts, labels, test_size=0.2, random_state=42
)

# توکنایز کردن داده‌های تقسیم‌شده
train_encodings = tokenizer(train_texts, truncation=True, padding=True, max_length=64, return_tensors="pt")
val_encodings = tokenizer(val_texts, truncation=True, padding=True, max_length=64, return_tensors="pt")

from transformers import Trainer, TrainingArguments
import os
# غیرفعال کردن WandB
os.environ["WANDB_MODE"] = "disabled"
os.environ["WANDB_PROJECT"] = "disabled"
os.environ["WANDB_DISABLE"] = "true"

# مدل و داده‌ها
model = DistilBertForSequenceClassification.from_pretrained("distilbert-base-uncased", num_labels=8)

# تنظیمات آموزش
training_args = TrainingArguments(
    per_device_train_batch_size=2,
    per_device_eval_batch_size=2,
    num_train_epochs=3,
    learning_rate=2e-5,
    output_dir="./results",
    evaluation_strategy="epoch"
)


#اجری مدل
trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=train_dataset,
    eval_dataset=val_dataset
)
# شروع آموزش
trainer.train()


### Expected behavior

run properly huggingface/transformers/distilbert-base-uncased

Comments:
This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.