Bert实现意图分类

来自保姆级教程,用PyTorch和BERT进行文本分类

一、bert

bert模型的下载:去抱抱脸网站bert-base-cased at main下载预训练模型,下载对应的这三个文件,这里下载的是pytorch版本

 下载后放入对应文件夹,是这样的:

 验证bert能不能调用成功:

from transformers import BertModel,BertTokenizer
BERT_PATH = './bert-base-cased'
tokenizer = BertTokenizer.from_pretrained(BERT_PATH)
print(tokenizer.tokenize('I have a good time, thank you.'))
bert = BertModel.from_pretrained(BERT_PATH)
print('load bert model over')

['I', 'have', 'a', 'good', 'time',
',', 'thank', 'you', '.'] 
load bert model over

BertTokenizer解析:BertTokenizer将数据处理成bert需要的格式

from transformers import BertTokenizer
tokenizer = BertTokenizer.from_pretrained('bert-base-cased')
example_text = 'I will watch Memento tonight'
bert_input = tokenizer(example_text,padding='max_length', 
                       max_length = 10, 
                       truncation=True,
                       return_tensors="pt")
# ------- bert_input ------
print(bert_input['input_ids'])
print(bert_input['token_type_ids'])
print(bert_input['attention_mask'])

tensor([[  101,   146,  1209,  2824,  2508,
         26173,  3568,   102,     0,     0]])
tensor([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
tensor([[1, 1, 1, 1, 1, 1, 1, 1, 0, 0]])

BertTokenizer参数:

  • padding:将每个sequence填充到指定的最大长度。
  • max_length: 每个sequence的最大长度。本示例中我们使用 10,但对于本文实际数据集,我们将使用 512,这是 BERT 允许的sequence 的最大长度。
  • truncation:如果为True,则每个序列中超过最大长度的标记将被截断。
  • return_tensors:将返回的张量类型。由于我们使用的是 Pytorch,所以我们使用pt;如果你使用 Tensorflow,那么你需要使用tf

BertTokenizer输出:bert_input

  • input_ids,它是每个 token 的 id 表示,101代表[CLS],102代表[SEP],0代表[PAD]
  • token_type_ids,它是一个 binary mask,用于标识 token 属于哪个 sequence。如果我们只有一个 sequence,那么所有的 token 类型 id 都将为 0。对于文本分类任务,token_type_ids是 BERT 模型的可选输入参数。
  • attention_mask,它是一个 binary mask,用于标识 token 是真实 word 还是只是由填充得到。如果 token 包含 [CLS]、[SEP] 或任何真实单词,则 mask 将为 1。如果 token 只是 [PAD] 填充,则 mask 将为 0

二、定义模型

bert的输出送入一层全连接层,再通过一层relu层

from torch import nn
from transformers import BertModel
import torch

class BertClassifier(nn.Module):
    def __init__(self, dropout=0.5):
        super(BertClassifier, self).__init__()
        #self.bert = BertModel.from_pretrained('bert-base-cased')
        self.bert = BertModel.from_pretrained('/home/jiqiboyi03/chenpp/bert-classification/bert-base-cased')
        self.dropout = nn.Dropout(dropout)
        self.linear = nn.Linear(768, 7)
        self.relu = nn.ReLU()

    def forward(self, input_id, mask):
        _, pooled_output = self.bert(input_ids= input_id, attention_mask=mask,return_dict=False)
       # print(pooled_output.size())#[batch_size,768] CLS的向量
        dropout_output = self.dropout(pooled_output)
        linear_output = self.linear(dropout_output)
        final_layer = self.relu(linear_output)
        return final_layer

输入input_ids和mask的格式应该是:以batch_size=2为例,input_ids应该是二维的,mask二维三维都可以

input_id=torch.tensor([[ 101,   178,   112,   173,  1176,   170,   189,  3624,  3043,  1121,
          17496,  1396, 11305,  1106,  1207, 26063,  4661,  1664, 26645,   102],

        [  101,  1110,  1175,   170, 20811,  3043,  1121, 10552,  4121,  1106,
          21718,  1179,   175,  4047, 21349,  2528,   102,     0,     0,     0]])
mask=torch.tensor([[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]],

        [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0]]])

三、数据预处理

import torch
import numpy as np
from transformers import BertTokenizer
import torch.utils.data as data

bert_path='/home/jiqiboyi03/chenpp/bert-classification/bert-base-cased'
tokenizer = BertTokenizer.from_pretrained(bert_path)
labels={'AddToPlaylist':0,'BookRestaurant':1,'GetWeather':2,'PlayMusic':3,'RateBook':4,'SearchCreativeWork':5,'SearchScreeningEvent':6}

class Dataset(data.Dataset):
    def __init__(self, df):
        self.labels = [labels[label] for label in df['category']]
        self.texts = [tokenizer(text,
                                padding='max_length',
                                max_length = 20,
                                truncation=True,
                                return_tensors="pt")
                      for text in df['text']]
    def classes(self):
        return self.labels

    def __len__(self):
        return len(self.labels)

    def get_batch_labels(self, idx):
        # Fetch a batch of labels
        return np.array(self.labels[idx])

    def get_batch_texts(self, idx):
        # Fetch a batch of inputs
        return self.texts[idx]

    def __getitem__(self, idx):
        batch_texts = self.get_batch_texts(idx)
        batch_y = self.get_batch_labels(idx)
        return batch_texts, batch_y

其中df的格式应该是:

{'category': ['PlayMusic', .....], 
'text': ['open groove shark and play native us',..... }

四、训练

from torch.optim import Adam
from tqdm import tqdm
from snips_process import Dataset
import torch
import torch.utils.data as data
import torch.nn as nn
import matplotlib.pyplot as plt


def train(model, train_data, val_data, learning_rate, epochs):
    # 通过Dataset类获取训练和验证集
    train, val = Dataset(train_data), Dataset(val_data)
    # DataLoader根据batch_size获取数据,训练时选择打乱样本
    train_dataloader = torch.utils.data.DataLoader(train, batch_size=32, shuffle=True)
    val_dataloader = torch.utils.data.DataLoader(val, batch_size=32)
    # 判断是否使用GPU
    use_cuda = torch.cuda.is_available()
    device = torch.device("cuda" if use_cuda else "cpu")
    # 定义损失函数和优化器
    criterion = nn.CrossEntropyLoss()
    optimizer = Adam(model.parameters(), lr=learning_rate)

    train_loss=[]
    train_acc=[]
    val_loss=[]
    val_acc=[]
    EPOCH=[]

    if use_cuda:
        model = model.cuda()
        criterion = criterion.cuda()
    # 开始进入训练循环
    for epoch_num in range(epochs):
        # 定义两个变量,用于存储训练集的准确率和损失
        total_acc_train = 0
        total_loss_train = 0
        # 进度条函数tqdm
        for train_input, train_label in tqdm(train_dataloader):
            train_label = train_label.to(device)#1维
            mask = train_input['attention_mask'].to(device)
            input_id = train_input['input_ids'].squeeze(1).to(device)
            # print("input_id size:",input_id.size())#[32,20]
            # print("mask size:",mask.size())
            # 通过模型得到输出
            output = model(input_id, mask)#[32,21]
            # 计算损失
            batch_loss = criterion(output, train_label)
            total_loss_train += batch_loss.item()
            # 计算精度
            acc = (output.argmax(dim=1) == train_label).sum().item()
            total_acc_train += acc
            # 模型更新
            model.zero_grad()
            batch_loss.backward()
            optimizer.step()
        # ------ 验证模型 -----------
        # 定义两个变量,用于存储验证集的准确率和损失
        total_acc_val = 0
        total_loss_val = 0
        # 不需要计算梯度
        with torch.no_grad():
            # 循环获取数据集,并用训练好的模型进行验证
            for val_input, val_label in val_dataloader:
                # 如果有GPU,则使用GPU,接下来的操作同训练
                val_label = val_label.to(device)
                mask = val_input['attention_mask'].to(device)
                input_id = val_input['input_ids'].squeeze(1).to(device)

                output = model(input_id, mask)

                batch_loss = criterion(output, val_label)
                total_loss_val += batch_loss.item()

                acc = (output.argmax(dim=1) == val_label).sum().item()
                total_acc_val += acc

        train_loss.append(total_loss_train / len(train_data['text']))
        train_acc.append(total_acc_train / len(train_data['text']))
        val_loss.append(total_loss_val / len(val_data['text']))
        val_acc.append(total_acc_val / len(val_data['text']))
        EPOCH.append(epoch_num+1)
        print(
            f'''Epochs: {epoch_num + 1} 
              | Train Loss: {total_loss_train / len(train_data['text']): .3f} 
              | Train Accuracy: {total_acc_train / len(train_data['text']): .3f} 
              | Val Loss: {total_loss_val / len(val_data['text']): .3f} 
              | Val Accuracy: {total_acc_val / len(val_data['text']): .3f}''')

    print("saving bert model......")
    torch.save(model.state_dict(),'../bert-base-cased/bert_trained_snips_full.pt')

    #画图
    plt.plot(EPOCH,train_loss,'b',label='train_loss')
    plt.plot(EPOCH, train_acc,'g',label='train_acc')
    plt.plot(EPOCH, val_loss, 'r', label='val_loss')
    plt.plot(EPOCH, val_acc, 'c', label='val_acc')
    plt.show()

注:train_data即数据预处理中的df,经过DataLoader会加一维batch_size,变成3维,但是bert的输入得是2维,因此经过了squeeze(1)操作

五、验证

from snips_process import Dataset,df_test
import torch
from model import BertClassifier
import torch.utils.data as data


def evaluate(model, test_data):
    test = Dataset(test_data)
    length=len(test_data['text'])
    test_dataloader = torch.utils.data.DataLoader(test, batch_size=2)
    use_cuda = torch.cuda.is_available()
    device = torch.device("cuda" if use_cuda else "cpu")
    if use_cuda:
        model = model.cuda()

    total_acc_test = 0
    with torch.no_grad():
        for test_input, test_label in test_dataloader:
            test_label = test_label.to(device)
            mask = test_input['attention_mask'].to(device)
            input_id = test_input['input_ids'].squeeze(1).to(device)
            output = model(input_id, mask)
            acc = (output.argmax(dim=1) == test_label).sum().item()
            total_acc_test += acc
    print(f'Test Accuracy: {total_acc_test / length: .3f}')

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
BERT是一个基于Transformer的预训练模型,可以用于文本分类任务。下面是BERT实现文本分类的步骤: 1. 数据预处理:将文本数据转化为模型可接受的格式。首先,将每个文本拆分成单词或子词(subwords)。然后,将每个单词或子词映射成其在词表(vocabulary)中的索引,得到输入序列。此外,还需要为输入序列添加特殊的标记,例如[CLS]和[SEP],分别标记句子的开头和结尾。 2. 模型搭建:使用预训练的BERT模型作为基础,通过Fine-tuning调整模型参数以适应文本分类任务。通常,在模型搭建时,会在输入序列的开头添加一个分类器(classifier),用于预测文本的类别。通过Fine-tuning可以提高模型在具体分类任务上的性能。 3. 模型训练:使用带有标注的训练数据对BERT模型进行训练。训练过程中,模型会通过反向传播算法不断更新参数,使得模型在分类任务上的损失函数逐渐减小。通常,可以使用一些优化算法(如Adam)来控制训练过程。 4. 模型评估:在模型训练完毕后,使用一部分标注好的测试数据来评估模型的性能。常用的评估指标包括准确率(accuracy)、精确率(precision)、召回率(recall)和F1值等。 5. 模型应用:经过训练的BERT模型可以用于后续的文本分类任务。对于新的、未见过的文本数据,可以将其输入到模型中,通过模型输出的预测结果来确定文本的类别。 总之,BERT实现主要包括数据预处理、模型搭建、模型训练、模型评估和模型应用等步骤。通过Fine-tuning,BERT能够在各种文本分类任务中达到较好的性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值