iT邦幫忙

2025 iThome 鐵人賽

DAY 3
0
佛心分享-IT 人自學之術

學習 LLM系列 第 3

Day 3:第一個模型體驗

  • 分享至 

  • xImage
  •  

i> 安裝 PyTorch
!pip install transformers datasets torch
https://ithelp.ithome.com.tw/upload/images/20250917/20169173eibicAhZR6.png

ii> 匯入 pipeline
from transformers import pipeline
https://ithelp.ithome.com.tw/upload/images/20250917/20169173RNfiOEIhS2.png

iii> 建立情感分析器
classifier = pipeline("sentiment-analysis")
** sentiment-analysis 是情感分析器
https://ithelp.ithome.com.tw/upload/images/20250917/20169173909gKdueeD.png

iv> 測試單句
result = classifier("I love learning LLMs with Hugging Face!") print(result)
https://ithelp.ithome.com.tw/upload/images/20250917/20169173ObGAS8GQJ7.png

  • 會輸出兩個 Key,一個是分類標籤 laber,一個是信心分數 score

v> 測試多句
texts = [ "I love this movie!", "This is the worst day of my life." ] results = classifier(texts) for t, r in zip(texts, results): print(t, "->", r)
https://ithelp.ithome.com.tw/upload/images/20250917/20169173LT2919yzgb.png

#中文情感分析

classifier = pipeline(
    "sentiment-analysis",
    model="uer/roberta-base-finetuned-jd-binary-chinese"
)

print(classifier("這部電影真的很好看!"))
print(classifier("這個產品太爛了,我很失望。"))
  • uer/roberta-base-finetuned-jd-binary-chinese 是專門做中文二分類情感分析的模型
    https://ithelp.ithome.com.tw/upload/images/20250917/20169173tEl9nY46Vq.png

上一篇
Day2 準備與安裝實作 LLM 所需環境
下一篇
Day4 文本生成
系列文
學習 LLM4
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言