日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/3/21 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 预训练模型transformers综合总结(一) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

預訓練模型transformers綜合總結(一)

這是我對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等形式,為此在加載前,需要將把文件前面的模型名稱,才能加載成功

我自己寫的處理代碼如下:

  • #coding=utf-8

  • import os

  • import os.path

  • # 模型存放路徑

  • rootdir = r"H:\code\Model\bert-large-uncased-whole-word-masking-finetuned-squad"# 指明被遍歷的文件夾

  • ?
  • for parent,dirnames,filenames in os.walk(rootdir):#三個參數:分別返回1.父目錄 2.所有文件夾名字(不含路徑) 3.所有文件名字

  • for filename in filenames:#文件名

  • # nameList=filename.split('.')

  • # print(nameList)

  • print(filename)

  • # filenew=nameList[0]+'.jpg'

  • # print(filenew)

  • #模型的名稱

  • newName=filename.replace('bert-large-uncased-whole-word-masking-finetuned-squad-','')

  • os.rename(os.path.join(parent,filename),os.path.join(parent,newName))#重命名

  • 處理完后就可以使用transformers庫進行代碼加載了。


    模型使用

    序列分類(以情感分類為例)

    1.使用管道

  • model_path="H:\\code\\Model\\bert-base-cased-finetuned-mrpc\\"

  • ?
  • from transformers import pipeline

  • #使用當前模型+使用Tensorflow框架,默認應該是使用PYTORCH框架

  • nlp = pipeline("sentiment-analysis",model=model_path, tokenizer=model_path, framework="tf")

  • result = nlp("I hate you")[0]

  • print(f"label: {result['label']}, with score: {round(result['score'], 4)}")

  • result = nlp("I love you")[0]

  • print(f"label: {result['label']}, with score: {round(result['score'], 4)}")

  • 2.直接使用模型

  • model_path="H:\\code\\Model\\bert-base-cased-finetuned-mrpc\\"

  • #pytorch框架

  • ?
  • from transformers import AutoTokenizer, AutoModelForSequenceClassification

  • import torch

  • tokenizer = AutoTokenizer.from_pretrained(model_path)

  • model = AutoModelForSequenceClassification.from_pretrained(model_path)

  • classes = ["not paraphrase", "is paraphrase"]

  • sequence_0 = "The company HuggingFace is based in New York City"

  • sequence_1 = "Apples are especially bad for your health"

  • sequence_2 = "HuggingFace's headquarters are situated in Manhattan"

  • paraphrase = tokenizer(sequence_0, sequence_2, return_tensors="pt")

  • not_paraphrase = tokenizer(sequence_0, sequence_1, return_tensors="pt")

  • paraphrase_classification_logits = model(**paraphrase).logits

  • not_paraphrase_classification_logits = model(**not_paraphrase).logits

  • paraphrase_results = torch.softmax(paraphrase_classification_logits, dim=1).tolist()[0]

  • not_paraphrase_results = torch.softmax(not_paraphrase_classification_logits, dim=1).tolist()[0]

  • # Should be paraphrase

  • for i in range(len(classes)):

  • print(f"{classes[i]}: {int(round(paraphrase_results[i] * 100))}%")

  • # Should not be paraphrase

  • for i in range(len(classes)):

  • print(f"{classes[i]}: {int(round(not_paraphrase_results[i] * 100))}%")

  • ?
  • #tensorflow框架

  • from transformers import AutoTokenizer, TFAutoModelForSequenceClassification

  • import tensorflow as tf

  • tokenizer = AutoTokenizer.from_pretrained(model_path)

  • model = TFAutoModelForSequenceClassification.from_pretrained(model_path)

  • classes = ["not paraphrase", "is paraphrase"]

  • sequence_0 = "The company HuggingFace is based in New York City"

  • sequence_1 = "Apples are especially bad for your health"

  • sequence_2 = "HuggingFace's headquarters are situated in Manhattan"

  • paraphrase = tokenizer(sequence_0, sequence_2, return_tensors="tf")

  • not_paraphrase = tokenizer(sequence_0, sequence_1, return_tensors="tf")

  • paraphrase_classification_logits = model(paraphrase)[0]

  • not_paraphrase_classification_logits = model(not_paraphrase)[0]

  • paraphrase_results = tf.nn.softmax(paraphrase_classification_logits, axis=1).numpy()[0]

  • not_paraphrase_results = tf.nn.softmax(not_paraphrase_classification_logits, axis=1).numpy()[0]

  • # Should be paraphrase

  • for i in range(len(classes)):

  • print(f"{classes[i]}: {int(round(paraphrase_results[i] * 100))}%")

  • # Should not be paraphrase

  • for i in range(len(classes)):

  • print(f"{classes[i]}: {int(round(not_paraphrase_results[i] * 100))}%")

  • 提取式問答

    1.使用管道

  • model_path="H:\\code\\Model\\bert-large-uncased-whole-word-masking-finetuned-squad\\"

  • ?
  • from transformers import pipeline

  • nlp = pipeline("question-answering",model=model_path, tokenizer=model_path)

  • context = r"""

  • Extractive Question Answering is the task of extracting an answer from a text given a question. An example of a

  • question answering dataset is the SQuAD dataset, which is entirely based on that task. If you would like to fine-tune

  • a model on a SQuAD task, you may leverage the examples/question-answering/run_squad.py script.

  • """

  • result = nlp(question="What is extractive question answering?", context=context)

  • print(f"Answer: '{result['answer']}', score: {round(result['score'], 4)}, start: {result['start']}, end: {result['end']}")

  • result = nlp(question="What is a good example of a question answering dataset?", context=context)

  • print(f"Answer: '{result['answer']}', score: {round(result['score'], 4)}, start: {result['start']}, end: {result['end']}")

  • 2.直接使用模型

  • model_path="H:\\code\\Model\\bert-large-uncased-whole-word-masking-finetuned-squad\\"

  • #使用pytorch框架

  • from transformers import AutoTokenizer, AutoModelForQuestionAnswering

  • import torch

  • tokenizer = AutoTokenizer.from_pretrained(model_path)

  • model = AutoModelForQuestionAnswering.from_pretrained(model_path)

  • text = r"""

  • 🤗 Transformers (formerly known as pytorch-transformers and pytorch-pretrained-bert) provides general-purpose

  • architectures (BERT, GPT-2, RoBERTa, XLM, DistilBert, XLNet…) for Natural Language Understanding (NLU) and Natural

  • Language Generation (NLG) with over 32+ pretrained models in 100+ languages and deep interoperability between

  • TensorFlow 2.0 and PyTorch.

  • """

  • questions = [

  • "How many pretrained models are available in 🤗 Transformers?",

  • "What does 🤗 Transformers provide?",

  • "🤗 Transformers provides interoperability between which frameworks?",

  • ]

  • for question in questions:

  • inputs = tokenizer(question, text, add_special_tokens=True, return_tensors="pt")

  • input_ids = inputs["input_ids"].tolist()[0]

  • text_tokens = tokenizer.convert_ids_to_tokens(input_ids)

  • outputs = model(**inputs)

  • answer_start_scores = outputs.start_logits

  • answer_end_scores = outputs.end_logits

  • answer_start = torch.argmax(

  • answer_start_scores

  • ) # Get the most likely beginning of answer with the argmax of the score

  • answer_end = torch.argmax(answer_end_scores) + 1 # Get the most likely end of answer with the argmax of the score

  • answer = tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(input_ids[answer_start:answer_end]))

  • print(f"Question: {question}")

  • print(f"Answer: {answer}")

  • ?
  • #使用tensorflow框架

  • from transformers import AutoTokenizer, TFAutoModelForQuestionAnswering

  • import tensorflow as tf

  • tokenizer = AutoTokenizer.from_pretrained(model_path)

  • model = TFAutoModelForQuestionAnswering.from_pretrained(model_path)

  • text = r"""

  • 🤗 Transformers (formerly known as pytorch-transformers and pytorch-pretrained-bert) provides general-purpose

  • architectures (BERT, GPT-2, RoBERTa, XLM, DistilBert, XLNet…) for Natural Language Understanding (NLU) and Natural

  • Language Generation (NLG) with over 32+ pretrained models in 100+ languages and deep interoperability between

  • TensorFlow 2.0 and PyTorch.

  • """

  • questions = [

  • "How many pretrained models are available in 🤗 Transformers?",

  • "What does 🤗 Transformers provide?",

  • "🤗 Transformers provides interoperability between which frameworks?",

  • ]

  • for question in questions:

  • inputs = tokenizer(question, text, add_special_tokens=True, return_tensors="tf")

  • input_ids = inputs["input_ids"].numpy()[0]

  • text_tokens = tokenizer.convert_ids_to_tokens(input_ids)

  • outputs = model(inputs)

  • answer_start_scores = outputs.start_logits

  • answer_end_scores = outputs.end_logits

  • answer_start = tf.argmax(

  • answer_start_scores, axis=1

  • ).numpy()[0] # Get the most likely beginning of answer with the argmax of the score

  • answer_end = (

  • tf.argmax(answer_end_scores, axis=1) + 1

  • ).numpy()[0] # Get the most likely end of answer with the argmax of the score

  • answer = tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(input_ids[answer_start:answer_end]))

  • print(f"Question: {question}")

  • print(f"Answer: {answer}")

  • 語言建模

    1.使用管道

  • model_path="H:\\code\\Model\\distilbert-base-cased\\"

  • from transformers import pipeline

  • nlp = pipeline("fill-mask",model=model_path, tokenizer=model_path, framework="tf")

  • from pprint import pprint

  • pprint(nlp(f"HuggingFace is creating a {nlp.tokenizer.mask_token} that the community uses to solve NLP tasks."))

  • 2.直接使用模型

  • model_path="H:\\code\\Model\\distilbert-base-cased\\"

  • ?
  • ## 使用pytorch實現

  • from transformers import AutoModelWithLMHead, AutoTokenizer

  • import torch

  • tokenizer = AutoTokenizer.from_pretrained(model_path)

  • model = AutoModelWithLMHead.from_pretrained(model_path)

  • sequence = f"Distilled models are smaller than the models they mimic. Using them instead of the large versions would help {tokenizer.mask_token} our carbon footprint."

  • input = tokenizer.encode(sequence, return_tensors="pt")

  • mask_token_index = torch.where(input == tokenizer.mask_token_id)[1]

  • token_logits = model(input).logits

  • mask_token_logits = token_logits[0, mask_token_index, :]

  • top_5_tokens = torch.topk(mask_token_logits, 5, dim=1).indices[0].tolist()

  • for token in top_5_tokens:

  • print(sequence.replace(tokenizer.mask_token, tokenizer.decode([token])))

  • ?
  • ## 使用tensorflow實現

  • from transformers import TFAutoModelWithLMHead, AutoTokenizer

  • import tensorflow as tf

  • tokenizer = AutoTokenizer.from_pretrained(model_path)

  • model = TFAutoModelWithLMHead.from_pretrained(model_path)

  • sequence = f"Distilled models are smaller than the models they mimic. Using them instead of the large versions would help {tokenizer.mask_token} our carbon footprint."

  • input = tokenizer.encode(sequence, return_tensors="tf")

  • mask_token_index = tf.where(input == tokenizer.mask_token_id)[0, 1]

  • token_logits = model(input)[0]

  • mask_token_logits = token_logits[0, mask_token_index, :]

  • top_5_tokens = tf.math.top_k(mask_token_logits, 5).indices.numpy()

  • for token in top_5_tokens:

  • print(sequence.replace(tokenizer.mask_token, tokenizer.decode([token])))

  • 文字生成

    1.使用管道

  • model_path="H:\\code\\Model\\xlnet-base-cased\\"

  • from transformers import pipeline

  • text_generator = pipeline("text-generation",model=model_path, tokenizer=model_path, framework="tf")

  • print(text_generator("As far as I am concerned, I will", max_length=50, do_sample=False))

  • 2.直接使用模型

  • model_path="H:\\code\\Model\\xlnet-base-cased\\"

  • #使用pytorch版本

  • from transformers import AutoModelWithLMHead, AutoTokenizer

  • model = AutoModelWithLMHead.from_pretrained(model_path)

  • tokenizer = AutoTokenizer.from_pretrained(model_path)

  • # Padding text helps XLNet with short prompts - proposed by Aman Rusia in https://github.com/rusiaaman/XLNet-gen#methodology

  • PADDING_TEXT = """In 1991, the remains of Russian Tsar Nicholas II and his family

  • (except for Alexei and Maria) are discovered.

  • The voice of Nicholas's young son, Tsarevich Alexei Nikolaevich, narrates the

  • remainder of the story. 1883 Western Siberia,

  • a young Grigori Rasputin is asked by his father and a group of men to perform magic.

  • Rasputin has a vision and denounces one of the men as a horse thief. Although his

  • father initially slaps him for making such an accusation, Rasputin watches as the

  • man is chased outside and beaten. Twenty years later, Rasputin sees a vision of

  • the Virgin Mary, prompting him to become a priest. Rasputin quickly becomes famous,

  • with people, even a bishop, begging for his blessing. <eod> </s> <eos>"""

  • prompt = "Today the weather is really nice and I am planning on "

  • inputs = tokenizer.encode(PADDING_TEXT + prompt, add_special_tokens=False, return_tensors="pt")

  • prompt_length = len(tokenizer.decode(inputs[0], skip_special_tokens=True, clean_up_tokenization_spaces=True))

  • outputs = model.generate(inputs, max_length=250, do_sample=True, top_p=0.95, top_k=60)

  • generated = prompt + tokenizer.decode(outputs[0])[prompt_length:]

  • print(generated)

  • ?
  • #使用tensorflow版本

  • from transformers import TFAutoModelWithLMHead, AutoTokenizer

  • model = TFAutoModelWithLMHead.from_pretrained(model_path)

  • tokenizer = AutoTokenizer.from_pretrained(model_path)

  • # Padding text helps XLNet with short prompts - proposed by Aman Rusia in https://github.com/rusiaaman/XLNet-gen#methodology

  • PADDING_TEXT = """In 1991, the remains of Russian Tsar Nicholas II and his family

  • (except for Alexei and Maria) are discovered.

  • The voice of Nicholas's young son, Tsarevich Alexei Nikolaevich, narrates the

  • remainder of the story. 1883 Western Siberia,

  • a young Grigori Rasputin is asked by his father and a group of men to perform magic.

  • Rasputin has a vision and denounces one of the men as a horse thief. Although his

  • father initially slaps him for making such an accusation, Rasputin watches as the

  • man is chased outside and beaten. Twenty years later, Rasputin sees a vision of

  • the Virgin Mary, prompting him to become a priest. Rasputin quickly becomes famous,

  • with people, even a bishop, begging for his blessing. <eod> </s> <eos>"""

  • prompt = "Today the weather is really nice and I am planning on "

  • inputs = tokenizer.encode(PADDING_TEXT + prompt, add_special_tokens=False, return_tensors="tf")

  • prompt_length = len(tokenizer.decode(inputs[0], skip_special_tokens=True, clean_up_tokenization_spaces=True))

  • outputs = model.generate(inputs, max_length=250, do_sample=True, top_p=0.95, top_k=60)

  • generated = prompt + tokenizer.decode(outputs[0])[prompt_length:]

  • print(generated)

  • ?
  • 命名實體識別

    1.使用管道

  • model_path="H:\\code\\Model\\dbmdz\\bert-large-cased-finetuned-conll03-english\\"

  • tokenizer="H:\\code\\Model\\bert-base-cased\\"

  • from transformers import pipeline

  • nlp = pipeline("ner",model=model_path, tokenizer=tokenizer_path)

  • sequence = "Hugging Face Inc. is a company based in New York City. Its headquarters are in DUMBO, therefore very"

  • "close to the Manhattan Bridge which is visible from the window."

  • print(nlp(sequence))

  • 2.直接使用模型

  • model_path="H:\\code\\Model\\dbmdz\\bert-large-cased-finetuned-conll03-english\\"

  • tokenizer_path="H:\\code\\Model\\bert-base-cased\\"

  • ?
  • ##使用pytorch格式

  • ?
  • from transformers import AutoModelForTokenClassification, AutoTokenizer

  • import torch

  • model = AutoModelForTokenClassification.from_pretrained(model_path)

  • tokenizer = AutoTokenizer.from_pretrained(tokenizer_path)

  • label_list = [

  • "O", # Outside of a named entity

  • "B-MISC", # Beginning of a miscellaneous entity right after another miscellaneous entity

  • "I-MISC", # Miscellaneous entity

  • "B-PER", # Beginning of a person's name right after another person's name

  • "I-PER", # Person's name

  • "B-ORG", # Beginning of an organisation right after another organisation

  • "I-ORG", # Organisation

  • "B-LOC", # Beginning of a location right after another location

  • "I-LOC" # Location

  • ]

  • sequence = "Hugging Face Inc. is a company based in New York City. Its headquarters are in DUMBO, therefore very" \

  • "close to the Manhattan Bridge."

  • # Bit of a hack to get the tokens with the special tokens

  • tokens = tokenizer.tokenize(tokenizer.decode(tokenizer.encode(sequence)))

  • inputs = tokenizer.encode(sequence, return_tensors="pt")

  • outputs = model(inputs).logits

  • predictions = torch.argmax(outputs, dim=2)

  • print([(token, label_list[prediction]) for token, prediction in zip(tokens, predictions[0].numpy())])

  • ?
  • ##使用tensorflow格式

  • from transformers import TFAutoModelForTokenClassification, AutoTokenizer

  • import tensorflow as tf

  • model = TFAutoModelForTokenClassification.from_pretrained(model_path)

  • tokenizer = AutoTokenizer.from_pretrained(tokenizer_path)

  • label_list = [

  • "O", # Outside of a named entity

  • "B-MISC", # Beginning of a miscellaneous entity right after another miscellaneous entity

  • "I-MISC", # Miscellaneous entity

  • "B-PER", # Beginning of a person's name right after another person's name

  • "I-PER", # Person's name

  • "B-ORG", # Beginning of an organisation right after another organisation

  • "I-ORG", # Organisation

  • "B-LOC", # Beginning of a location right after another location

  • "I-LOC" # Location

  • ]

  • sequence = "Hugging Face Inc. is a company based in New York City. Its headquarters are in DUMBO, therefore very" \

  • "close to the Manhattan Bridge."

  • # Bit of a hack to get the tokens with the special tokens

  • tokens = tokenizer.tokenize(tokenizer.decode(tokenizer.encode(sequence)))

  • inputs = tokenizer.encode(sequence, return_tensors="tf")

  • outputs = model(inputs)[0]

  • predictions = tf.argmax(outputs, axis=2)

  • print([(token, label_list[prediction]) for token, prediction in zip(tokens, predictions[0].numpy())])

  • 文本摘要

    1.使用管道

  • model_path="H:\\code\\Model\\t5-base\\"

  • ?
  • from transformers import pipeline

  • summarizer = pipeline("summarization",model=model_path, tokenizer=model_path)

  • ARTICLE = """ New York (CNN)When Liana Barrientos was 23 years old, she got married in Westchester County, New York.

  • A year later, she got married again in Westchester County, but to a different man and without divorcing her first husband.

  • Only 18 days after that marriage, she got hitched yet again. Then, Barrientos declared "I do" five more times, sometimes only within two weeks of each other.

  • In 2010, she married once more, this time in the Bronx. In an application for a marriage license, she stated it was her "first and only" marriage.

  • Barrientos, now 39, is facing two criminal counts of "offering a false instrument for filing in the first degree," referring to her false statements on the

  • 2010 marriage license application, according to court documents.

  • Prosecutors said the marriages were part of an immigration scam.

  • On Friday, she pleaded not guilty at State Supreme Court in the Bronx, according to her attorney, Christopher Wright, who declined to comment further.

  • After leaving court, Barrientos was arrested and charged with theft of service and criminal trespass for allegedly sneaking into the New York subway through an emergency exit, said Detective

  • Annette Markowski, a police spokeswoman. In total, Barrientos has been married 10 times, with nine of her marriages occurring between 1999 and 2002.

  • All occurred either in Westchester County, Long Island, New Jersey or the Bronx. She is believed to still be married to four men, and at one time, she was married to eight men at once, prosecutors say.

  • Prosecutors said the immigration scam involved some of her husbands, who filed for permanent residence status shortly after the marriages.

  • Any divorces happened only after such filings were approved. It was unclear whether any of the men will be prosecuted.

  • The case was referred to the Bronx District Attorney\'s Office by Immigration and Customs Enforcement and the Department of Homeland Security\'s

  • Investigation Division. Seven of the men are from so-called "red-flagged" countries, including Egypt, Turkey, Georgia, Pakistan and Mali.

  • Her eighth husband, Rashid Rajput, was deported in 2006 to his native Pakistan after an investigation by the Joint Terrorism Task Force.

  • If convicted, Barrientos faces up to four years in prison. Her next court appearance is scheduled for May 18.

  • """

  • print(summarizer(ARTICLE, max_length=130, min_length=30, do_sample=False))

  • 2.直接使用模型

  • model_path="H:\\code\\Model\\t5-base\\"

  • ?
  • #使用pytorch框架

  • from transformers import AutoModelWithLMHead, AutoTokenizer

  • model = AutoModelWithLMHead.from_pretrained(model_path)

  • tokenizer = AutoTokenizer.from_pretrained(model_path)

  • # T5 uses a max_length of 512 so we cut the article to 512 tokens.

  • inputs = tokenizer.encode("summarize: " + ARTICLE, return_tensors="pt", max_length=512)

  • outputs = model.generate(inputs, max_length=150, min_length=40, length_penalty=2.0, num_beams=4, early_stopping=True)

  • # print(outputs)

  • print(tokenizer.decode(outputs[0]))

  • ?
  • #使用tensorflow框架

  • from transformers import TFAutoModelWithLMHead, AutoTokenizer

  • model = TFAutoModelWithLMHead.from_pretrained(model_path)

  • tokenizer = AutoTokenizer.from_pretrained(model_path)

  • # T5 uses a max_length of 512 so we cut the article to 512 tokens.

  • inputs = tokenizer.encode("summarize: " + ARTICLE, return_tensors="tf", max_length=512)

  • outputs = model.generate(inputs, max_length=150, min_length=40, length_penalty=2.0, num_beams=4, early_stopping=True)

  • 翻譯

    1.使用管道

  • model_path="H:\\code\\Model\\t5-base\\"

  • from transformers import pipeline

  • translator = pipeline("translation_en_to_de",model=model_path, tokenizer=model_path)

  • print(translator("Hugging Face is a technology company based in New York and Paris", max_length=40))

  • 2.直接使用模型

    模型定制

    初始化調整

  • model_path="H:\\code\\Model\\distilbert-base-uncased\\"

  • #pytorch方式

  • ?
  • from transformers import DistilBertConfig, DistilBertTokenizer, DistilBertForSequenceClassification

  • config = DistilBertConfig(n_heads=8, dim=512, hidden_dim=4*512)

  • tokenizer = DistilBertTokenizer.from_pretrained(model_path)

  • model = DistilBertForSequenceClassification(config)

  • ?
  • #tensorflow方式

  • ?
  • from transformers import DistilBertConfig, DistilBertTokenizer, TFDistilBertForSequenceClassification

  • config = DistilBertConfig(n_heads=8, dim=512, hidden_dim=4*512)

  • tokenizer = DistilBertTokenizer.from_pretrained(model_path)

  • model = TFDistilBertForSequenceClassification(config)

  • ?
  • 部分調整

  • model_name="H:\\code\\Model\\distilbert-base-uncased\\"

  • ?
  • #pytorch方式

  • ?
  • from transformers import DistilBertConfig, DistilBertTokenizer, DistilBertForSequenceClassification

  • #model_name = "distilbert-base-uncased"

  • model = DistilBertForSequenceClassification.from_pretrained(model_name, num_labels=10)

  • tokenizer = DistilBertTokenizer.from_pretrained(model_name)

  • ?
  • #tensorflow方式

  • from transformers import DistilBertConfig, DistilBertTokenizer, TFDistilBertForSequenceClassification

  • #model_name = "distilbert-base-uncased"

  • model = TFDistilBertForSequenceClassification.from_pretrained(model_name, num_labels=10)

  • tokenizer = DistilBertTokenizer.from_pretrained(model_name)

  • 模型保存與加載

  • #模型保存

  • ##對模型進行微調后,可以通過以下方式將其與標記器一起保存:

  • save_directory="./save/"

  • tokenizer.save_pretrained(save_directory)

  • model.save_pretrained(save_directory)

  • ?
  • #模型加載

  • ##TensorFlow模型中加載已保存的PyTorch模型

  • tokenizer = AutoTokenizer.from_pretrained(save_directory)

  • model = TFAutoModel.from_pretrained(save_directory, from_pt=True)

  • ?
  • ##PyTorch模型中加載已保存的TensorFlow模型

  • tokenizer = AutoTokenizer.from_pretrained(save_directory)

  • model = AutoModel.from_pretrained(save_directory, from_tf=True)

  • ?
  • ?

    總結

    以上是生活随笔為你收集整理的预训练模型transformers综合总结(一)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。