ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【零基础-2】PaddlePaddle学习Bert

2021-11-24 21:33:05  阅读:264  来源: 互联网

标签:Bert tokenizer inputs sequence PaddlePaddle ids 学习 token example


概要

【零基础-1】PaddlePaddle学习Bert_ 一只博客-CSDN博客icon-default.png?t=LA92https://blog.csdn.net/qq_42276781/article/details/121488335

Cell 3

# 调用bert模型用的tokenizer
tokenizer = ppnlp.transformers.BertTokenizer.from_pretrained('bert-base-chinese')
inputs_1 = tokenizer('今天天气真好')
print(inputs_1)
inputs_2 = tokenizer('明天会下雨吗')
print(inputs_2)

调用bert预训练分词工具,这里的bert-base-chinese,结合论文来看应该是bert-base的中文版。

具体情况可以参照文档。PaddleNLP Transformer API — PaddleNLP 文档icon-default.png?t=LA92https://paddlenlp.readthedocs.io/zh/latest/model_zoo/transformers.html?highlight=from_pretrained

tokenizer的主要工作应该是文本向量化,即把中文句子中的每个字转换成与其对应的数字编码,使得机器可以理解。

从输出结果来看,第一个id一定是101,最后一个id一定是102,剩下的id分别与单个汉字对应。对照两个输出结果,可以发现“天”对应的数字编码为1921,是固定的,并不会因为输入而改变。

Cell 4

# 输出训练集的前 10 条样本
for idx, example in enumerate(train_ds):
    if idx < 10:
        print(example)

这里的enumerate是python的内置函数,用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中,详见

Python enumerate() 函数 | 菜鸟教程 (runoob.com)icon-default.png?t=LA92https://www.runoob.com/python/python-func-enumerate.html这段代码就是输出训练集中的前10个样本,看看里面的内容。

 可以发现训练集中的样本没有qid,文本是对某个事物的评价,label为1表示该文本是正向评价,0表示该为本是负向评价。

Cell 5

# 超参数
EPOCHS = 10  # 训练的轮数
BATCH_SIZE = 8  # 批大小
MAX_LEN = 300  # 文本最大长度
LR = 1e-5  # 学习率
WARMUP_STEPS = 100  # 热身步骤
T_TOTAL = 1000  # 总步骤

这里是定义了一些参数,具体有什么用途我也不是很清楚。

Cell 6

# 将文本内容转化成模型所需要的token id
def convert_example(example, tokenizer, max_seq_length=512, is_test=False):
    """
    Builds model inputs from a sequence or a pair of sequence for sequence classification tasks
    by concatenating and adding special tokens. And creates a mask from the two sequences passed 
    to be used in a sequence-pair classification task.
    """
    encoded_inputs = tokenizer(text=example["text"], max_seq_len=max_seq_length)
    input_ids = encoded_inputs["input_ids"]
    token_type_ids = encoded_inputs["token_type_ids"]

    if not is_test:
        label = np.array([example["label"]], dtype="int64")
        return input_ids, token_type_ids, label
    else:
        return input_ids, token_type_ids

这段代码的理解可以对照下面这个文档链接

PaddleNLP Transformer API — PaddleNLP 文档icon-default.png?t=LA92https://paddlenlp.readthedocs.io/zh/latest/model_zoo/transformers.html?highlight=convert_example#id2Builds model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. And creates a mask from the two sequences passed  to be used in a sequence-pair classification task.

这段话大概是说,通过连接和添加特殊标记,从序列或序列对中构建模型for序列分类任务。并从传递的两个序列创建一个掩码,以用于序列对分类任务。

这里函数传入的tokenizer应该就是咱们在Cell 3里引入的tokenizer,max_seq_len就是分词器所能接收的最大句子长度,input_ids就是汉字对应的数字编码,token_type_ids暂时还不知的是什么意思。

is_test就是用来区分是否是测试集,如果不是就额外返回样本标签。

标签:Bert,tokenizer,inputs,sequence,PaddlePaddle,ids,学习,token,example
来源: https://blog.csdn.net/qq_42276781/article/details/121523268

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有