📅 2025-02-03T08:53:53.110Z
👁️ 52 katselukertaa
🔓 Julkinen


<bos>how do I use AutoModelForCausalLM?

The `AutoModelForCausalLM` class from Hugging Face's `transformers` library is used to create a causal language model. Here's a step-by-step guide on how to use it:

1. **Install the Transformers library:**
   ```bash
   pip install transformers
   ```

2. **Import the necessary modules:**
   ```python
   from transformers import AutoModelForCausalLM, AutoTokenizer
   ```

3. **Load a pre-trained model:**
   You can choose from a variety of pre-trained models available in the Hugging Face Model Hub. For example, to load the GPT-2 model:
   ```python
   model_name = "gpt2"
   model = AutoModelForCausalLM.from_pretrained(model_name)
   tokenizer = AutoTokenizer.from_pretrained(model_name)
   ```

4. **Prepare your input text:**
   Tokenize your input text using the tokenizer:
   ```python
   input_text = "Once upon a time"
   input_ids = tokenizer.encode(input_text, return_tensors="pt")
   ```

5. **Generate text:**
   Use the model to generate text based on the input:
   ```python
   output = model.generate(input_ids, max_length=50, num_return_sequences=1)
   generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
   print(generated_text)
   ```

6. **Fine-tune the model (optional):**
   If you want to fine-tune the model on your own dataset, you can use the `Trainer` class from the `transformers` library.

Here's a complete example:

```python
from transformers import AutoModelForCausalLM, AutoTokenizer

# Load pre-trained model and tokenizer
model_name = "gpt2"
model = AutoModelForCausalLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

# Prepare input text
input_text = "Once upon a time"
input_ids = tokenizer.encode(input_text, return_tensors="pt")

# Generate text
output = model.generate(input_ids, max_length=50, num_return_sequences=1)
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
print(generated_text)
```

This code will generate text based on the input text "Once upon a time". You can adjust the `max_length` and `num_return_sequences` parameters to control the length and number of generated outputs.

Remember to refer to the official Hugging Face documentation for more details and advanced usage