What do I do to fix AutoModel.from_pretrained to make it auto model with lm head
What do I do to fix AutoModel.from_pretrained to make it auto model with lm head
To load a pre-trained model with its language modeling (LM) head using the Hugging Face `transformers` library, you should use the appropriate `AutoModelFor...` class instead of the base `AutoModel`. Here's how to do it: ### 1. **Choose the Correct Auto Class** Select the `AutoModel` variant that matches your model's purpose: - **Causal Language Modeling** (e.g., GPT): `AutoModelForCausalLM` - **Masked Language Modeling** (e.g., BERT): `AutoModelForMaskedLM` - **Sequence-to-Sequence** (e.g., T5, BART): `AutoModelForSeq2SeqLM` ### 2. **Load the Model** Replace `AutoModel` with the correct class for your task:
1 2 3 4 5 6 | from transformers import AutoModelForCausalLM, AutoTokenizer # Example: Load GPT-2 with LM head for text generation model_name = "gpt2" model = AutoModelForCausalLM.from_pretrained(model_name) tokenizer = AutoTokenizer.from_pretrained(model_name) |
1 2 3 4 5 | from transformers import AutoModelForMaskedLM # Example: Load BERT with MLM head for masked prediction model_name = "bert-base-uncased" model = AutoModelForMaskedLM.from_pretrained(model_name) |
1 2 3 | input_text = "The capital of France is [MASK]." inputs = tokenizer(input_text, return_tensors="pt") outputs = model(**inputs) |
Testing if the previous output contains the string AutoModelForCausalLM
: True