iT邦幫忙

2025 iThome 鐵人賽

DAY 8
0
生成式 AI

我的 AI 學習之路:30天 Gemma 與 Gemini系列 第 8

我的 AI 學習之路:第8天 Gemma 與 Gemini - Hugging Face 使用 Gemma 3 270m

  • 分享至 

  • xImage
  •  

Gemma 3 270m 模型簡介

Gemma 3 270M 是 Google DeepMind 最新推出的極致輕量且高效率的文本模型,具備以下優勢:
-強大的指令追蹤能力與文本結構生成能力 
- 極低功耗:在手機上執行多輪對話也僅耗 ~0.75% 電量
- 支援 32K 上下文、超過 140 種語言
- 多平台、雲端與行動設備皆易於部署
- 提供 fine-tune 與量化方案,加速模型客製化與資源最佳化

輸入存取權杖 (HF_TOKEN 和值) 後,您可以使用下列程式碼,在 Colab 筆記本環境中存取及設定權杖:

from huggingface_hub import login
login(new_session=False)

使用 Gemma3 270m

# Use a pipeline as a high-level helper
# 直接載入模型與分詞器(比 pipeline 更靈活)
from transformers import pipeline

# 建立一個 text-generation 的管線,並指定模型為 google/gemma-3-270m

pipe = pipeline("text-generation", model="google/gemma-3-270m")
# Load model directly
# 使用 pipeline 作為高階介面,快速建立文字生成器
# 直接載入模型與分詞器(比 pipeline 更靈活)
from transformers import AutoTokenizer, AutoModelForCausalLM

# 載入對應的分詞器(Tokenizer),將輸入文字轉成模型可處理的 token
tokenizer = AutoTokenizer.from_pretrained("google/gemma-3-270m")

# 載入因果語言模型(Causal Language Model),用於文字生成任務
model = AutoModelForCausalLM.from_pretrained("google/gemma-3-270m")
# 準備輸入文字
input_text = "gemma3家族介紹,用繁體中文回答"

# 使用分詞器將文字轉換為模型輸入格式 (tensor),並放到 GPU 上
input_ids = tokenizer(input_text, return_tensors="pt").to("cuda")

# Move the model to the GPU
# 將模型移到 GPU(CUDA 裝置),以加速推論
model.to("cuda")

# Generate text
# 進行文字生成,設定最大輸出長度為 50 個 token
outputs = model.generate(**input_ids, max_length=50)

# 將生成的 token 序列轉回可讀文字
generated_text = tokenizer.decode(outputs[0])

# 印出生成結果
print(generated_text)

回答

<bos>gemma3家族介紹,用繁體中文回答:在日本,有很多人用翻譯網站的翻譯網站,用翻譯網站的翻譯,是非常有效率的,而且翻譯非常簡單,只要把你要翻譯的短文或

總結

使用 Gemma3 270m 的模型,沒有回答的很正確
看來真的是用來 "任務明確" 的方式


上一篇
我的 AI 學習之路:第7天 Gemma 與 Gemini - Hugging Face Transformers
下一篇
我的 AI 學習之路:第9天 Gemma 與 Gemini - Kaggle 使用 Gemma
系列文
我的 AI 學習之路:30天 Gemma 與 Gemini12
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言