model = EncoderDecoderModel.from_encoder_decoder_pretrained('aubmindlab/bert-base-arabert', 'aubmindlab/bert-base-arabert')
#tokenizer = AutoTokenizer.from_pretrained("UBC-NLP/AraT5-base")#flax-community/arabic-t5-small")
#model = AutoModelForSeq2SeqLM.from_pretrained("UBC-NLP/AraT5-base")#flax-community/arabic-t5-small")
tokenizer_config.json: 100%
637/637 [00:00<00:00, 52.5kB/s]
vocab.txt: 100%
717k/717k [00:00<00:00, 44.9MB/s]
tokenizer.json: 100%
2.26M/2.26M [00:00<00:00, 5.97MB/s]
special_tokens_map.json: 100%
112/112 [00:00<00:00, 7.20kB/s]
config.json: 100%
578/578 [00:00<00:00, 39.3kB/s]
model.safetensors: 100%
543M/543M [00:01<00:00, 394MB/s]
Some weights of BertLMHeadModel were not initialized from the model checkpoint at aubmindlab/bert-base-arabert and are newly initialized: ['bert.encoder.layer.8.crossattention.self.key.weight', 'bert.encoder.layer.5.crossattention.self.query.weight', 'bert.encoder.layer.11.crossattention.self.value.weight', 'bert.encoder.layer.10.crossattention.self.query.weight', 'bert.encoder.layer.1.crossattention.output.dense.weight', 'bert.encoder.layer.0.crossattention.self.value.bias', 'bert.encoder.layer.1.crossattention.self.key.weight', 'bert.encoder.layer.3.crossattention.output.LayerNorm.bias', 'bert.encoder.layer.0.crossattention.self.key.weight', 'bert.encoder.layer.2.crossattention.output.dense.weight', 'bert.encoder.layer.9.crossattention.self.value.weight', 'bert.encoder.layer.4.crossattention.self.query.bias', 'bert.encoder.layer.0.crossattention.self.query.weight', 'bert.encoder.layer.4.crossattention.output.dense.weight', 'bert.encoder.layer.4.crossattention.self.query.weight', 'bert.encoder.layer.8.crossattention.output.LayerNorm.weight', 'bert.encoder.layer.2.crossattention.output.dense.bias', 'bert.encoder.layer.11.crossattention.self.key.bias', 'bert.encoder.layer.8.crossattention.self.value.weight', 'bert.encoder.layer.9.crossattention.output.dense.weight', 'bert.encoder.layer.5.crossattention.output.dense.bias', 'bert.encoder.layer.7.crossattention.self.key.bias', 'bert.encoder.layer.4.crossattention.output.LayerNorm.weight', 'bert.encoder.layer.1.crossattention.self.query.bias', 'bert.encoder.layer.7.crossattention.output.LayerNorm.weight', 'bert.encoder.layer.11.crossattention.output.LayerNorm.weight', 'bert.encoder.layer.8.crossattention.self.key.bias', 'bert.encoder.layer.4.crossattention.output.LayerNorm.bias', 'bert.encoder.layer.2.crossattention.self.value.weight', 'bert.encoder.layer.6.crossattention.output.dense.bias', 'bert.encoder.layer.7.crossattention.self.value.weight', 'bert.encoder.layer.6.crossattention.self.query.bias', 'bert.encoder.layer.5.crossattention.self.value.weight', 'bert.encoder.layer.7.crossattention.self.query.weight', 'bert.encoder.layer.10.crossattention.self.query.bias', 'bert.encoder.layer.4.crossattention.self.key.weight', 'bert.encoder.layer.4.crossattention.self.key.bias', 'bert.encoder.layer.6.crossattention.output.LayerNorm.weight', 'bert.encoder.layer.3.crossattention.self.value.bias', 'bert.encoder.layer.6.crossattention.self.key.weight', 'bert.encoder.layer.6.crossattention.output.dense.weight', 'bert.encoder.layer.2.crossattention.output.LayerNorm.weight', 'bert.encoder.layer.10.crossattention.self.value.weight', 'bert.encoder.layer.0.crossattention.output.LayerNorm.weight', 'bert.encoder.layer.11.crossattention.self.value.bias', 'bert.encoder.layer.11.crossattention.output.dense.weight', 'bert.encoder.layer.6.crossattention.self.value.weight', 'bert.encoder.layer.8.crossattention.output.dense.bias', 'bert.encoder.layer.8.crossattention.self.value.bias', 'bert.encoder.layer.11.crossattention.output.LayerNorm.bias', 'bert.encoder.layer.10.crossattention.self.value.bias', 'bert.encoder.layer.11.crossattention.output.dense.bias', 'bert.encoder.layer.3.crossattention.output.dense.bias', 'bert.encoder.layer.9.crossattention.self.key.weight', 'bert.encoder.layer.9.crossattention.self.query.weight', 'bert.encoder.layer.8.crossattention.self.query.bias', 'bert.encoder.layer.7.crossattention.output.dense.weight', 'bert.encoder.layer.2.crossattention.self.query.weight', 'bert.encoder.layer.2.crossattention.output.LayerNorm.bias', 'bert.encoder.layer.4.crossattention.output.dense.bias', 'bert.encoder.layer.3.crossattention.self.key.weight', 'bert.encoder.layer.1.crossattention.output.dense.bias', 'bert.encoder.layer.10.crossattention.output.LayerNorm.weight', 'bert.encoder.layer.7.crossattention.output.dense.bias', 'bert.encoder.layer.0.crossattention.output.LayerNorm.bias', 'bert.encoder.layer.8.crossattention.output.LayerNorm.bias', 'bert.encoder.layer.1.crossattention.self.value.weight', 'bert.encoder.layer.10.crossattention.output.dense.weight', 'bert.encoder.layer.6.crossattention.self.query.weight', 'bert.encoder.layer.6.crossattention.self.value.bias', 'bert.encoder.layer.0.crossattention.self.key.bias', 'bert.encoder.layer.11.crossattention.self.query.weight', 'bert.encoder.layer.5.crossattention.output.dense.weight', 'bert.encoder.layer.1.crossattention.self.key.bias', 'bert.encoder.layer.9.crossattention.output.LayerNorm.weight', 'bert.encoder.layer.11.crossattention.self.key.weight', 'bert.encoder.layer.5.crossattention.self.key.weight', 'bert.encoder.layer.8.crossattention.self.query.weight', 'bert.encoder.layer.3.crossattention.output.LayerNorm.weight', 'bert.encoder.layer.5.crossattention.self.key.bias', 'bert.encoder.layer.8.crossattention.output.dense.weight', 'bert.encoder.layer.1.crossattention.self.query.weight', 'bert.encoder.layer.10.crossattention.output.dense.bias', 'bert.encoder.layer.3.crossattention.self.value.weight', 'bert.encoder.layer.5.crossattention.output.LayerNorm.bias', 'bert.encoder.layer.3.crossattention.self.query.bias', 'bert.encoder.layer.2.crossattention.self.query.bias', 'bert.encoder.layer.3.crossattention.self.key.bias', 'bert.encoder.layer.2.crossattention.self.value.bias', 'bert.encoder.layer.9.crossattention.output.LayerNorm.bias', 'bert.encoder.layer.7.crossattention.self.query.bias', 'bert.encoder.layer.3.crossattention.self.query.weight', 'bert.encoder.layer.5.crossattention.output.LayerNorm.weight', 'bert.encoder.layer.9.crossattention.self.key.bias', 'bert.encoder.layer.2.crossattention.self.key.bias', 'bert.encoder.layer.5.crossattention.self.query.bias', 'bert.encoder.layer.9.crossattention.self.value.bias', 'bert.encoder.layer.10.crossattention.self.key.bias', 'bert.encoder.layer.2.crossattention.self.key.weight', 'bert.encoder.layer.7.crossattention.self.key.weight', 'bert.encoder.layer.4.crossattention.self.value.bias', 'bert.encoder.layer.7.crossattention.self.value.bias', 'bert.encoder.layer.3.crossattention.output.dense.weight', 'bert.encoder.layer.1.crossattention.self.value.bias', 'bert.encoder.layer.5.crossattention.self.value.bias', 'bert.encoder.layer.1.crossattention.output.LayerNorm.weight', 'bert.encoder.layer.6.crossattention.output.LayerNorm.bias', 'bert.encoder.layer.0.crossattention.self.value.weight', 'bert.encoder.layer.11.crossattention.self.query.bias', 'bert.encoder.layer.7.crossattention.output.LayerNorm.bias', 'bert.encoder.layer.9.crossattention.self.query.bias', 'bert.encoder.layer.10.crossattention.self.key.weight', 'bert.encoder.layer.0.crossattention.output.dense.weight', 'bert.encoder.layer.9.crossattention.output.dense.bias', 'bert.encoder.layer.6.crossattention.self.key.bias', 'bert.encoder.layer.4.crossattention.self.value.weight', 'bert.encoder.layer.0.crossattention.self.query.bias', 'bert.encoder.layer.1.crossattention.output.LayerNorm.bias', 'bert.encoder.layer.10.crossattention.output.LayerNorm.bias', 'bert.encoder.layer.0.crossattention.output.dense.bias']
You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.
add Codeadd Markdown
Assistant
The error you're experiencing is coming from an attempt to use a model or a combination of models () that does not inherently support the task of sequence-to-sequence modeling without some further configuration. The expects the to be set in the model's configuration, as it is required for sequence generation tasks.
User
can give me code to use arabert transformer?
Assistant
Certainly! If you're interested in using aubmindlab/bert-base-arabert for a task like sequence classification, here's an example of how you might use it with Hugging Face's Transformers library:
First, ensure you have the Transformers library installed:
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=eval_dataset,
data_collator=lambda data: {
"input_ids": torch.stack([item["input_ids"] for item in data]),
"attention_mask": torch.stack([item["attention_mask"] for item in data]),
"labels": torch.stack([item["labels"] for item in data]),
},
callbacks=[early_stopping], # Add the EarlyStoppingCallback
)
NameError Traceback (most recent call last)
Cell In[13], line 28
21 early_stopping = EarlyStoppingCallback(early_stopping_patience=3, early_stopping_threshold=0.005)
23 # Model trainer
24 trainer = Trainer(
25 model=model,
26 args=training_args,
27 train_dataset=train_dataset,
---> 28 eval_dataset=eval_dataset,
29 data_collator=lambda data: {
30 "input_ids": torch.stack([item["input_ids"] for item in data]),
31 "attention_mask": torch.stack([item["attention_mask"] for item in data]),
32 "labels": torch.stack([item["labels"] for item in data]),
33 },
34 callbacks=[early_stopping], # Add the EarlyStoppingCallback
35 )
NameError: name 'eval_dataset' is not defined
add Codeadd Markdown
from transformers import EarlyStoppingCallback
Model training arguments
training_args = TrainingArguments(
output_dir="./question_generation_bert",
num_train_epochs=3,
auto_find_batch_size=True,
logging_steps=15000,
save_steps=15000,
#warmup_steps=500,
#weight_decay=0.01,
#logging_dir="./logs",
#logging_steps=10,
learning_rate=1e-5,
evaluation_strategy="steps", # Change evaluation strategy to "steps"
report_to=[], # Set report_to to an empty list to disable wandb logging
load_best_model_at_end=True
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=tokenized_train_dataset,
eval_dataset=tokenized_val_dataset,
tokenizer=tokenizer,
data_collator=data_collator,
#optimizer=optimizer,
#compute_metrics=compute_metrics,
#callbacks=[early_stopping], # Add the EarlyStoppingCallback
)
add Codeadd Markdown
trainer.train()
You're using a BertTokenizerFast tokenizer. Please note that with a fast tokenizer, using the __call__ method is faster than using a method to encode the text followed by a call to the pad method to get a padded encoding.
File /opt/conda/lib/python3.10/site-packages/accelerate/utils/memory.py:136, in find_executable_batch_size..decorator(*args, **kwargs)
134 raise RuntimeError("No executable batch size found, reached zero.")
135 try:
--> 136 return function(batch_size, *args, **kwargs)
137 except Exception as e:
138 if should_reduce_batch_size(e):
File /opt/conda/lib/python3.10/site-packages/transformers/trainer.py:1854, in Trainer._inner_training_loop(self, batch_size, args, resume_from_checkpoint, trial, ignore_keys_for_eval)
1851 self.control = self.callback_handler.on_step_begin(args, self.state, self.control)
1853 with self.accelerator.accumulate(model):
-> 1854 tr_loss_step = self.training_step(model, inputs)
1856 if (
1857 args.logging_nan_inf_filter
1858 and not is_torch_tpu_available()
1859 and (torch.isnan(tr_loss_step) or torch.isinf(tr_loss_step))
1860 ):
1861 # if loss is nan or inf simply add the average of previous logged losses
1862 tr_loss += tr_loss / (1 + self.state.global_step - self._globalstep_last_logged)
File /opt/conda/lib/python3.10/site-packages/transformers/trainer.py:2735, in Trainer.training_step(self, model, inputs)
2732 return loss_mb.reduce_mean().detach().to(self.args.device)
2734 with self.compute_loss_context_manager():
-> 2735 loss = self.compute_loss(model, inputs)
2737 if self.args.n_gpu > 1:
2738 loss = loss.mean() # mean() to average on multi-gpu parallel training
File /opt/conda/lib/python3.10/site-packages/transformers/trainer.py:2758, in Trainer.compute_loss(self, model, inputs, return_outputs)
2756 else:
2757 labels = None
-> 2758 outputs = model(**inputs)
2759 # Save past state if it exists
2760 # TODO: this needs to be fixed and made cleaner later.
2761 if self.args.past_index >= 0:
File /opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py:1501, in Module._call_impl(self, *args, **kwargs)
1496 # If we don't have any hooks, we want to skip the rest of the logic in
1497 # this function, and just call forward.
1498 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
1499 or _global_backward_pre_hooks or _global_backward_hooks
1500 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1501 return forward_call(*args, **kwargs)
1502 # Do not call functions when jit is used
1503 full_backward_hooks, non_full_backward_hooks = [], []
File /opt/conda/lib/python3.10/site-packages/transformers/models/encoder_decoder/modeling_encoder_decoder.py:616, in EncoderDecoderModel.forward(self, input_ids, attention_mask, decoder_input_ids, decoder_attention_mask, encoder_outputs, past_key_values, inputs_embeds, decoder_inputs_embeds, labels, use_cache, output_attentions, output_hidden_states, return_dict, **kwargs)
613 encoder_hidden_states = self.enc_to_dec_proj(encoder_hidden_states)
615 if (labels is not None) and (decoder_input_ids is None and decoder_inputs_embeds is None):
--> 616 decoder_input_ids = shift_tokens_right(
617 labels, self.config.pad_token_id, self.config.decoder_start_token_id
618 )
619 if decoder_attention_mask is None:
620 decoder_attention_mask = decoder_input_ids.new_tensor(decoder_input_ids != self.config.pad_token_id)
File /opt/conda/lib/python3.10/site-packages/transformers/models/encoder_decoder/modeling_encoder_decoder.py:158, in shift_tokens_right(input_ids, pad_token_id, decoder_start_token_id)
156 shifted_input_ids[:, 1:] = input_ids[:, :-1].clone()
157 if decoder_start_token_id is None:
--> 158 raise ValueError("Make sure to set the decoder_start_token_id attribute of the model's configuration.")
159 shifted_input_ids[:, 0] = decoder_start_token_id
161 if pad_token_id is None:
ValueError: Make sure to set the decoder_start_token_id attribute of the model's configuration. how fix error
EncoderDecoderModel
EncoderDecoderModel
decoder_start_token_id
The decoder_start_token_id is a special token used to start the decoding process when generating sequences in a sequence-to-sequence model. The error message tells you that this token id is not set, which is necessary for the model to work properly.
To fix this, you should set the decoder_start_token_id attribute in the configuration of the model. For example, if you are using BERT for both the encoder and decoder in a sequence-to-sequence fashion, keep in mind that BERT is not specifically designed for sequence-to-sequence tasks like T5 or BART. However, if you still want to use this architecture for such a task, you need to configure it to assign a token as the starting token for the decoder.
Here is how you can set the decoder_start_token_id in your configuration:
python
# Assuming `model` is already defined as an instance of EncoderDecoderModel
# You need to define a suitable `decoder_start_token_id`
# For BERT-based models, the `cls_token_id` is often a good choice
cls_token_id = tokenizer.cls_token_id # or use an id corresponding to a token you want to be the start token
# Set the decoder_start_token_id in the configuration of the model
model.config.decoder_start_token_id = cls_token_id
# Now you can proceed with training
trainer.train()
Make sure to replace tokenizer with the actual tokenizer instance you are using and model with your model instance. If you are generating textual content like questions, you can also choose to use the token ID that corresponds to the period . or any other token that typically signifies the start of a sentence.
Additionally, since you're using an EncoderDecoderModel with BERT as both the encoder and decoder, but it's not typically used for sequence-to-sequence tasks, you may want to use a model that is better suited for such tasks like T5 or BART. These models come with predefined configurations, including decoder_start_token_id.
Lastly, if you are unsure about the token ID to use, consider using a model like T5 or BART that is designed for sequence-to-sequence tasks and therefore has this attribute already set up in their configurations.
bash
pip install transformers
Here's a simple code snippet for using aubmindlab/bert-base-arabert for sequence classification:
python
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
# Load the tokenizer and model from the Hugging Face Transformers library
tokenizer = AutoTokenizer.from_pretrained('aubmindlab/bert-base-arabert')
model = AutoModelForSequenceClassification.from_pretrained('aubmindlab/bert-base-arabert', num_labels=2) # specify the number of labels for your classification task
# Example sequence
sequence = "هذا مثال باللغة العربية" # This is an example in Arabic
# Tokenize the sequence
tokens = tokenizer(sequence, padding=True, truncation=True, return_tensors="pt")
# Forward pass, get the logits
logits = model(**tokens).logits
# Convert logits to probabilities
probabilities = torch.softmax(logits, dim=1)
# Output the probabilities
print(probabilities)
# Optionally, convert probabilities to actual class labels
_, predicted_class = torch.max(probabilities, dim=1)
print(f"Predicted class: {predicted_class.item()}")
Please replace the example text with your own data and modify num_labels to match the number of classes for your specific classification task.
Keep in mind that if you're dealing with a task other than sequence classification, like token classification or question answering, you would need to use a corresponding model class (e.g., AutoModelForTokenClassification or AutoModelForQuestionAnswering).
For the case of a sequence-to-sequence task, like text summarization, you would typically use a model that's been specifically designed for that purpose, such as T5 or BART, because BERT-based models like aubmindlab/bert-base-arabert are typically pre-trained for tasks like masked language modeling and next sentence prediction, and they don't have mechanisms like encoder-decoder architecture for generating text sequences.
If you have further questions about how to adapt aubmindlab/bert-base-arabert for your specific task or how to use it in your code, please let me know!