• 使用bert进行文本二分类


    构建BERT(Bidirectional Encoder Representations from Transformers)的训练网络可以使用PyTorch来实现。下面是一个简单的示例代码:

    1. import torch
    2. import torch.nn as nn
    3. from transformers import BertModel, BertTokenizer
    4. # Load BERT tokenizer and model
    5. tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
    6. bert_model = BertModel.from_pretrained('bert-base-uncased')
    7. # Example input sentence
    8. input_sentence = "I love BERT!"
    9. # Tokenize input sentence
    10. tokens = tokenizer.encode_plus(input_sentence, add_special_tokens=True, padding='max_length', max_length=10, return_tensors='pt')
    11. # Get input tensors
    12. input_ids = tokens['input_ids']
    13. attention_mask = tokens['attention_mask']
    14. # Define BERT-based model
    15. class BERTModel(nn.Module):
    16. def __init__(self):
    17. super(BERTModel, self).__init__()
    18. self.bert = bert_model
    19. self.fc = nn.Linear(768, 2) # Example: 2-class classification
    20. self.softmax = nn.Softmax(dim=1)
    21. def forward(self, input_ids, attention_mask):
    22. bert_output = self.bert(input_ids=input_ids, attention_mask=attention_mask)[0]
    23. pooled_output = bert_output[:, 0, :] # Use the first token's representation (CLS token)
    24. output = self.fc(pooled_output)
    25. output = self.softmax(output)
    26. return output
    27. # Initialize BERT model
    28. model = BERTModel()
    29. # Example of training process
    30. input_ids = input_ids.squeeze(0)
    31. attention_mask = attention_mask.squeeze(0)
    32. labels = torch.tensor([0]) # Example: binary classification with label 0
    33. criterion = nn.CrossEntropyLoss()
    34. optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
    35. # Training loop
    36. for epoch in range(10):
    37. optimizer.zero_grad()
    38. output = model(input_ids, attention_mask)
    39. loss = criterion(output, labels)
    40. loss.backward()
    41. optimizer.step()
    42. print(f"Epoch {epoch+1} - Loss: {loss.item()}")
    43. # Example of using trained BERT model for prediction
    44. test_sentence = "I hate BERT!"
    45. test_tokens = tokenizer.encode_plus(test_sentence, add_special_tokens=True, padding='max_length', max_length=10, return_tensors='pt')
    46. test_input_ids = test_tokens['input_ids'].squeeze(0)
    47. test_attention_mask = test_tokens['attention_mask'].squeeze(0)
    48. with torch.no_grad():
    49. test_output = model(test_input_ids, test_attention_mask)
    50. predicted_label = torch.argmax(test_output, dim=1).item()
    51. print(f"Predicted label: {predicted_label}")

    在这个示例中,使用Hugging Face的transformers库加载已经预训练好的BERT模型和tokenizer。然后定义了一个自定义的BERT模型,它包含一个BERT模型层(bert_model)和一个线性层和softmax激活函数用于分类任务。

    在训练过程中,使用交叉熵损失函数和Adam优化器进行训练。在每个训练周期中,将输入数据传递给BERT模型和线性层,计算输出并计算损失。然后更新模型的权重。

    在使用训练好的BERT模型进行预测时,我们通过输入句子使用tokenizer进行编码,并传入BERT模型获取输出。最后,我们使用argmax函数获取最可能的标签。

    请确保在运行代码之前已经安装了PyTorch和transformers库,并且已经下载了BERT预训练模型(bert-base-uncased)。可以使用pip install torch transformers进行安装。

  • 相关阅读:
    核酸检测小程序实战教程
    idea 打 jar 包以及运行使用
    【设计模式】单例模式
    云端智享——记移动云手写docker-demo
    19. 内置 Tomcat 配置和切换
    4.1 应用层Hook挂钩原理分析
    MySQL事务隔离级别
    142. 环形链表 II
    web基础与HTTP协议
    【面试经典150题】跳跃游戏Ⅱ
  • 原文地址:https://blog.csdn.net/Metal1/article/details/132890852