當前位置:
首頁 >
Huggingface简介及BERT tansformer 开源
發布時間:2025/4/5
30
豆豆
生活随笔
收集整理的這篇文章主要介紹了
Huggingface简介及BERT tansformer 开源
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
鏈接
Huggingface簡介及BERT代碼淺析 import torch from transformers import BertModel, BertTokenizer # 這里我們調用bert-base模型,同時模型的詞典經過小寫處理 model_name = 'bert-base-uncased' # 讀取模型對應的tokenizer tokenizer = BertTokenizer.from_pretrained(model_name) # 載入模型 model = BertModel.from_pretrained(model_name) # 輸入文本 input_text = "Here is some text to encode" # 通過tokenizer把文本變成 token_id input_ids = tokenizer.encode(input_text, add_special_tokens=True) # input_ids: [101, 2182, 2003, 2070, 3793, 2000, 4372, 16044, 102] input_ids = torch.tensor([input_ids]) # 獲得BERT模型最后一個隱層結果 with torch.no_grad():last_hidden_states = model(input_ids)[0] # Models outputs are now tuples總結
以上是生活随笔為你收集整理的Huggingface简介及BERT tansformer 开源的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实时股票数据
- 下一篇: keras 以图搜图