目标:修改此记事本以配合Albert和Distilbert模型
内核:conda_pytorch_p36.我重新启动并运行了所有文件,并在工作目录中刷新了文件视图。
错误发生在第1.2节中,仅针对这两个新模型。
对于文件名等,我创建了一个在任何地方使用的变量:
MODEL_NAME = 'albert-base-v2' # 'distilbert-base-uncased', 'bert-base-uncased'我将进口商品改为:
from transformers import (AutoConfig, AutoModel, AutoTokenizer)
#from transformers import (BertConfig, BertForSequenceClassification, BertTokenizer,)正如变压器文档.自动类中所建议的。
实例化AutoConfig、AutoModel和AutoTokenizer之一将直接创建相关体系结构的类。
第1.2节:
# load model
model = AutoModel.from_pretrained(configs.output_dir) # BertForSequenceClassification
model.to(configs.device)
# quantize model
quantized_model = torch.quantization.quantize_dynamic(
model, {torch.nn.Linear}, dtype=torch.qint8
)
#print(quantized_model)
def print_size_of_model(model):
torch.save(model.state_dict(), "temp.p")
print('Size (MB):', os.path.getsize("temp.p")/(1024*1024))
os.remove('temp.p')
print_size_of_model(model)
print_size_of_model(quantized_model)回溯:
ValueError: Unrecognized model in ./MRPC/. Should have a `model_type` key in its config.json, or contain one of the following strings in its name: imagegpt, qdqbert, vision-encoder-decoder, trocr, fnet, segformer, vision-text-dual-encoder, perceiver, gptj, layoutlmv2, beit, rembert, visual_bert, canine, roformer, clip, bigbird_pegasus, deit, luke, detr, gpt_neo, big_bird, speech_to_text_2, speech_to_text, vit, wav2vec2, m2m_100, convbert, led, blenderbot-small, retribert, ibert, mt5, t5, mobilebert, distilbert, albert, bert-generation, camembert, xlm-roberta, pegasus, marian, mbart, megatron-bert, mpnet, bart, blenderbot, reformer, longformer, roberta, deberta-v2, deberta, flaubert, fsmt, squeezebert, hubert, bert, openai-gpt, gpt2, transfo-xl, xlnet, xlm-prophetnet, prophetnet, xlm, ctrl, electra, speech-encoder-decoder, encoder-decoder, funnel, lxmert, dpr, layoutlm, rag, tapas, splinter, sew-d, sew, unispeech-sat, unispeech, wavlm如果还有什么可以添加到帖子中,请告诉我。
发布于 2022-01-13 14:10:59
解释:
实例化AutoModel时,必须在./MRPC/config.json文件中指定一个model_type参数(在笔记本运行时下载)。
model_types列表可以找到这里。
解决方案:
以相同格式将model_type附加到config.json的代码:
import json
json_filename = './MRPC/config.json'
with open(json_filename) as json_file:
json_decoded = json.load(json_file)
json_decoded['model_type'] = # !!
with open(json_filename, 'w') as json_file:
json.dump(json_decoded, json_file, indent=2, separators=(',', ': '))config.json
{
"attention_probs_dropout_prob": 0.1,
"finetuning_task": "mrpc",
"hidden_act": "gelu",
"hidden_dropout_prob": 0.1,
"hidden_size": 768,
"initializer_range": 0.02,
"intermediate_size": 3072,
"layer_norm_eps": 1e-12,
"max_position_embeddings": 512,
"num_attention_heads": 12,
"num_hidden_layers": 12,
"num_labels": 2,
"output_attentions": false,
"output_hidden_states": false,
"pruned_heads": {},
"torchscript": false,
"type_vocab_size": 2,
"vocab_size": 30522,
"model_type": "albert"
}https://stackoverflow.com/questions/70697470
复制相似问题