1. 背景
使用 超算互聯網 的云服務,不能連接外網,只能把模型下載到本地,再上傳上去到云服務。
2. 模型下載
在 模型中 https://huggingface.co/models 找到所需的模型后
點擊下載 config.json pytorch_model.bin vocab.txt
3. 上傳模型文件
打開文件管理 e-file
點擊 上傳,選擇 本地上傳
4. 運行代碼
由原來的代碼:
from transformers import BertTokenizer, BertForSequenceClassificationmodel = BertForSequenceClassification.from_pretrained('bert-base-chinese', num_labels=2)
tokenizer = BertTokenizer.from_pretrained('bert-base-chinese')
改成這樣的:
from transformers import BertTokenizer, BertForSequenceClassificationpath_to_local_model_directory="/public/home/acc5trotmy/jupyter/models/bert-base-chinese"model = BertForSequenceClassification.from_pretrained(path_to_local_model_directory, num_labels=2)
tokenizer = BertTokenizer.from_pretrained(path_to_local_model_directory)
或者:
from transformers import BertTokenizer, BertForSequenceClassification, BertConfigconfig = BertConfig.from_pretrained(path_to_local_model_directory)
model = BertForSequenceClassification.from_pretrained(path_to_local_model_directory, config=config)
tokenizer = BertTokenizer.from_pretrained(path_to_local_model_directory)