ValueError: Model Not Returning Loss from Inputs in Trainer.train()

I am encountering a ValueError while training my model using the Hugging Face Transformers library. The error message states that the model did not return a loss from the inputs, only the following keys: start_logits, end_logits. However, the inputs it received are input_ids, token_type_ids, attention_mask.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>
# Load smaller model
model = BertForQuestionAnswering.from_pretrained('bert-base-uncased')
# Define training arguments (using eval_strategy instead of evaluation_strategy)
training_args = TrainingArguments(
output_dir='./results',
eval_strategy='epoch', # Update for compatibility
learning_rate=2e-5,
per_device_train_batch_size=4, # Further reduce batch size
per_device_eval_batch_size=4, # Further reduce batch size
num_train_epochs=3,
weight_decay=0.01,
save_steps=100, # Save model less frequently to reduce memory usage
save_total_limit=1 # Limit the number of saved checkpoints to reduce memory usage
)
def compute_loss(model, inputs):
start_logits, end_logits = model(**inputs)
labels = inputs["start_positions"] # Assuming "start_positions" is your ground truth label key
# MLM loss calculation (replace with your preferred loss function)
loss = F.cross_entropy(start_logits, labels, reduction="mean")
return loss
# Trainer
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=train_dataset, # Use train_dataset for simplicity, ideally you should split a validation set
compute_metrics=compute_loss
)
# Train model
trainer.train()
</code>
<code> # Load smaller model model = BertForQuestionAnswering.from_pretrained('bert-base-uncased') # Define training arguments (using eval_strategy instead of evaluation_strategy) training_args = TrainingArguments( output_dir='./results', eval_strategy='epoch', # Update for compatibility learning_rate=2e-5, per_device_train_batch_size=4, # Further reduce batch size per_device_eval_batch_size=4, # Further reduce batch size num_train_epochs=3, weight_decay=0.01, save_steps=100, # Save model less frequently to reduce memory usage save_total_limit=1 # Limit the number of saved checkpoints to reduce memory usage ) def compute_loss(model, inputs): start_logits, end_logits = model(**inputs) labels = inputs["start_positions"] # Assuming "start_positions" is your ground truth label key # MLM loss calculation (replace with your preferred loss function) loss = F.cross_entropy(start_logits, labels, reduction="mean") return loss # Trainer trainer = Trainer( model=model, args=training_args, train_dataset=train_dataset, eval_dataset=train_dataset, # Use train_dataset for simplicity, ideally you should split a validation set compute_metrics=compute_loss ) # Train model trainer.train() </code>

# Load smaller model
model = BertForQuestionAnswering.from_pretrained('bert-base-uncased')

# Define training arguments (using eval_strategy instead of evaluation_strategy)
training_args = TrainingArguments(
  output_dir='./results',
  eval_strategy='epoch',  # Update for compatibility
  learning_rate=2e-5,
  per_device_train_batch_size=4,  # Further reduce batch size
  per_device_eval_batch_size=4,  # Further reduce batch size
  num_train_epochs=3,
  weight_decay=0.01,
  save_steps=100,                  # Save model less frequently to reduce memory usage
  save_total_limit=1               # Limit the number of saved checkpoints to reduce memory usage
)

def compute_loss(model, inputs):
  start_logits, end_logits = model(**inputs)
  labels = inputs["start_positions"]  # Assuming "start_positions" is your ground truth label key

  # MLM loss calculation (replace with your preferred loss function)
  loss = F.cross_entropy(start_logits, labels, reduction="mean")

  return loss

# Trainer
trainer = Trainer(
  model=model,
  args=training_args,
  train_dataset=train_dataset,
  eval_dataset=train_dataset,  # Use train_dataset for simplicity, ideally you should split a validation set
  compute_metrics=compute_loss
)

# Train model
trainer.train()

How can I resolve this issue and ensure that the model returns the loss from the inputs during training?

I tried chatgpt and Gemini to get help and try to write code different way but didnt help.

New contributor

O.T L is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật