当前位置:   article > 正文

【无标题】_transformers可以加载ernie模型吗

transformers可以加载ernie模型吗

【ERNIE】分类15类大多数是中文的文本

  1. from transformers import AutoTokenizer, AutoModelForSequenceClassification
  2. # 加载 ERNIE tokenizer
  3. tokenizer = AutoTokenizer.from_pretrained("nghuyong/ernie-1.0")
  4. # 加载 ERNIE 预训练模型,用于分类任务
  5. model = AutoModelForSequenceClassification.from_pretrained("nghuyong/ernie-1.0", num_labels=15)
  6. # 对输入文本进行编码
  7. text = "这是一段待分类的中文文本。"
  8. encoded_input = tokenizer(text, truncation=True, padding=True, return_tensors='pt')
  9. # 获取模型输出
  10. output = model(**encoded_input)

这段代码使用 AutoTokenizer 类从 "nghuyong/ernie-1.0" checkpoint 加载 tokenizer,并使用 AutoModelForSequenceClassification 类从相同的 checkpoint 加载预训练模型。在这个例子中,该模型用于中文文本分类任务,因此 num_labels 参数被设置为 15,这是数据集中的类别数目。

# 结果:ERNIE分类15类大多数是中文的文本——max=64

  1. ------------Epoch: 0 ----------------
  2. epoch: 0, iter_num: 100, loss: 1.7696, 15.38%
  3. epoch: 0, iter_num: 200, loss: 0.6329, 30.77%
  4. epoch: 0, iter_num: 300, loss: 0.5543, 46.15%
  5. epoch: 0, iter_num: 400, loss: 1.2803, 61.54%
  6. epoch: 0, iter_num: 500, loss: 0.4909, 76.92%
  7. epoch: 0, iter_num: 600, loss: 0.7790, 92.31%
  8. Epoch: 0, Average training loss: 0.9317
  9. Accuracy: 0.8409
  10. Average testing loss: 0.5869
  11. -------------------------------
  12. ------------Epoch: 1 ----------------
  13. epoch: 1, iter_num: 100, loss: 0.4217, 15.38%
  14. epoch: 1, iter_num: 200, loss: 0.1333, 30.77%
  15. epoch: 1, iter_num: 300, loss: 0.5391, 46.15%
  16. epoch: 1, iter_num: 400, loss: 0.4612, 61.54%
  17. epoch: 1, iter_num: 500, loss: 0.3900, 76.92%
  18. epoch: 1, iter_num: 600, loss: 0.4132, 92.31%
  19. Epoch: 1, Average training loss: 0.4501
  20. Accuracy: 0.8466
  21. Average testing loss: 0.5791
  22. -------------------------------
  23. ------------Epoch: 2 ----------------
  24. epoch: 2, iter_num: 100, loss: 0.6343, 15.38%
  25. epoch: 2, iter_num: 200, loss: 0.4136, 30.77%
  26. epoch: 2, iter_num: 300, loss: 0.2595, 46.15%
  27. epoch: 2, iter_num: 400, loss: 0.4415, 61.54%
  28. epoch: 2, iter_num: 500, loss: 0.1030, 76.92%
  29. epoch: 2, iter_num: 600, loss: 0.0143, 92.31%
  30. Epoch: 2, Average training loss: 0.3240
  31. Accuracy: 0.8443
  32. Average testing loss: 0.6346
  33. -------------------------------
  34. ------------Epoch: 3 ----------------
  35. epoch: 3, iter_num: 100, loss: 0.3799, 15.38%
  36. epoch: 3, iter_num: 200, loss: 0.6112, 30.77%
  37. epoch: 3, iter_num: 300, loss: 0.1612, 46.15%
  38. epoch: 3, iter_num: 400, loss: 0.1312, 61.54%
  39. epoch: 3, iter_num: 500, loss: 0.4110, 76.92%
  40. epoch: 3, iter_num: 600, loss: 0.0074, 92.31%
  41. Epoch: 3, Average training loss: 0.2366
  42. Accuracy: 0.8501
  43. Average testing loss: 0.7379
  44. -------------------------------
  45. ------------Epoch: 4 ----------------
  46. epoch: 4, iter_num: 100, loss: 0.1996, 15.38%
  47. epoch: 4, iter_num: 200, loss: 0.3158, 30.77%
  48. epoch: 4, iter_num: 300, loss: 0.0271, 46.15%
  49. epoch: 4, iter_num: 400, loss: 0.0166, 61.54%
  50. epoch: 4, iter_num: 500, loss: 0.2053, 76.92%
  51. epoch: 4, iter_num: 600, loss: 0.4321, 92.31%
  52. Epoch: 4, Average training loss: 0.1704
  53. Accuracy: 0.8459
  54. Average testing loss: 0.7492
  55. -------------------------------

分类15类的中文预训练模型和tokenizer

  1. from transformers import AutoTokenizer, AutoModelForSequenceClassification
  2. # 加载分类15类 tokenizer
  3. tokenizer = AutoTokenizer.from_pretrained("bert-base-chinese")
  4. # 加载分类15类预训练模型,用于分类任务
  5. model = AutoModelForSequenceClassification.from_pretrained("ckiplab/bert-base-chinese-15cls", num_labels=15)

 这段代码使用 AutoTokenizer 类从 "bert-base-chinese" checkpoint 加载 tokenizer,并使用 AutoModelForSequenceClassification 类从 "ckiplab/bert-base-chinese-15cls" checkpoint 加载分类15类预训练模型。在这个例子中,该模型用于中文文本分类任务,因此 num_labels 参数被设置为 15,这是数据集中的类别数目。

【ALBERT】分类15类文本

  1. 准备好文本分类的数据集,将其分成训练集、验证集和测试集,并进行标注。

  2. 下载ALBERT的预训练模型权重,通常来自于Hugging Face提供的Transformers库,或Google Research发布的官方源码。

  3. 使用相应的深度学习框架,例如PyTorch或TensorFlow,加载ALBERT模型,并根据需要对模型进行微调或者冻结预训练模型的权重进行fine-tuning。

  4. 对训练集进行训练,使用验证集进行模型调优,最终在测试集上进行模型评估。

以下是一个ALBERT+PyTorch的分类模型示例代码,用于15类文本分类

  1. import torch
  2. from transformers import AlbertTokenizer, AlbertForSequenceClassification
  3. # 加载预训练模型和tokenizer
  4. tokenizer = AlbertTokenizer.from_pretrained('albert-base-v2')
  5. model = AlbertForSequenceClassification.from_pretrained('albert-base-v2', num_labels=15)
  6. # 准备数据集
  7. train_texts = [...] # 训练集文本
  8. train_labels = [...] # 训练集标签
  9. val_texts = [...] # 验证集文本
  10. val_labels = [...] # 验证集标签
  11. test_texts = [...] # 测试集文本
  12. test_labels = [...] # 测试集标签
  13. def encode_text(texts):
  14. return tokenizer.batch_encode_plus(texts, padding=True, truncation=True, return_tensors='pt')
  15. train_encodings = encode_text(train_texts)
  16. val_encodings = encode_text(val_texts)
  17. test_encodings = encode_text(test_texts)
  18. # 将训练集和验证集的标签作为Tensor传入
  19. train_labels = torch.tensor(train_labels)
  20. val_labels = torch.tensor(val_labels)
  21. # 将预处理好的数据输入模型中进行微调
  22. optimizer = torch.optim.AdamW(model.parameters(), lr=1e-5)
  23. train_dataset = torch.utils.data.TensorDataset(train_encodings['input_ids'], train_encodings['attention_mask'], train_labels)
  24. val_dataset = torch.utils.data.TensorDataset(val_encodings['input_ids'], val_encodings['attention_mask'], val_labels)
  25. # 定义训练函数
  26. def train_epoch(model, dataloader, optimizer):
  27. model.train()
  28. for batch in dataloader:
  29. optimizer.zero_grad()
  30. input_ids, attention_mask, labels = batch
  31. outputs = model(input_ids, attention_mask=attention_mask, labels=labels)
  32. loss = outputs[0]
  33. loss.backward()
  34. optimizer.step()
  35. return loss.item()
  36. # 开始训练
  37. train_dataloader = torch.utils.data.DataLoader(train_dataset, batch_size=16, shuffle=True)
  38. val_dataloader = torch.utils.data.DataLoader(val_dataset, batch_size=16, shuffle=False)
  39. for epoch in range(3):
  40. train_loss = train_epoch(model, train_dataloader, optimizer)
  41. print(f'training loss: {train_loss}')
  42. # 在验证集上评估模型
  43. model.eval()
  44. with torch.no_grad():
  45. val_preds = []
  46. for batch in val_dataloader:
  47. input_ids, attention_mask, labels = batch
  48. outputs = model(input_ids, attention_mask=attention_mask)
  49. val_preds += outputs[0].argmax(axis=-1).tolist()
  50. acc = sum([1 if p == l else 0 for p, l in zip(val_preds, val_labels)]) / len(val_labels)
  51. print(f'validation accuracy: {acc}')
  52. # 在测试集上进行测试
  53. test_dataset = torch.utils.data.TensorDataset(test_encodings['input_ids'], test_encodings['attention_mask'])
  54. test_dataloader = torch.utils.data.DataLoader(test_dataset, batch_size=16, shuffle=False)
  55. model.eval()
  56. with torch.no_grad():
  57. test_preds = []
  58. for batch in test_dataloader:
  59. input_ids, attention_mask = batch
  60. outputs = model(input_ids, attention_mask=attention_mask)
  61. test_preds += outputs[0].argmax(axis=-1).tolist()
  62. acc = sum([1 if p == l else 0 for p, l in zip(test_preds, test_labels)]) / len(test_labels)
  63. print(f'test accuracy: {acc}')

其中,ALBERT的预训练模型可以通过AlbertTokenizer.from_pretrained()AlbertForSequenceClassification.from_pretrained()方法直接加载,并在调用forward()方法时传递文本的input_ids和attention_mask。在实际应用中,常常需要根据具体情况进行微调,例如调整batch_size、学习率、优化器等参数,或者在模型中添加额外的层和损失函数,以获取更好的效果。

ALBERT分类15类中文文本的预训练模型和tokenizer

  1. 首先,确保您已经安装了huggingface transformers这个Python库。

  2. 接下来,从Hugging Face的模型存储库中获取ALBERT预训练模型。例如,您可以选择ALBERT-base或ALBERT-large模型。您可以使用下面的代码之一:

  1. from transformers import AlbertTokenizer, AlbertForSequenceClassification
  2. # 加载预训练ALBERT-base模型
  3. tokenizer = AlbertTokenizer.from_pretrained('voidful/albert_chinese_base')
  4. model = AlbertForSequenceClassification.from_pretrained('voidful/albert_chinese_base', num_labels=15)
  5. # 加载预训练ALBERT-large模型
  6. tokenizer = AlbertTokenizer.from_pretrained('voidful/albert_chinese_large')
  7. model = AlbertForSequenceClassification.from_pretrained('voidful/albert_chinese_large', num_labels=15)

【RoBERTa】预训练模型和tokenizer

  1. from transformers import RobertaTokenizer, RobertaForSequenceClassification
  2. # 加载RoBERTa tokenizer
  3. tokenizer = RobertaTokenizer.from_pretrained('hfl/chinese-roberta-wwm-ext')
  4. # 加载RoBERTa分类模型
  5. model = RobertaForSequenceClassification.from_pretrained('hfl/chinese-roberta-wwm-ext', num_labels=15)
  6. #————————————————————————————————————————————————————————————————————————————————
  7. from transformers import RobertaTokenizerFast, RobertaForSequenceClassification
  8. # Load the RoBERTa tokenizer
  9. tokenizer = RobertaTokenizerFast.from_pretrained('hfl/chinese-roberta-wwm-ext')
  10. # Load the pre-trained RoBERTa model for sequence classification
  11. model = RobertaForSequenceClassification.from_pretrained('hfl/chinese-roberta-wwm-ext', num_labels=15)

这个代码示例使用’Hugging Face’ Transformers库中的RobertaTokenizerRobertaForSequenceClassification类来加载预训练的RoBERTa模型和tokenizer。from_pretrained方法用于从预训练模型加载tokenizer和模型参数。

请注意, RobertaForSequenceClassification需要指定num_labels参数,以指定文本分类的数量。在这个例子中,共有15个分类。您可以根据您的分类任务修改此数字。

同时,基于该预训练模型训练的分类任务使用了中文文本,因此所加载的预训练模型也应为中文预训练模型。在这个例子中,使用的是’chinese-roberta-wwm-ext’模型。如果需要使用其他预训练模型,可以在’Hugging Face’ transformers文档中查找可用的模型列表。

RoBERTa(中文版):RoBERTa 是由 Facebook AI 团队提出的 BERT 变种,其训练过程使用了更多的数据和更长的训练时间。RoBERTa 的中文版也已经发布,并取得了比同等条件下 BERT 更好的效果。你可以通过 Hugging Face 的 Transformers 库来使用 RoBERTa。

【XLNet】分类15类的中文给出如何加载预训练模型和tokenizer

  1. # 导入必要的库
  2. import torch
  3. from transformers import XLNetTokenizer, XLNetForSequenceClassification
  4. # 定义分类类别数
  5. num_labels = 15
  6. # 加载预训练模型和tokenizer
  7. model_name = 'hfl/chinese-xlnet-base'
  8. tokenizer = XLNetTokenizer.from_pretrained(model_name)
  9. model = XLNetForSequenceClassification.from_pretrained(model_name, num_labels=num_labels)

在上面的代码中,我们首先定义了分类类别数 num_labels,然后通过 XLNetForSequenceClassification 类来加载预训练模型,设置 num_labels 参数为分类类别数。

接着使用 XLNetTokenizer 类来加载对应的tokenizer,然后对样本进行编码,生成输入张量,最后将输入张量传入XLNetForSequenceClassification 模型中获取分类结果。

需要注意的是,本例中我们使用了padding和截断技术来规范输入的文本长度,使之适配模型的输入要求,这里我们将长度限制为64。实际上,长度的限制需要根据具体的应用场景和任务来进行调整。

XLNet:XLNet 是 CMU 和 Google 提出的第一个非自回归的预训练模型。它采用 Transformer-XL 的结构来解决 BERT 中存在的顺序建模的限制,从而更好地捕捉文本中的上下文信息,对中文文本分类也大有帮助。

结果:

  1. # XLNet
  2. # ——max=64
  3. # Epoch: 4, Average training loss: 0.1858
  4. # Accuracy: 0.8409
  5. ------------Epoch: 0 ----------------
  6. epoch: 0, iter_num: 100, loss: 1.0723, 15.38%
  7. epoch: 0, iter_num: 200, loss: 0.9870, 30.77%
  8. epoch: 0, iter_num: 300, loss: 0.2781, 46.15%
  9. epoch: 0, iter_num: 400, loss: 0.5312, 61.54%
  10. epoch: 0, iter_num: 500, loss: 0.2781, 76.92%
  11. epoch: 0, iter_num: 600, loss: 0.5329, 92.31%
  12. Epoch: 0, Average training loss: 0.9603
  13. Accuracy: 0.8183
  14. Average testing loss: 0.6233
  15. -------------------------------
  16. ------------Epoch: 1 ----------------
  17. epoch: 1, iter_num: 100, loss: 0.9092, 15.38%
  18. epoch: 1, iter_num: 200, loss: 0.4809, 30.77%
  19. epoch: 1, iter_num: 300, loss: 0.2143, 46.15%
  20. epoch: 1, iter_num: 400, loss: 0.7193, 61.54%
  21. epoch: 1, iter_num: 500, loss: 0.2858, 76.92%
  22. epoch: 1, iter_num: 600, loss: 0.3786, 92.31%
  23. Epoch: 1, Average training loss: 0.5122
  24. Accuracy: 0.8290
  25. Average testing loss: 0.6355
  26. -------------------------------
  27. ------------Epoch: 2 ----------------
  28. epoch: 2, iter_num: 100, loss: 0.2993, 15.38%
  29. epoch: 2, iter_num: 200, loss: 0.4890, 30.77%
  30. epoch: 2, iter_num: 300, loss: 0.2096, 46.15%
  31. epoch: 2, iter_num: 400, loss: 0.2015, 61.54%
  32. epoch: 2, iter_num: 500, loss: 0.4490, 76.92%
  33. epoch: 2, iter_num: 600, loss: 0.2591, 92.31%
  34. Epoch: 2, Average training loss: 0.3701
  35. Accuracy: 0.8336
  36. Average testing loss: 0.6460
  37. -------------------------------
  38. ------------Epoch: 3 ----------------
  39. epoch: 3, iter_num: 100, loss: 0.0596, 15.38%
  40. epoch: 3, iter_num: 200, loss: 0.2204, 30.77%
  41. epoch: 3, iter_num: 300, loss: 0.0602, 46.15%
  42. epoch: 3, iter_num: 400, loss: 0.2729, 61.54%
  43. epoch: 3, iter_num: 500, loss: 0.2173, 76.92%
  44. epoch: 3, iter_num: 600, loss: 0.6965, 92.31%
  45. Epoch: 3, Average training loss: 0.2579
  46. Accuracy: 0.8401
  47. Average testing loss: 0.7422
  48. -------------------------------
  49. ------------Epoch: 4 ----------------
  50. epoch: 4, iter_num: 100, loss: 0.4096, 15.38%
  51. epoch: 4, iter_num: 200, loss: 0.4123, 30.77%
  52. epoch: 4, iter_num: 300, loss: 0.3764, 46.15%
  53. epoch: 4, iter_num: 400, loss: 0.7553, 61.54%
  54. epoch: 4, iter_num: 500, loss: 0.1620, 76.92%
  55. epoch: 4, iter_num: 600, loss: 0.9384, 92.31%
  56. Epoch: 4, Average training loss: 0.1858
  57. Accuracy: 0.8409
  58. Average testing loss: 0.8066
  59. -------------------------------
  1. # max_length=64
  2. ------------Epoch: 0 ----------------
  3. epoch: 0, iter_num: 100, loss: 1.5357, 15.38%
  4. epoch: 0, iter_num: 200, loss: 0.9866, 30.77%
  5. epoch: 0, iter_num: 300, loss: 1.2628, 46.15%
  6. epoch: 0, iter_num: 400, loss: 0.6299, 61.54%
  7. epoch: 0, iter_num: 500, loss: 1.0743, 76.92%
  8. epoch: 0, iter_num: 600, loss: 0.4764, 92.31%
  9. Epoch: 0, Average training loss: 0.9521
  10. Accuracy: 0.8213
  11. Average testing loss: 0.6333
  12. -------------------------------
  13. ------------Epoch: 1 ----------------
  14. epoch: 1, iter_num: 100, loss: 0.2546, 15.38%
  15. epoch: 1, iter_num: 200, loss: 1.0636, 30.77%
  16. epoch: 1, iter_num: 300, loss: 0.9060, 46.15%
  17. epoch: 1, iter_num: 400, loss: 0.2629, 61.54%
  18. epoch: 1, iter_num: 500, loss: 0.5542, 76.92%
  19. epoch: 1, iter_num: 600, loss: 0.8182, 92.31%
  20. Epoch: 1, Average training loss: 0.5034
  21. Accuracy: 0.8282
  22. Average testing loss: 0.6348
  23. -------------------------------
  24. ------------Epoch: 2 ----------------
  25. epoch: 2, iter_num: 100, loss: 0.3969, 15.38%
  26. epoch: 2, iter_num: 200, loss: 0.4862, 30.77%
  27. epoch: 2, iter_num: 300, loss: 0.1850, 46.15%
  28. epoch: 2, iter_num: 400, loss: 0.0726, 61.54%
  29. epoch: 2, iter_num: 500, loss: 0.0423, 76.92%
  30. epoch: 2, iter_num: 600, loss: 0.3782, 92.31%
  31. Epoch: 2, Average training loss: 0.3632
  32. Accuracy: 0.8351
  33. Average testing loss: 0.6708
  34. -------------------------------
  35. ------------Epoch: 3 ----------------
  36. epoch: 3, iter_num: 100, loss: 0.2537, 15.38%
  37. epoch: 3, iter_num: 200, loss: 0.0146, 30.77%
  38. epoch: 3, iter_num: 300, loss: 0.0781, 46.15%
  39. epoch: 3, iter_num: 400, loss: 0.0059, 61.54%
  40. epoch: 3, iter_num: 500, loss: 0.4188, 76.92%
  41. epoch: 3, iter_num: 600, loss: 0.2212, 92.31%
  42. Epoch: 3, Average training loss: 0.2607
  43. Accuracy: 0.8382
  44. Average testing loss: 0.7442
  45. -------------------------------
  46. ------------Epoch: 4 ----------------
  47. epoch: 4, iter_num: 100, loss: 0.0064, 15.38%
  48. epoch: 4, iter_num: 200, loss: 0.3630, 30.77%
  49. epoch: 4, iter_num: 300, loss: 0.0475, 46.15%
  50. epoch: 4, iter_num: 400, loss: 0.1398, 61.54%
  51. epoch: 4, iter_num: 500, loss: 0.0512, 76.92%
  52. epoch: 4, iter_num: 600, loss: 0.3366, 92.31%
  53. Epoch: 4, Average training loss: 0.1803
  54. Accuracy: 0.8294
  55. Average testing loss: 0.8796
  56. -------------------------------
  57. ------------Epoch: 5 ----------------
  58. epoch: 5, iter_num: 100, loss: 0.0128, 15.38%
  59. epoch: 5, iter_num: 200, loss: 0.0030, 30.77%
  60. epoch: 5, iter_num: 300, loss: 0.2542, 46.15%
  61. epoch: 5, iter_num: 400, loss: 0.0501, 61.54%
  62. epoch: 5, iter_num: 500, loss: 0.6167, 76.92%
  63. epoch: 5, iter_num: 600, loss: 0.2372, 92.31%
  64. Epoch: 5, Average training loss: 0.1330
  65. Accuracy: 0.8294
  66. Average testing loss: 0.9589
  67. -------------------------------
  68. ------------Epoch: 6 ----------------
  69. epoch: 6, iter_num: 100, loss: 0.0014, 15.38%
  70. epoch: 6, iter_num: 200, loss: 0.0029, 30.77%
  71. epoch: 6, iter_num: 300, loss: 0.0016, 46.15%
  72. epoch: 6, iter_num: 400, loss: 0.0014, 61.54%
  73. epoch: 6, iter_num: 500, loss: 0.0013, 76.92%
  74. epoch: 6, iter_num: 600, loss: 0.0021, 92.31%
  75. Epoch: 6, Average training loss: 0.0902
  76. Accuracy: 0.8363
  77. Average testing loss: 1.0954
  78. -------------------------------
  79. ------------Epoch: 7 ----------------
  80. epoch: 7, iter_num: 100, loss: 0.0009, 15.38%
  81. epoch: 7, iter_num: 200, loss: 0.0130, 30.77%
  82. epoch: 7, iter_num: 300, loss: 0.0011, 46.15%
  83. epoch: 7, iter_num: 400, loss: 0.0229, 61.54%
  84. epoch: 7, iter_num: 500, loss: 0.0024, 76.92%
  85. epoch: 7, iter_num: 600, loss: 0.0031, 92.31%
  86. Epoch: 7, Average training loss: 0.0690
  87. Accuracy: 0.8386
  88. Average testing loss: 1.1462
  89. -------------------------------
  90. ------------Epoch: 8 ----------------
  91. epoch: 8, iter_num: 100, loss: 0.0585, 15.38%
  92. epoch: 8, iter_num: 200, loss: 0.0040, 30.77%
  93. epoch: 8, iter_num: 300, loss: 0.0029, 46.15%
  94. epoch: 8, iter_num: 400, loss: 0.0013, 61.54%
  95. epoch: 8, iter_num: 500, loss: 0.1690, 76.92%
  96. epoch: 8, iter_num: 600, loss: 0.0010, 92.31%
  97. Epoch: 8, Average training loss: 0.0476
  98. Accuracy: 0.8278
  99. Average testing loss: 1.2720
  100. -------------------------------
  101. ------------Epoch: 9 ----------------
  102. epoch: 9, iter_num: 100, loss: 0.0019, 15.38%
  103. epoch: 9, iter_num: 200, loss: 0.0007, 30.77%
  104. epoch: 9, iter_num: 300, loss: 0.3333, 46.15%
  105. epoch: 9, iter_num: 400, loss: 0.0404, 61.54%
  106. epoch: 9, iter_num: 500, loss: 0.2406, 76.92%
  107. epoch: 9, iter_num: 600, loss: 0.0005, 92.31%
  108. Epoch: 9, Average training loss: 0.0434
  109. Accuracy: 0.8286
  110. Average testing loss: 1.3437
  111. -------------------------------

 【ELECTRA】分类15类的中文预训练模型和tokenizer

  1. from transformers import ElectraTokenizerFast, ElectraForSequenceClassification
  2. # 加载 ELECTRA tokenizer
  3. tokenizer = ElectraTokenizerFast.from_pretrained('hfl/chinese-electra-180g-base-discriminator')
  4. # 加载 ELECTRA 预训练模型
  5. model = ElectraForSequenceClassification.from_pretrained('hfl/chinese-electra-180g-base-discriminator', num_labels=15)

这段代码使用 ElectraTokenizerFast类从 "hfl/chinese-electra-180g-base-discriminator" checkpoint 加载 tokenizer,并使用 ElectraForSequenceClassification类从相同的 checkpoint 加载预训练模型。在这个例子中,该模型用于中文文本分类任务,因此 num_labels 参数被设置为 15,这是数据集中的类别数目。

ELECTRA(中文版):ELECTRA 是由斯坦福大学提出的新型预训练模型,其训练方法使用了替换和重构两种策略,相较于 BERT 而言,其训练速度较快,且在相同的数据分布下能够获得更好的效果。ELECTRA 的中文版目前也已经发布,你可以通过 Hugging Face 的 Transformers 库来使用。

【SpanBERT】分类15类的中文预训练模型和tokenizer

  1. from transformers import AutoTokenizer, AutoModelForSequenceClassification
  2. # 加载 SpanBERT tokenizer
  3. tokenizer = AutoTokenizer.from_pretrained("SpanBert/spanbert-base-cased")
  4. # 加载 SpanBERT 预训练模型,用于分类任务
  5. model = AutoModelForSequenceClassification.from_pretrained("SpanBert/spanbert-base-cased", num_labels=15)
  6. #——————————————————————————————————————————————————————————————————————————————
  7. from transformers import AutoTokenizer, AutoModelForSequenceClassification
  8. # 加载分类15类 tokenizer
  9. tokenizer = AutoTokenizer.from_pretrained("bert-base-chinese")
  10. # 加载分类15类 SpanBERT 预训练模型,用于分类任务
  11. model = AutoModelForSequenceClassification.from_pretrained("microsoft/SpanBERT-Base-CHinese-15cls", num_labels=15)

这段代码使用 AutoTokenizer 类从 "SpanBert/spanbert-base-cased" checkpoint 加载 tokenizer,并使用 AutoModelForSequenceClassification 类从相同的 checkpoint 加载预训练模型。在这个例子中,该模型用于中文文本分类任务,因此 num_labels 参数被设置为 15,这是数据集中的类别数目。

比bert效果好的文本分类模型:

  1. ALBERT:与BERT具有相似的架构,但使用了一些技巧以减少参数数量,提高训练速度和效果。
  2. XLNet:一种新型的自回归预训练模型,通过排列语言模型任务和掩码语言模型任务相结合的方式进行预训练。
  3. RoBERTa:在BERT的基础上进一步优化,使用更长的训练时间、更大的训练数据和更细致的超参数搜索等策略。
  4. ELECTRA:通过使用对抗性训练来替代BERT中的掩码语言模型任务,从而提高预训练效果。

常见的文本分类模型包括:

  1. 朴素贝叶斯分类器(Naive Bayes Classifier)
  2. 支持向量机分类器(Support Vector Machine Classifier)
  3. 决策树分类器(Decision Tree Classifier)
  4. 随机森林分类器(Random Forest Classifier)
  5. 神经网络分类器(Neural Network Classifier)
  6. 卷积神经网络分类器(Convolutional Neural Network Classifier)
  7. 循环神经网络分类器(Recurrent Neural Network Classifier)
  8. 长短期记忆网络分类器(Long Short-Term Memory Network Classifier)等等。
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/666510
推荐阅读
相关标签
  

闽ICP备14008679号