快速学会一个算法,BERT

今天给大家介绍一个强大的算法模型,BERT

BERT(Bidirectional Encoder Representations from Transformers)是一种基于 Transformer 架构的深度学习模型,主要用于处理自然语言处理(NLP)问题。

BERT 由 Google AI 的研究团队在 2018 年提出,它通过双向捕获上下文信息的能力彻底改变了 NLP。与之前单向读取文本的模型不同,BERT 通过考虑左右上下文来理解句子中的单词。这种能力极大地增强了它对语言细微差别的理解,使其在各种 NLP 任务中非常有效。

BERT 如何工作

BERT 架构

BERT 建立在 Transformer 的架构之上,特别是利用了其编码器部分。

BERT 由多层自注意力和前馈神经网络组成。BERT 采用双向方法从句子中前后单词捕获上下文信息。

根据模型架构的规模,BERT 有四种类型的预训练版本。

  • BERT-Base(Cased / Un-Cased),12 层,768 个隐藏节点,12 个注意头,110M 个参数

  • BERT-Large(Cased / Un-Cased),24 层,1024 个隐藏节点,16 个注意头,340M 个参数

文本预处理

BERT 的开发人员添加了一组特定规则来表示模型的输入文本。

首先,每个输入嵌入都是 3 个嵌入的组合。

  • 位置嵌入

    BERT 学习并使用位置嵌入来表达单词在句子中的位置。添加这些是为了克服 Transformer 的局限性,因为与 RNN 不同,Transformer 无法捕获 “序列” 或 “顺序” 信息。

  • 片段嵌入

    BERT 还可以将句子对作为任务的输入。这就是为什么它会为第一句和第二句学习一个独特的嵌入,以帮助模型区分它们。在上面的例子中,所有标记为 的标记都属于句子 A( 也是如此)。

  • 标记嵌入

    这些是从 WordPiece 标记词汇表中为特定标记学习到的嵌入。

    需要注意的是,我们在第一个句子的开头,将 [CLS] 标记添加到输入单词标记中,在每个句子之后,添加 [SEP] 标记。

对于给定的标记,其输入表示是通过对相应的标记、段和位置嵌入进行求和来构建的。

预训练任务

BERT 在两个 NLP 任务上进行了预训练:

  • Masked Language Model (MLM)

    在这个任务中,模型的输入序列中随机选择一些单词被替换为一个特殊的 [MASK] 标记,模型的任务是预测这些被遮蔽的单词。这促使模型学习到更加深入的语言理解能力。

  • Next Sentence Prediction (NSP)

    在这个任务中,给定两个句子A 和 B,模型需要预测B是否是A的直接后续。这有助于模型理解句子间的关系,对于某些特定的任务(如问答系统)尤为重要。

微调

  • 过程

    预训练完成后,BERT 可以通过微调来适应具体的下游任务,例如情感分析、命名实体识别等。在微调阶段,通常保持预训练期间学到的参数不变,只对输出层进行修改,以适应特定任务的需求。

  • 优化

    通过微调,BERT 能够利用预训练中获得的丰富语言理解能力,迅速适应并提高在特定任务上的表现。

案例分享

下面是一个使用 BERT 来进行情绪分析的案例。

1.加载预训练的 BERT 模型

这里,我们使用 BERT 基础模型的无大小写预训练版本。

from transformers import BertModel  
from transformers import BertTokenizerFast  bert = BertModel.from_pretrained('bert-base-uncased')  
tokenizer = BertTokenizerFast.from_pretrained('bert-base-uncased', do_lower_case=True)  
2.加载数据集

这里,我们首先加载数据集并对其进行预处理。

import re  
import pandas as pd  def preprocessor(text):  #convering text to lower case  text = text.lower()  #remove user mentions  text = re.sub(r'@[A-Za-z0-9]+','',text)             #remove hashtags  #text = re.sub(r'#[A-Za-z0-9]+','',text)           #remove links  text = re.sub(r'http\S+', '', text)    #split token to remove extra spaces  tokens = text.split()  #join tokens by space  return " ".join(tokens)  df = pd.read_csv('Sentiment.csv')  
# perform text cleaning  
df['clean_text']= df['text'].apply(preprocessor)  
# save cleaned text and labels to a variable  
text   = df['clean_text'].values  
labels = df['sentiment'].values  
print(text[50:55])

接下来,准备模型的输入输出数据。

#importing label encoder  
from sklearn.preprocessing import LabelEncoder  
#define label encoder  
le = LabelEncoder()  
#fit and transform target strings to a numbers  
labels = le.fit_transform(labels)  
print(le.classes_)  
print(labels)  # library for progress bar  
from tqdm import notebook  
# create an empty list to save integer sequence  
sent_id = []  
# iterate over each tweet  
for i in notebook.tqdm(range(len(text))):  encoded_sent = tokenizer.encode(text[i],                        add_special_tokens = True,      max_length = 25,  truncation = True,           pad_to_max_length='right')       # saving integer sequence to a list  sent_id.append(encoded_sent)  attention_masks = []  
for sent in sent_id:  att_mask = [int(token_id > 0) for token_id in sent]  attention_masks.append(att_mask)  # Use train_test_split to split our data into train and validation sets  
from sklearn.model_selection import train_test_split  # Use 90% for training and 10% for validation.  
train_inputs, validation_inputs, train_labels, validation_labels = train_test_split(sent_id, labels, random_state=2018, test_size=0.1, stratify=labels)  # Do the same for the masks.  
train_masks, validation_masks, _, _ = train_test_split(attention_masks, labels, random_state=2018, test_size=0.1, stratify=labels)  import torch  
train_inputs = torch.tensor(train_inputs)  
validation_inputs = torch.tensor(validation_inputs)  
train_labels = torch.tensor(train_labels)  
validation_labels = torch.tensor(validation_labels)  
train_masks = torch.tensor(train_masks)  
validation_masks = torch.tensor(validation_masks)  from torch.utils.data import TensorDataset, DataLoader, RandomSampler, SequentialSampler  # For fine-tuning BERT on a specific task, the authors recommend a batch size of 16 or 32.  
# define a batch size  
batch_size = 32  # Create the DataLoader for our training set.  
#Dataset wrapping tensors.  
train_data = TensorDataset(train_inputs, train_masks, train_labels)  #define a sampler for sampling the data during training  #random sampler samples randomly from a dataset   #sequential sampler samples sequentially, always in the same order  
train_sampler = RandomSampler(train_data)  #represents a iterator over a dataset. Supports batching, customized data loading order  
train_dataloader = DataLoader(train_data, sampler=train_sampler, batch_size=batch_size)  # Create the DataLoader for our validation set.  
#Dataset wrapping tensors.  
validation_data = TensorDataset(validation_inputs, validation_masks, validation_labels)  #define a sequential sampler   
#This samples data in a sequential order  
validation_sampler = SequentialSampler(validation_data)  #create a iterator over the dataset  
validation_dataloader = DataLoader(validation_data, sampler=validation_sampler, batch_size=batch_size)

3.定义模型架构

#importing nn module  
import torch.nn as nn  class classifier(nn.Module):  #define the layers and wrappers used by model  def __init__(self, bert):  #constructor  super(classifier, self).__init__()  #bert model  self.bert = bert   # dense layer 1  self.fc1 = nn.Linear(768,512)  #dense layer 2 (Output layer)  self.fc2 = nn.Linear(512,3)  #dropout layer  self.dropout = nn.Dropout(0.1)  #relu activation function  self.relu =  nn.ReLU()  #softmax activation function  self.softmax = nn.LogSoftmax(dim=1)  #define the forward pass  def forward(self, sent_id, mask):  #pass the inputs to the model    all_hidden_states, cls_hidden_state = self.bert(sent_id, attention_mask=mask, return_dict=False)  #pass CLS hidden state to dense layer  x = self.fc1(cls_hidden_state)  #Apply ReLU activation function  x = self.relu(x)  #Apply Dropout  x = self.dropout(x)  #pass input to the output layer  x = self.fc2(x)  #apply softmax activation  x = self.softmax(x)  return x  device="cuda:0"  
for param in bert.parameters():  param.requires_grad = False  
#create the model  
model = classifier(bert)  #push the model to GPU, if available  
model = model.to(device)

4.定义优化器和损失函数

# Adam optimizer  
optimizer = torch.optim.Adam(model.parameters(), lr = 0.001)  import numpy as np  
from sklearn.utils.class_weight import compute_class_weight  
#class_weights = compute_class_weight('balanced', np.unique(labels), y=labels)  
class_weights = compute_class_weight(class_weight = "balanced", classes= np.unique(labels), y= labels)  
print("Class Weights:",class_weights)  # converting a list of class weights to a tensor  
weights= torch.tensor(class_weights,dtype=torch.float)  
# transfer to GPU  
weights = weights.to(device)  
# define the loss function  
cross_entropy  = nn.NLLLoss(weight=weights) 

5.模型训练与评估

import time  
import datetime  
# compute time in hh:mm:ss  
def format_time(elapsed):  # round to the nearest second.  elapsed_rounded = int(round((elapsed)))  # format as hh:mm:ss  return str(datetime.timedelta(seconds = elapsed_rounded))
import time  
#define a function for training the model  
def train():  print("\nTraining.....")    #set the model on training phase - Dropout layers are activated  model.train()  #record the current time  t0 = time.time()  #initialize loss and accuracy to 0  total_loss, total_accuracy = 0, 0  #Create a empty list to save the model predictions  total_preds=[]  #for every batch  for step,batch in enumerate(train_dataloader):  # Progress update after every 40 batches.  if step % 40 == 0 and not step == 0:  # Calculate elapsed time in minutes.  elapsed = format_time(time.time() - t0)  # Report progress.  print('  Batch {:>5,}  of  {:>5,}.    Elapsed: {:}.'.format(step, len(train_dataloader), elapsed))  #push the batch to gpu  batch = tuple(t.to(device) for t in batch)  #unpack the batch into separate variables  # `batch` contains three pytorch tensors:  #   [0]: input ids   #   [1]: attention masks  #   [2]: labels   sent_id, mask, labels = batch  # Always clear any previously calculated gradients before performing a  # backward pass. PyTorch doesn't do this automatically.   model.zero_grad()          # Perform a forward pass. This returns the model predictions  preds = model(sent_id, mask)  #compute the loss between actual and predicted values  loss =  cross_entropy(preds, labels)  # Accumulate the training loss over all of the batches so that we can  # calculate the average loss at the end. `loss` is a Tensor containing a  # single value; the `.item()` function just returns the Python value   # from the tensor.  total_loss = total_loss + loss.item()  # Perform a backward pass to calculate the gradients.  loss.backward()  # Update parameters and take a step using the computed gradient.  # The optimizer dictates the "update rule"--how the parameters are  # modified based on their gradients, the learning rate, etc.  optimizer.step()  #The model predictions are stored on GPU. So, push it to CPU  preds=preds.detach().cpu().numpy()  #Accumulate the model predictions of each batch  total_preds.append(preds)  #compute the training loss of a epoch  avg_loss     = total_loss / len(train_dataloader)  #The predictions are in the form of (no. of batches, size of batch, no. of classes).  #So, reshaping the predictions in form of (number of samples, no. of classes)  total_preds  = np.concatenate(total_preds, axis=0)  #returns the loss and predictions  return avg_loss, total_preds
#define a function for evaluating the model  
def evaluate():  print("\nEvaluating.....")  #set the model on training phase - Dropout layers are deactivated  model.eval()  #record the current time  t0 = time.time()  #initialize the loss and accuracy to 0  total_loss, total_accuracy = 0, 0  #Create a empty list to save the model predictions  total_preds = []  #for each batch    for step,batch in enumerate(validation_dataloader):  # Progress update every 40 batches.  if step % 40 == 0 and not step == 0:  # Calculate elapsed time in minutes.  elapsed = format_time(time.time() - t0)  # Report progress.  print('  Batch {:>5,}  of  {:>5,}.    Elapsed: {:}.'.format(step, len(validation_dataloader), elapsed))  #push the batch to gpu  batch = tuple(t.to(device) for t in batch)  #unpack the batch into separate variables  # `batch` contains three pytorch tensors:  #   [0]: input ids   #   [1]: attention masks  #   [2]: labels          sent_id, mask, labels = batch  #deactivates autograd  with torch.no_grad():  # Perform a forward pass. This returns the model predictions  preds = model(sent_id, mask)  #compute the validation loss between actual and predicted values  loss = cross_entropy(preds,labels)  # Accumulate the validation loss over all of the batches so that we can  # calculate the average loss at the end. `loss` is a Tensor containing a  # single value; the `.item()` function just returns the Python value   # from the tensor.        total_loss = total_loss + loss.item()  #The model predictions are stored on GPU. So, push it to CPU  preds=preds.detach().cpu().numpy()  #Accumulate the model predictions of each batch  total_preds.append(preds)  #compute the validation loss of a epoch  avg_loss = total_loss / len(validation_dataloader)   #The predictions are in the form of (no. of batches, size of batch, no. of classes).  #So, reshaping the predictions in form of (number of samples, no. of classes)  total_preds  = np.concatenate(total_preds, axis=0)  return avg_loss, total_preds

接下来,对模型进行训练。

#Assign the initial loss to infinite  
best_valid_loss = float('inf')  #create a empty list to store training and validation loss of each epoch  
train_losses=[]  
valid_losses=[]  epochs = 5  #for each epoch  
for epoch in range(epochs):  print('\n....... epoch {:} / {:} .......'.format(epoch + 1, epochs))  #train model  train_loss, _ = train()  #evaluate model  valid_loss, _ = evaluate()  #save the best model  if valid_loss < best_valid_loss:  best_valid_loss = valid_loss  torch.save(model.state_dict(), 'saved_weights.pt')  #accumulate training and validation loss  train_losses.append(train_loss)  valid_losses.append(valid_loss)  print(f'\nTraining Loss: {train_loss:.3f}')  print(f'Validation Loss: {valid_loss:.3f}')  print("")  
print("Training complete!")

接下来,对模型进行评估。

# load weights of best model  
path='saved\_weights.pt'  
model.load\_state\_dict(torch.load(path))  
# get the model predictions on the validation data  
# returns 2 elements- Validation loss and Predictions  
valid\_loss, preds = evaluate()  
print(valid\_loss)  from sklearn.metrics import classification\_report  
# Converting the log(probabities) into a classes  
# Choosing index of a maximum value as class  
y\_pred = np.argmax(preds,axis=1)  
# actual labels  
y\_true = validation\_labels  
print(classification\_report(y\_true,y\_pred))


如何学习AI大模型?

作为一名热心肠的互联网老兵,我决定把宝贵的AI知识分享给大家。 至于能学习到多少就看你的学习毅力和能力了 。我已将重要的AI大模型资料包括AI大模型入门学习思维导图、精品AI大模型学习书籍手册、视频教程、实战学习等录播视频免费分享出来。

这份完整版的大模型 AI 学习资料已经上传CSDN,朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费

一、全套AGI大模型学习路线

AI大模型时代的学习之旅:从基础到前沿,掌握人工智能的核心技能!

img

二、640套AI大模型报告合集

这套包含640份报告的合集,涵盖了AI大模型的理论研究、技术实现、行业应用等多个方面。无论您是科研人员、工程师,还是对AI大模型感兴趣的爱好者,这套报告合集都将为您提供宝贵的信息和启示。

img

三、AI大模型经典PDF籍

随着人工智能技术的飞速发展,AI大模型已经成为了当今科技领域的一大热点。这些大型预训练模型,如GPT-3、BERT、XLNet等,以其强大的语言理解和生成能力,正在改变我们对人工智能的认识。 那以下这些PDF籍就是非常不错的学习资源。

img

四、AI大模型商业化落地方案

img

作为普通人,入局大模型时代需要持续学习和实践,不断提高自己的技能和认知水平,同时也需要有责任感和伦理意识,为人工智能的健康发展贡献力量。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/150454.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

星辰计划-深入理解kafka的消息存储和索引设计

消息存储 提到存储不得不说消息的读写&#xff0c;那么kafka他是如何读写数据的呢&#xff1f; 读取消息 1.通过debug(如何debug) 我们可以得到下面的调用栈&#xff0c;最终通过FileRecords来读取保存的数据 写入消息 1.通过debug(如何debug) 我们可以得到下面的调用栈&am…

模型django封装uvicorn服务器部署实战

Uvicorn 是一个轻量级的 ASGI 服务器&#xff0c;它基于 uvloop 和 httptools 这两个高性能的异步库。Uvicorn 提供了快速的启动时间和低延迟的响应&#xff0c;非常适合用于生产环境。 Django&#xff1a; 是一个开源且强大的Web框架&#xff0c;适用于快速开发和部署Python …

深度学习——线性回归

房价预测 线性模型 单层神经网络 损失函数的均方误差 训练数据 参数学习 显示解 偏导数少了负号 最优解y旁边的X少了转置符号 梯度下降 学习率选择 小批量随机梯度下降 批量规模的选择 总结

LeetCode 909. 蛇梯棋

LeetCode 909. 蛇梯棋 给你一个大小为 n x n 的整数矩阵 board &#xff0c;方格按从 1 到 n2 编号&#xff0c;编号遵循 转行交替方式 &#xff0c;从左下角开始 &#xff08;即&#xff0c;从 board[n - 1][0] 开始&#xff09;的每一行改变方向。 你一开始位于棋盘上的方格 …

Linux:八种重定向详解(万字长文警告)

相关阅读Linuxhttps://blog.csdn.net/weixin_45791458/category_12234591.html?spm1001.2014.3001.5482 本文将讨论Linux中的重定向相关问题&#xff0c;在阅读本文前&#xff0c;强烈建议先学习文件描述符的相关内容Linux&#xff1a;文件描述符详解。 重定向分为两类&#x…

个性化大语言模型:PPlug——让AI更懂你

在当今数字化转型的时代&#xff0c;大型语言模型&#xff08;LLMs&#xff09;已经成为了不可或缺的工具&#xff0c;它们在自然语言理解、生成和推理方面展现了非凡的能力。然而&#xff0c;这些模型普遍采用的是“一刀切”的方式&#xff0c;即对于相同的输入给予所有用户相…

MySQL的乐观锁、悲观锁机制及实现

乐观锁 乐观锁的实现参考了这篇文章&#xff0c;里面还将了乐观锁的时间戳实现方式&#xff1a; 跳转 概述 乐观锁是一种并发控制策略&#xff0c;它假设多个事务不会发生冲突&#xff0c;在执行操作时不加锁&#xff0c;非常乐观&#xff0c;只需每次进行提交时利用标识进行…

ansible批量安装postgresql软件

本文为杭州云贝教育 刘老师 原创&#xff0c;请尊重知识产权&#xff0c;转发请注明出处&#xff0c;不接受任何抄袭、演绎和未经注明出处的转载。 随着分布式系统和大规模应用的普及&#xff0c;自动化部署和管理变得越来越重要。Ansible 是一种流行的自动化工具&#xff0c;它…

DAY80服务攻防-中间件安全HW2023-WPS 分析WeblogicJettyJenkinsCVE

知识点 1、中间件-Jetty-CVE&信息泄漏 2、中间件-Jenkins-CVE&RCE执行 3、中间件-Weblogic-CVE&反序列化&RCE 4、应用WPS-HW2023-RCE&复现&上线CS 中间件-Jetty-CVE&信息泄漏 Jetty是一个开源的servlet容器&#xff0c;它为基于Java的Web容器…

分布式光伏监控系统 在鄂尔多斯市鄂托克旗某煤矿项目中的应用

摘 要&#xff1a;分布式光伏发电就是将太阳能光伏板分散布置在各个区域&#xff0c;通过小规模、模块化的方式实现电能的并网或独立使用&#xff0c;这种发电方式具有就近发电、就近并网、就近转换、就近使用的特点。近年来&#xff0c;技术和政策支持推动了光伏组件的成本持续…

sed(1):强大的文本处理命令

一、命令简介 ​sed​&#xff08;stream editor&#xff09;是一个强大的文本处理工具&#xff0c;它能够执行基本的文本转换&#xff0c;如替换、删除、插入和修改文本行的特定部分。sed​ 命令通常用于对文本文件进行批量编辑&#xff0c;也可以用于处理来自管道的输入。 …

煤矿井下钻场目标检测数据集 5类 voc格式

煤矿井下钻场目标检测数据集 本数据集包含了来自不同钻场和环境背景条件下的70948张图片&#xff0c;涵盖了夹持器、钻机卡盘、煤矿工人、矿井安全帽和钻杆等五类目标&#xff0c;并提供了PASCAL VOC格式的标注文件。 摘要 煤矿井下钻场打钻是解决瓦斯灾害、水害、隐蔽地质灾害…

9.24-k8s服务发布

Ingress 使用域名发布 K8S 服务 部署项目 一、先部署mariadb [rootk8s-master ~]# mkdir aaa [rootk8s-master ~]# cd aaa/ [rootk8s-master aaa]# # 先部署mariadb [rootk8s-master aaa]# # configmap [rootk8s-master aaa]# vim mariadb-configmap.yaml apiVersion: v1 ki…

灵当CRM multipleUpload.php 文件上传致RCE漏洞复现

0x01 产品描述&#xff1a; 灵当CRM是一款专为中小企业量身定制的智能客户关系管理工具&#xff0c;由上海灵当信息科技有限公司开发和运营。该系统广泛应用于多个行业&#xff0c;包括金融、教育、医疗、IT服务及房地产等领域&#xff0c;旨在满足企业对客户个性化管理的需求&…

使用yum为centos系统安装软件以及使用(包含阿里云yum源配置)

centos系统配置阿里云yum源 因为centos7官方停止维护&#xff0c;自带yum源用不了了&#xff0c;所以可以更换成阿里云yum源 方法&#xff1a; 使用root权限执行以下语句 curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo CentOS…

【LinuxC高级】汇总

ls ls -l&#xff1a;显示文件的详细信息 ls -a&#xff1a;显示隐藏文件 ls -lh&#xff1a;文件大小单位显示 ls -i&#xff1a;显示文件的inode号 修改密码 passwd 用户名 su 用户名 -----> 用户名 su ----> 如果不加用户名&#xff0c;默认切换到超级用户 cd cd 路径…

YOLOv10改进,YOLOv10主干网络替换为VanillaNet( CVPR 2023 华为提出的全新轻量化架构),大幅度涨点

摘要 基础模型的核心理念是“更多即不同”,这一理念在计算机视觉和自然语言处理领域取得了惊人的成功。然而,变压器模型的优化挑战和固有复杂性呼唤一种向简化转变的范式。在本研究中,引入了 VanillaNet,一种拥抱设计优雅的神经网络架构。通过避免高深度、快捷方式和复杂操…

MySQL—存储过程详解

基本介绍 存储过程和函数是数据库中预先编译并存储的一组SQL语句集合。它们的主要目的是提高代码的复用性、减少数据传输、简化业务逻辑处理&#xff0c;并且一旦编译成功&#xff0c;可以永久有效。 存储过程和函数的好处 提高代码的复用性&#xff1a;存储过程和函数可以在…

记某地级市护网的攻防演练行动

0x1 前言 哈喽&#xff0c;师傅们&#xff01; 这次给师傅们分享的是上上个星期的地级市护网的攻防演练的两个案例&#xff0c;涉及到的知识点可能比较偏&#xff0c;下面我也会提前给师傅们拓展下改漏洞相关的知识点内容。护网攻防演练中&#xff0c;涉及到的很多敏感内容这…

【Linux】驱动的基本架构和编译

驱动源码 /** Silicon Integrated Co., Ltd haptic sih688x haptic driver file** Copyright (c) 2021 kugua <daokuan.zhusi-in.com>** This program is free software; you can redistribute it and/or modify it* under the terms of the GNU General Public Licen…