BERT模型的应用实例

BERT(Bidirectional Encoder Representations from Transformers)模型在自然语言处理(NLP)中有广泛的应用,以下是一些具体的应用实例:

1.文本分类:

  • 利用BERT模型对新闻文章进行情感分析,判断评论的情感极性(积极、消极或中立)。
   from transformers import BertForSequenceClassification, BertTokenizer
   
   # 加载预训练模型和分词器
   model = BertForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=3)
   tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')

   # 假设text是一个待分类的文本字符串
   text = "I really enjoyed the movie!"
   encoded_input = tokenizer(text, return_tensors='pt')

   # 获取模型预测
   outputs = model(**encoded_input)
   logits = outputs.logits
   prediction = torch.argmax(logits).item()

2.问答系统:

  • BERT可以用来抽取给定段落中的答案,如SQuAD等阅读理解任务。
   from transformers import BertForQuestionAnswering, BertTokenizer

   model = BertForQuestionAnswering.from_pretrained('bert-large-uncased-whole-word-masking-finetuned-squad')
   tokenizer 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值