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)