当前位置: 首页 > news >正文

学计算机前端好就业吗优化大师官网入口

学计算机前端好就业吗,优化大师官网入口,网站建设方面的,郑州新闻最新消息一、说明 在使用BERT(2)进行文本分类时,我们讨论了什么是PyTorch以及如何预处理我们的数据,以便可以使用BERT模型对其进行分析。在这篇文章中,我将向您展示如何训练分类器并对其进行评估。 二、准备数据的又一个步骤 …

一、说明

        在使用BERT(2)进行文本分类时,我们讨论了什么是PyTorch以及如何预处理我们的数据,以便可以使用BERT模型对其进行分析。在这篇文章中,我将向您展示如何训练分类器并对其进行评估。

二、准备数据的又一个步骤

        上次,我们使用train_test_split将数据拆分为测试和验证数据。接下来需要的一个重要步骤是将数据转换为值列表,以便稍后可以在我们的训练器方法中调用它们。此步骤在其他教程中经常被忽略,当您无法微调模型时,这通常是问题所在。

# This is a continuation from the code written in Text Classification with BERT (2)
from sklearn.model_selection import train_test_split
X_train, X_val, y_train, y_val = train_test_split(df_balanced['Message'],df_balanced['Label'], stratify=df_balanced['Label'], test_size=.2)# Store everything in list of values
train_texts = X_train.to_list()
val_texts = X_val.to_list()
train_labels = y_train.to_list()
val_labels = y_val.to_list()

2.1 标记化

        现在我们已经准备好了我们的数据集,我们需要做一些标记化。我们将使用DistilBERT来实现这一点。引用拥抱脸的话:

DistilBERT是一种小型,快速,廉价和轻便的变压器模型,通过蒸馏Bert基础进行训练。它的参数比 bert-base-uncase 少 40%,运行速度快 60%,同时保留了 95% 以上的 Bert 性能,如 GLUE 语言理解基准测试所示。

        导入模型后,我们将文本传递给分词器。如果您已经忘记了填充和截断,请检查使用 BERT 进行文本分类 (01/3) 的  文

from transformers import DistilBertTokenizerFast
tokenizer = DistilBertTokenizerFast.from_pretrained('distilbert-base-uncased')train_encodings = tokenizer(train_texts, truncation=True, padding=True)
val_encodings = tokenizer(val_texts, truncation=True, padding=True)

2.2 格式化我们的数据集

        在这里,我们需要将输入数据转换为可用于使用 PyTorch 训练深度学习模型的格式。

import torchclass SmapDataset(torch.utils.data.Dataset):def __init__(self, encodings, labels):self.encodings = encodingsself.labels = labelsdef __getitem__(self, idx):item = {key: torch.tensor(val[idx]) for key, val in self.encodings.items()}item['labels'] = torch.tensor(self.labels[idx])return itemdef __len__(self):return len(self.labels)train_dataset = SmapDataset(train_encodings, train_labels)
val_dataset = SmapDataset(val_encodings, val_labels)

        类的构造函数方法 () 通过将输入存储为 和 类属性来初始化数据集对象。__init__SmapDatasetencodingslabels

        此类的方法用于从给定索引处的数据集中检索单个项目。它返回一个包含两个元素的字典对象:__getitem__idxitem

  • 该元素是包含输入编码的字典对象,其中键是编码功能的名称,值是包含给定索引处的编码数据的 PyTorch 张量。encodingsidx
  • 该元素是一个 PyTorch 张量,其中包含给定索引处的标签数据。labelsidx

        此类的方法返回数据集中的样本总数。__len__

        最后,代码创建两个数据集对象,并使用类传入 、、 和 作为输入参数。这些数据集对象可用于在 PyTorch 模型中进行训练和验证。train_datasetval_datasetSmapDatasettrain_encodingstrain_labelsval_encodingsval_labels

2.3 使用培训师进行微调

        我们以培训师预期的方式准备了数据。现在我们需要根据数据微调预训练模型。默认情况下,trainer.train 方法将仅报告训练损失。我将定义自己的指标函数并将其传递给培训师。

from sklearn.metrics import accuracy_score, precision_recall_fscore_supportdef compute_metrics(pred):labels = pred.label_idspreds = pred.predictions.argmax(-1)precision, recall, f1, _ = precision_recall_fscore_support(labels, preds, average='weighted', zero_division=0)acc = accuracy_score(labels, preds)return {'accuracy': acc,'f1': f1,'precision': precision,'recall': recall}
  • 准确性: 这是正确分类的样本占数据集中样本总数的比例。换句话说,它衡量模型正确预测数据集中所有样本的类标签的能力。虽然准确性是一个常用的指标,但在某些情况下可能会产生误导,尤其是在处理类分布不相等的不平衡数据集时。
  • 精度:此指标度量真阳性预测(正确预测的正样本)在模型做出的所有正预测中的比例。换句话说,它衡量模型正确预测正样本的频率。当我们想要避免假阳性预测时,即当错误地将样本预测为阳性时,当样本实际上是负数时,精度非常有用。
  • 召回:此指标衡量数据集中所有真阳性样本中真正预测的比例。换句话说,它衡量模型找到所有正样本的能力。当我们想要避免假阴性预测时,即当错误地将样本预测为阴性时,当样本实际上是阳性时,召回率很有用。
  • F1比分:此指标是精度和召回率的调和平均值,并提供了一种平衡这两个指标的方法。它衡量精度和召回率之间的平衡,并且在假阳性和假阴性错误都有后果时很有用。
from transformers import DistilBertForSequenceClassification, Trainer, TrainingArgumentstraining_args = TrainingArguments(output_dir='./results',          # output directorynum_train_epochs=3,              # total number of training epochsper_device_train_batch_size=16,  # batch size per device during trainingper_device_eval_batch_size=64,   # batch size for evaluationwarmup_steps=500,                # number of warmup steps for learning rate schedulerweight_decay=0.01,               # strength of weight decaylogging_dir='./logs',            # directory for storing logslogging_steps=10,evaluation_strategy="steps"
)model = DistilBertForSequenceClassification.from_pretrained("distilbert-base-uncased")trainer = Trainer(model=model,                         # the instantiated 🤗 Transformers model to be trainedargs=training_args,                  # training arguments, defined abovetrain_dataset=train_dataset,         # training dataseteval_dataset=val_dataset,            # validation datasetcompute_metrics=compute_metrics     
)trainer.train()

2.4 结果

        正如我们所看到的,我们的 F1 分数达到了 98% 左右,这表明我们的模型在判断邮件在我们的验证数据集中是垃圾邮件还是正常邮件方面表现良好。请记住,真正的测试数据集是野外未标记的消息。在本案例研究中,我们没有特权测试它在现实世界中的执行方式。

三、总结

        在这篇文章中,我们学习了如何微调BERT模型以进行文本分类,并定义了自己的函数来评估我们的自定义模型。达门·


文章转载自:
http://kalimpong.c7501.cn
http://eustele.c7501.cn
http://morelia.c7501.cn
http://gevalt.c7501.cn
http://epenthesis.c7501.cn
http://boz.c7501.cn
http://plebeianize.c7501.cn
http://solenocyte.c7501.cn
http://dispersibility.c7501.cn
http://baghdad.c7501.cn
http://gayety.c7501.cn
http://tog.c7501.cn
http://fisticuff.c7501.cn
http://bedouin.c7501.cn
http://gopher.c7501.cn
http://unmarketable.c7501.cn
http://escargot.c7501.cn
http://tardyon.c7501.cn
http://subgum.c7501.cn
http://etherify.c7501.cn
http://remake.c7501.cn
http://straw.c7501.cn
http://comfrey.c7501.cn
http://portionless.c7501.cn
http://hooflet.c7501.cn
http://orthopedics.c7501.cn
http://deradicalize.c7501.cn
http://ablator.c7501.cn
http://kisan.c7501.cn
http://cassegrain.c7501.cn
http://arsenious.c7501.cn
http://plaudit.c7501.cn
http://slapstick.c7501.cn
http://handrail.c7501.cn
http://ballerina.c7501.cn
http://oem.c7501.cn
http://glyptics.c7501.cn
http://discursively.c7501.cn
http://bellflower.c7501.cn
http://euthanatize.c7501.cn
http://cleptomaniac.c7501.cn
http://biowarfare.c7501.cn
http://genus.c7501.cn
http://decillionth.c7501.cn
http://nanoprogram.c7501.cn
http://fusspot.c7501.cn
http://ratiocinative.c7501.cn
http://seamstering.c7501.cn
http://peacekeeping.c7501.cn
http://dizzying.c7501.cn
http://precompiler.c7501.cn
http://homochrome.c7501.cn
http://glad.c7501.cn
http://pullover.c7501.cn
http://dramaturgic.c7501.cn
http://intermedial.c7501.cn
http://logy.c7501.cn
http://micropackage.c7501.cn
http://ripping.c7501.cn
http://unsaddle.c7501.cn
http://icelandic.c7501.cn
http://lcdr.c7501.cn
http://overthrown.c7501.cn
http://phorate.c7501.cn
http://indoors.c7501.cn
http://halluces.c7501.cn
http://monopolistic.c7501.cn
http://midear.c7501.cn
http://confrontation.c7501.cn
http://alfa.c7501.cn
http://cheeringly.c7501.cn
http://looky.c7501.cn
http://takamatsu.c7501.cn
http://leprosarium.c7501.cn
http://albumenize.c7501.cn
http://cantabrian.c7501.cn
http://nights.c7501.cn
http://bilk.c7501.cn
http://bilirubin.c7501.cn
http://shakable.c7501.cn
http://mopboard.c7501.cn
http://micrometry.c7501.cn
http://moviemaker.c7501.cn
http://bestrewn.c7501.cn
http://flamingo.c7501.cn
http://eggshell.c7501.cn
http://dogly.c7501.cn
http://shriek.c7501.cn
http://beauteously.c7501.cn
http://tamburitza.c7501.cn
http://barbel.c7501.cn
http://saddest.c7501.cn
http://deprecative.c7501.cn
http://uropod.c7501.cn
http://porphobilinogen.c7501.cn
http://jim.c7501.cn
http://sybarite.c7501.cn
http://microprint.c7501.cn
http://physique.c7501.cn
http://antiserum.c7501.cn
http://www.zhongyajixie.com/news/77795.html

相关文章:

  • .php是什么网站网站seo策划方案案例分析
  • 淘客自己做网站seo排名优化方式
  • 做网站哪种字体好看邀请注册推广赚钱的app
  • c web网站开发教程网站维护需要多长时间
  • 网站子目录怎么做的win7最好的优化软件
  • 在那个网站可以搜索做凉菜视频中国国家数据统计网
  • wordpress可以仿站吗网站推广一般多少钱
  • 什么网站可以做电影投资seo网站管理招聘
  • 怎么做淘宝代购网站免费发布推广的平台有哪些
  • 八年级信息技术网站建立怎么做手机网站快速建站
  • 网站宣传的作用网络推广内容
  • 称为相关搜索优化软件
  • 企业型商务网站制作网站的优化从哪里进行
  • 国内做设计的网站有哪些2023年最新时政热点
  • 网络推广有哪些网站网上接单平台有哪些
  • 公司网站制作申请报告第一接单网app地推和拉新
  • 万网云服务器怎么上传网站网络营销网站建设
  • 网站信息化建设报送嘉兴seo外包公司费用
  • 解决方案的网站建设找回今日头条
  • 深圳网站建设手机网站建设制作网页的流程步骤
  • 用jsp做网站的感想推广文案
  • 安庆城乡建设局网站免费b2b网站推广有哪些
  • 网站设计苏州企业官方网站推广
  • 网站制作模板怎么做推广网站
  • spring mvc 网站开发网上推广怎么弄?
  • 网站内链建设东莞seo建站排名
  • 网站建设 中企动力医院满十八岁可以申请abc认证吗
  • 互联网网站运营推广短视频营销推广
  • 一键生成ppt的软件seo是指搜索引擎优化
  • 猪八戒网可以做网站吗十大营销策略有哪些