Chapter 05
模型加载与保存
Notebooktransformers27 cells
模型加载与保存
In [ ]python · cell 2
python
from transformers import AutoConfig, AutoModel, AutoTokenizer在线加载
In [ ]python · cell 4
python
model = AutoModel.from_pretrained("hfl/rbt3", force_download=True)模型下载(需要科学)
In [ ]python · cell 6
python
!git clone "https://huggingface.co/hfl/rbt3"In [ ]python · cell 7
python
!git lfs clone "https://huggingface.co/hfl/rbt3" --include="*.bin"离线加载
In [ ]python · cell 9
python
model = AutoModel.from_pretrained("rbt3")模型加载参数
In [ ]python · cell 11
python
model = AutoModel.from_pretrained("rbt3")In [ ]python · cell 12
python
model.configIn [ ]python · cell 13
python
config = AutoConfig.from_pretrained("./rbt3/")
configIn [ ]python · cell 14
python
config.output_attentionsIn [ ]python · cell 15
python
from transformers import BertConfig模型调用
In [ ]python · cell 17
python
sen = "弱小的我也有大梦想!"
tokenizer = AutoTokenizer.from_pretrained("rbt3")
inputs = tokenizer(sen, return_tensors="pt")
inputs不带Model Head的模型调用
In [ ]python · cell 19
python
model = AutoModel.from_pretrained("rbt3", output_attentions=True)In [ ]python · cell 20
python
output = model(**inputs)
outputIn [ ]python · cell 21
python
output.last_hidden_state.size()In [ ]python · cell 22
python
len(inputs["input_ids"][0])带Model Head的模型调用
In [ ]python · cell 24
python
from transformers import AutoModelForSequenceClassification, BertForSequenceClassificationIn [ ]python · cell 25
python
clz_model = AutoModelForSequenceClassification.from_pretrained("rbt3", num_labels=10)In [ ]python · cell 26
python
clz_model(**inputs)In [ ]python · cell 27
python
clz_model.config.num_labels