Pytorch-手动实现Bert的训练过程(简写版)

该博客介绍了如何在PyTorch中手动实现Bert的训练过程,重点在于数据预处理,包括构造单词表和映射,以及设置超参数。接着详细阐述了Dataloader的实现,包括数据生成和DataLoader的编写,特别是如何进行随机mask操作以模拟Bert的训练策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

视频讲解

直接看这个-->Github

导包:

import re
import math
import torch
import numpy as np
from random import *
import torch.nn as nn
import torch.optim as optim
import torch.utils.data as Data

1. 数据预处理

1.1 构造单词表和映射

text = (
    'Hello, how are you? I am Romeo.\n'                   # R
    'Hello, Romeo My name is Juliet. Nice to meet you.\n' # J
    'Nice to meet you too. How are you today?\n'          # R
    'Great. My baseball team won the competition.\n'      # J
    'Oh Congratulations, Juliet\n'                        # R
    'Thank you Romeo\n'                                   # J
    'Where are you going today?\n'                        # R
    'I am going shopping. What about you?\n'              # J
    'I am going to visit my grandmother. she is not very well' # R
)
sentences = re.sub("[.,!?\\-]", '', text.lower()).split('\n')    # filter '.', ',', '?', '!'

# 所有句子的单词list
word_list = list(set(" ".join(sentences).split()))               # ['hello', 'how', 'are', 'you',...]

# 给单词表中所有单词设置序号
word2idx = {'[PAD]' : 0, '[CLS]' : 1, '[SEP]' : 2, '[MASK]' : 3}
for i, w in enumerate(word_list):
    word2idx[w] = i + 4

# 用于 idx 映射回 word
idx2word = {i: w for i, w in enumerate(word2idx)}
vocab_size = len(word2idx)         # 40

# token: 就是每个单词在词表中的index
token_list = list()                # token_list存储了每一句的token
for sentence in sentences:
    arr = [word2idx[s] for s in sentence.split()]
    token_list.append(arr)

展示一下

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值