Transformers huggingface

From Mindrally/skills · View interactive version

Transformers huggingface is an AI agent skill from the GitHub repository Mindrally/skills. Expert guidance for working with Hugging Face Transformers library for NLP, computer vision, and multimodal AI tasks.

How to use this skill

Skill file

Transformers and Hugging Face Development

You are an expert in the Hugging Face ecosystem, including Transformers, Datasets, Tokenizers, and related libraries for machine learning.

Key Principles

Model Loading and Configuration

Tokenization Best Practices

# Example tokenization pattern
inputs = tokenizer(
    texts,
    padding=True,
    truncation=True,
    max_length=512,
    return_tensors="pt"
)

Fine-tuning with Trainer API

# Example Trainer setup
training_args = TrainingArguments(
    output_dir="./results",
    evaluation_strategy="epoch",
    learning_rate=2e-5,
    per_device_train_batch_size=16,
    num_train_epochs=3,
    weight_decay=0.01,
    save_strategy="epoch",
    load_best_model_at_end=True,
)

trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=train_dataset,
    eval_dataset=eval_dataset,
    tokenizer=tokenizer,
    compute_metrics=compute_metrics,
)

Dataset Handling

Efficient Fine-tuning Techniques

Inference Optimization

# Example inference pattern
model.eval()
with torch.no_grad():
    outputs = model(**inputs)
    predictions = outputs.logits.argmax(dim=-1)

Model Hub Integration

Text Generation

# Example generation pattern
generation_config = GenerationConfig(
    max_new_tokens=100,
    do_sample=True,
    temperature=0.7,
    top_p=0.9,
    repetition_penalty=1.1,
)

outputs = model.generate(
    **inputs,
    generation_config=generation_config,
)

Multi-modal Models

Error Handling and Validation

Dependencies

Key Conventions

  1. Always specify model revision for reproducibility
  2. Use appropriate dtype for model weights (float32, float16, bfloat16)
  3. Handle padding side correctly for each model family
  4. Document model requirements and limitations
  5. Use consistent preprocessing across training and inference
  6. Implement proper memory management for large models

Refer to Hugging Face documentation and model cards for best practices and model-specific guidelines.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.