我想要一天分享一點「LLM從底層堆疊的技術」,並且每篇文章長度控制在三分鐘以內,讓大家不會壓力太大,但是又能夠每天成長一點。
!pip install transformers
from transformers import BertTokenizer, BertModel
input_text = "The output shows the attention values" #@param {type:"string"}
from transformers import BertTokenizer, BertModel
model_name = 'bert-base-uncased'
tokenizer = BertTokenizer.from_pretrained(model_name)
model = BertModel.from_pretrained(model_name, output_attentions = True)
tokens = tokenizer.tokenize(input_text)
input_ids = tokenizer.convert_tokens_to_ids(tokens)
inputs = tokenizer.encode_plus(input_text, return_tensors = 'pt')
input_ids = inputs['input_ids']
attention_mask = inputs['attention_mask']
outputs = model(input_ids, attention_mask = attention_mask)
attentions = outputs.attentions
當中註解如下:
- input_text:這是一個字串變數 (input_text),它被賦值為 The output shows the attention values,這段文字表示變數內的內容
- #@param {type:"string"}:這是 Colab Notebooks 中用來指定變數類型的註釋,在 Google Colab 中使用 #@param 可以讓此變數在筆記本的 UI 界面中顯示為可編輯的字段,{type:"string"} 指定了這個參數應該是一個字符串
















