開始使用免費開始

使用 AutoClasses

你已經了解 tokenizer 的運作方式,並探索它在為模型準備文字資料時所扮演的角色。現在更進一步,把 AutoModels 和 AutoTokenizers 與 pipeline() 函式結合。這能在可控性與便利性之間取得良好平衡。

延續情感分析任務,將 AutoClasses 與 pipeline 模組結合使用。

transformers 函式庫中的 AutoModelForSequenceClassificationAutoTokenizerpipeline 已經替你匯入。

本練習屬於課程

善用 Hugging Face

檢視課程

練習說明

  • 下載模型與 tokenizer,並分別存為 my_modelmy_tokenizer
  • 建立 pipeline,並存為 my_pipeline
  • 使用 my_pipeline 進行預測,並存為 output

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Download the model and tokenizer
my_model = ____.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
my_tokenizer = ____.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")

# Create the pipeline
my_pipeline = pipeline(task="sentiment-analysis", ____=____, ____=____)

# Predict the sentiment
output = ____("This course is pretty good, I guess.")
print(f"Sentiment using AutoClasses: {output[0]['label']}")
編輯並執行程式碼