当前位置:   article > 正文

预训练模型transformers综合总结(一)_transformers.js

transformers.js

这是我对transformers库查看了原始文档后,进行的学习总结。

第一部分是将如何调用加载本地模型,使用模型,修改模型,保存模型

之后还会更新如何使用自定义的数据集训练以及对模型进行微调,感觉这样这个库基本就能玩熟了。


# 加载本地模型须知

* 1.使用transformers库加载预训练模型,99%的时间都是用于模型的下载。
为此,我直接从清华大学软件("https://mirrors.tuna.tsinghua.edu.cn/hugging-face-models/")把模型放在了我的本地目录地址:"H:\\code\\Model\\"下,这里可以进行修改。

* 2.下载的模型通常会是"模型名称-"+"config.json"的格式例如(bert-base-cased-finetuned-mrpc-config.json),但如果使用transformers库加载本地模型,需要的是模型路径中是config.json、vocab.txt、pytorch_model.bin、tf_model.h5、tokenizer.json等形式,为此在加载前,需要将把文件前面的模型名称,才能加载成功

我自己写的处理代码如下:

  1. #coding=utf-8
  2. import os
  3. import os.path
  4. # 模型存放路径
  5. rootdir = r"H:\code\Model\bert-large-uncased-whole-word-masking-finetuned-squad"# 指明被遍历的文件夹
  6. for parent,dirnames,filenames in os.walk(rootdir):#三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字
  7. for filename in filenames:#文件名
  8. # nameList=filename.split('.')
  9. # print(nameList)
  10. print(filename)
  11. # filenew=nameList[0]+'.jpg'
  12. # print(filenew)
  13. #模型的名称
  14. newName=filename.replace('bert-large-uncased-whole-word-masking-finetuned-squad-','')
  15. os.rename(os.path.join(parent,filename),os.path.join(parent,newName))#重命名

处理完后就可以使用transformers库进行代码加载了。


模型使用

序列分类(以情感分类为例)

1.使用管道

  1. model_path="H:\\code\\Model\\bert-base-cased-finetuned-mrpc\\"
  2. from transformers import pipeline
  3. #使用当前模型+使用Tensorflow框架,默认应该是使用PYTORCH框架
  4. nlp = pipeline("sentiment-analysis",model=model_path, tokenizer=model_path, framework="tf")
  5. result = nlp("I hate you")[0]
  6. print(f"label: {result['label']}, with score: {round(result['score'], 4)}")
  7. result = nlp("I love you")[0]
  8. print(f"label: {result['label']}, with score: {round(result['score'], 4)}")

2.直接使用模型

  1. model_path="H:\\code\\Model\\bert-base-cased-finetuned-mrpc\\"
  2. #pytorch框架
  3. from transformers import AutoTokenizer, AutoModelForSequenceClassification
  4. import torch
  5. tokenizer = AutoTokenizer.from_pretrained(model_path)
  6. model = AutoModelForSequenceClassification.from_pretrained(model_path)
  7. classes = ["not paraphrase", "is paraphrase"]
  8. sequence_0 = "The company HuggingFace is based in New York City"
  9. sequence_1 = "Apples are especially bad for your health"
  10. sequence_2 = "HuggingFace's headquarters are situated in Manhattan"
  11. paraphrase = tokenizer(sequence_0, sequence_2, return_tensors="pt")
  12. not_paraphrase = tokenizer(sequence_0, sequence_1, return_tensors="pt")
  13. paraphrase_classification_logits = model(**paraphrase).logits
  14. not_paraphrase_classification_logits = model(**not_paraphrase).logits
  15. paraphrase_results = torch.softmax(paraphrase_classification_logits, dim=1).tolist()[0]
  16. not_paraphrase_results = torch.softmax(not_paraphrase_classification_logits, dim=1).tolist()[0]
  17. # Should be paraphrase
  18. for i in range(len(classes)):
  19. print(f"{classes[i]}: {int(round(paraphrase_results[i] * 100))}%")
  20. # Should not be paraphrase
  21. for i in range(len(classes)):
  22. print(f"{classes[i]}: {int(round(not_paraphrase_results[i] * 100))}%")
  23. #tensorflow框架
  24. from transformers import AutoTokenizer, TFAutoModelForSequenceClassification
  25. import tensorflow as tf
  26. tokenizer = AutoTokenizer.from_pretrained(model_path)
  27. model = TFAutoModelForSequenceClassification.from_pretrained(model_path)
  28. classes = ["not paraphrase", "is paraphrase"]
  29. sequence_0 = "The company HuggingFace is based in New York City"
  30. sequence_1 = "Apples are especially bad for your health"
  31. sequence_2 = "HuggingFace's headquarters are situated in Manhattan"
  32. paraphrase = tokenizer(sequence_0, sequence_2, return_tensors="tf")
  33. not_paraphrase = tokenizer(sequence_0, sequence_1, return_tensors="tf")
  34. paraphrase_classification_logits = model(paraphrase)[0]
  35. not_paraphrase_classification_logits = model(not_paraphrase)[0]
  36. paraphrase_results = tf.nn.softmax(paraphrase_classification_logits, axis=1).numpy()[0]
  37. not_paraphrase_results = tf.nn.softmax(not_paraphrase_classification_logits, axis=1).numpy()[0]
  38. # Should be paraphrase
  39. for i in range(len(classes)):
  40. print(f"{classes[i]}: {int(round(paraphrase_results[i] * 100))}%")
  41. # Should not be paraphrase
  42. for i in range(len(classes)):
  43. print(f"{classes[i]}: {int(round(not_paraphrase_results[i] * 100))}%")

提取式问答

1.使用管道

  1. model_path="H:\\code\\Model\\bert-large-uncased-whole-word-masking-finetuned-squad\\"
  2. from transformers import pipeline
  3. nlp = pipeline("question-answering",model=model_path, tokenizer=model_path)
  4. context = r"""
  5. Extractive Question Answering is the task of extracting an answer from a text given a question. An example of a
  6. question answering dataset is the SQuAD dataset, which is entirely based on that task. If you would like to fine-tune
  7. a model on a SQuAD task, you may leverage the examples/question-answering/run_squad.py script.
  8. """
  9. result = nlp(question="What is extractive question answering?", context=context)
  10. print(f"Answer: '{result['answer']}', score: {round(result['score'], 4)}, start: {result['start']}, end: {result['end']}")
  11. result = nlp(question="What is a good example of a question answering dataset?", context=context)
  12. print(f"Answer: '{result['answer']}', score: {round(result['score'], 4)}, start: {result['start']}, end: {result['end']}")

2.直接使用模型

  1. model_path="H:\\code\\Model\\bert-large-uncased-whole-word-masking-finetuned-squad\\"
  2. #使用pytorch框架
  3. from transformers import AutoTokenizer, AutoModelForQuestionAnswering
  4. import torch
  5. tokenizer = AutoTokenizer.from_pretrained(model_path)
  6. model = AutoModelForQuestionAnswering.from_pretrained(model_path)
  7. text = r"""
  8. 声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/129761
    推荐阅读
    相关标签