使用预训练的模型BERT来完成对整个竞赛的数据分析
lf0517
发表于 2023-1-14 22:04:15
1038
0
0
导入需要的库! S& z/ H! q3 G+ ]9 z
import numpy as np' [$ _+ X" w3 Q5 ?$ q
import pandas as pd( B" I' x0 ~6 A2 V2 W
from math import ceil, floor4 r& v& R6 U/ s5 K& |* {
import tensorflow as tf
import tensorflow.keras.layers as L
from tensorflow.keras.initializers import TruncatedNormal8 I, ~1 t$ x* ?" p
from sklearn import model_selection
from transformers import BertConfig, TFBertPreTrainedModel, TFBertMainLayer+ o3 V/ `# L% Z! c9 n
from tokenizers import BertWordPieceTokenizer
读取并解释数据0 d9 z$ H/ f4 u/ R) S" |
在竞赛中,对数据的理解是非常关键的。因此我们首先要做的就是读取数据,然后查看数据的内容以及特点。$ z9 d. S' @: E8 l. Z7 Y' `+ ] H9 B; }
先用pandas来读取csv数据,. _, t% G6 k6 _9 {. k) ^' E" s2 Z' w
train_df = pd.read_csv('train.csv')
train_df.dropna(inplace=True)
test_df = pd.read_csv('test.csv')
test_df.loc[:, "selected_text"] = test_df.text.values
submission_df = pd.read_csv('sample_submission.csv')
再查看下我们的数据的数量,我们一共有27485条训练数据,3535条测试数据,8 x4 r1 m0 q- U) S+ e2 u
print("train numbers =", train_df.shape)
print("test numbers =", test_df.shape)
紧接着查看训练数据和测试数据前10条表单的字段跟数据,表单中包含了一下几个数据字段:$ x4 j2 r, F4 X8 N& i
. Z0 A# U& y1 c* ^
textID: 文本数据记录的唯一ID;7 F9 E ^$ W8 T3 V( T7 ^3 R) U
: ]; Z/ X7 G, e- [8 [& q
text: 原始语句;
selected_text: 表示情感的语句;
sentiment: 情感类型, neutral中立, positive积极, negative消极;# j# L' @4 n" Q6 x1 U9 W* ~
* X8 W2 h0 a- |2 X
从数据中我们可以得出,目标就是根据现有的情感从原本是的语句中选出能代表这个情感的语句部分。- R% P7 u6 q! `7 K, l
train_df.head(10)
test_df.head(10)% l! h4 S L- F. I
定义常量# U2 a3 m. `2 n B0 P
# bert预训练权重跟数据存放的目录4 T" V( y- X, n1 G' R) K
PATH = "./bert-base-uncased/"
# 语句最大长度
MAX_SEQUENCE_LENGTH = 128
载入词向量, U; g: V0 u3 K- v4 E, e+ F
BERT是依据一个固定的词向量来进行训练的。因此在竞赛中需要先使用BertWordPieceTokenizer来加载这些词向量,其中的lowercase=True表示所有的词向量都是小写。设置大小写不敏感可以减少模型对资源的占用。
TOKENIZER = BertWordPieceTokenizer(f"{PATH}/vocab.txt", lowercase=True)
定义数据加载器
定义数据预处理函数
def preprocess(tweet, selected_text, sentiment):
1 N- _; z: E; e+ q6 }. ^% e
# 将被转成byte string的原始字符串转成utf-8的字符串
tweet = tweet.decode('utf-8')/ V- e0 t8 d0 u: I0 r2 X4 Y
selected_text = selected_text.decode('utf-8')
sentiment = sentiment.decode('utf-8'); ~" N1 y2 L# @3 ~
tweet = " ".join(str(tweet).split()). K# A5 j# B4 c& H4 H H6 m
selected_text = " ".join(str(selected_text).split())
# 标记出selected text和text共有的单词; ^5 [* B, o. P' M
idx_start, idx_end = None, None
for index in (i for i, c in enumerate(tweet) if c == selected_text[0]):
if tweet[index:index+len(selected_text)] == selected_text:
idx_start = index
idx_end = index + len(selected_text)
break8 `/ n) Z2 z8 s4 k9 ~
intersection = [0] * len(tweet)9 p9 {) r% y' M; A: b- X& O! n
if idx_start != None and idx_end != None:0 r, Y+ f3 |6 T; z
for char_idx in range(idx_start, idx_end):
intersection[char_idx] = 1
: v& T1 n' n* \) M
# 对原始数据用词向量进行编码, 这里会返回原始数据中的词在词向量中的下标
# 和原始数据中每个词向量的单词在文中的起始位置跟结束位置
enc = TOKENIZER.encode(tweet)
input_ids_orig, offsets = enc.ids, enc.offsets# {4 |& l) }8 R: ^' O
target_idx = []
for i, (o1, o2) in enumerate(offsets):
if sum(intersection[o1: o2]) > 0:
target_idx.append(i)* O4 E [/ Y; L
target_start = target_idx[0]8 m/ ^) ?+ Q2 J
target_end = target_idx[-1]
sentiment_map = {
'positive': 3893,3 |# j8 Q! `5 x! r* r8 v/ B
'negative': 4997,2 w1 \) @+ h4 I# L! E& L$ y
'neutral': 8699,- S5 Q' d, P9 _ j$ b
}" {6 a- G3 \0 k9 @! M" A1 M
1 x% i" _$ a; q/ e( E
# 将情感标签和原始的语句的词向量组合在一起组成我们新的数据
input_ids = [101] + [sentiment_map[sentiment]] + [102] + input_ids_orig + [102]
input_type_ids = [0] * (len(input_ids_orig) + 4)
attention_mask = [1] * (len(input_ids_orig) + 4)
offsets = [(0, 0), (0, 0), (0, 0)] + offsets + [(0, 0)]
target_start += 36 Z9 b; C& g4 u
target_end += 3, b: `! r: I6 i& E2 L7 j( H, ]* n
# 计算需要paddning的长度, BERT是以固定长度进行输入的,因此对于不足的我们需要做pandding
padding_length = MAX_SEQUENCE_LENGTH - len(input_ids)
if padding_length > 0:6 _' n% b* U U
input_ids = input_ids + ([0] * padding_length)
attention_mask = attention_mask + ([0] * padding_length)
input_type_ids = input_type_ids + ([0] * padding_length)' D/ k c, O* y1 [' j& m
offsets = offsets + ([(0, 0)] * padding_length)( Z2 G8 u. I8 ~1 [
elif padding_length . U4 [. U! E2 v9 C% {+ z0 a w
定义数据加载器7 E) ~/ x7 h- ~2 j8 g( u/ _
class TweetDataset(tf.data.Dataset):
; z* o) T6 t# \, X5 Q
outputTypes = (( d4 f0 l3 r% w& r! M
tf.dtypes.int32, tf.dtypes.int32, tf.dtypes.int32, 2 v' d Y8 d7 E8 ]
tf.dtypes.int32, tf.dtypes.float32, tf.dtypes.float32,8 ?9 D" ^3 s; W" ^. @7 ], S- g! J
tf.dtypes.string, tf.dtypes.string, tf.dtypes.string,
)
9 I' V, p5 b+ y: [1 @/ n9 s7 ]
outputShapes = (4 q$ T6 f8 y& O; b' b' n
(128,), (128,), (128,), 9 v4 e B f3 k9 O
(128, 2), (), (),
(), (), (),8 E2 j$ X8 t" b$ j" E; m) l
)
def _generator(tweet, selected_text, sentiment):1 j# T C& F1 Q0 K3 z* }
for tw, st, se in zip(tweet, selected_text, sentiment):: P5 W! K% O: ^
yield preprocess(tw, st, se)
def __new__(cls, tweet, selected_text, sentiment):. _1 [3 }2 Z0 P
return tf.data.Dataset.from_generator(
cls._generator,
output_types=cls.outputTypes,! K& V; J; H* s7 `/ G
output_shapes=cls.outputShapes,* p2 r y. g: y' ` K: k
args=(tweet, selected_text, sentiment)
)
# R. N# p& w- e
@staticmethod
def create(dataframe, batch_size, shuffle_buffer_size=-1):5 A8 l- f, v7 E/ @0 b0 A) R+ m
dataset = TweetDataset(
dataframe.text.values, ( Z8 o `2 P1 b! l: X. ? ^
dataframe.selected_text.values, $ [3 Z: Q$ f" {& C8 Y7 D8 K$ n
dataframe.sentiment.values
)% n- F# C" s7 U7 y. K
dataset = dataset.cache()& l. V6 Q. A1 W8 ?, p( _
if shuffle_buffer_size != -1:: H: T2 [! @& x8 m; h7 p
dataset = dataset.shuffle(shuffle_buffer_size)+ [# P9 U: ^. ^& t# V. S* l
dataset = dataset.batch(batch_size)
dataset = dataset.prefetch(tf.data.experimental.AUTOTUNE)
return dataset
定义模型4 d. q! ^; r0 D# V0 z! V+ j
我们使用BERT模型来进行这次竞赛,这里对BERT模型做一些简单的介绍。/ j/ M" q6 H7 I. l5 r
BERT的全称是Bidirectional Encoder Representation from Transformers,即双向Transformer的Encoder,因为decoder是不能获要预测的信息的。
模型的主要创新点都在pre-train方法上,即用了Masked LM和Next Sentence Prediction两种方法分别捕捉词语和句子级别representation。
BERT主要特点如下:
使用了Transformer作为算法的主要框架,Trabsformer能更彻底的捕捉语句中的双向关系;: l7 t7 ~- @: ^& G
5 p; y/ p! c5 ~& f8 l. Z) r
使用了Mask Language Model 和 Next Sentence Prediction的多任务训练目标;2 Q$ ^# P0 y9 q( q
3 s. t3 ~" \% i- M$ Z0 c1 ^* y
使用更强大的机器训练更大规模的数据,Google开源了BERT模型,我们可以直接使用BERT作为Word2Vec的转换矩阵并高效的将其应用到自己的任务中。
. s* Q/ u9 `7 S$ e
BERT的本质是在海量的语料基础上,运行自监督学习方法让单词学习得到一个较好的特征表示。( W' j, ^' w. T1 j5 C. K/ | n- D
在之后特定任务中,可以直接使用BERT的特征表示作为该任务的词嵌入特征。所以BERT提供的是一个供其它任务迁移学习的模型,该模型可以根据任务微调或者固定之后作为特征提取器。$ L: F7 I3 s' r$ B: ^
在竞赛中,我们定义了一个BertModel类,里面使用TFBertPreTrainedModel来进行推理。; T4 L( G% n$ h. G/ {1 m
BERT的输出我们保存在hidden_states中,然后将这个得到的hidden_states结果在加入到Dense Layer,最后输出我们需要提取的表示情感的文字的起始位置跟结束位置。
这两个位置信息就是我们需要从原文中提取的词向量的位置。! X6 z ?5 Z7 r% J
class BertModel(TFBertPreTrainedModel):, J! J+ ^8 P" W/ T0 ^% b
V, I4 y- r; L/ J4 ]/ _& a' x
# drop out rate, 防止过拟合
dr = 0.13 w# m1 V5 ~# }: Z7 Y; o
# hidden state数量
hs = 2# }1 g; m- r N) |- |
def __init__(self, config, *inputs, **kwargs):1 Y1 G9 w6 o( j. q0 t# B# y$ _9 v
super().__init__(config, *inputs, **kwargs)
' G4 |9 g1 @" Q! {/ S
self.bert = TFBertMainLayer(config, name="bert")3 v5 p" @$ `! _
self.concat = L.Concatenate() l y) Y' ?- n
self.dropout = L.Dropout(self.dr)
self.qa_outputs = L.Dense(5 x/ L8 n6 z# R5 A8 }
config.num_labels, - Y7 b/ ?0 n. g4 B) Q7 V3 q+ M
kernel_initializer=TruncatedNormal(stddev=config.initializer_range),# ]( j% k5 @5 ^. _; t8 `; |' K" n
dtype='float32',
name="qa_outputs")
0 A1 Z8 ^8 x5 e+ L4 |% `9 X9 k
@tf.function( j' b2 v i% ]& X [0 {
def call(self, inputs, **kwargs): J3 S! r0 ?+ n
_, _, hidden_states = self.bert(inputs, **kwargs)
m0 {; O& V. M/ B9 W1 _
hidden_states = self.concat([
hidden_states[-i] for i in range(1, self.hs+1)& ?) W# M1 w/ p+ Y- l- V2 J$ o
])
hidden_states = self.dropout(hidden_states, training=kwargs.get("training", False))+ |& M) @9 I" C' [; q
logits = self.qa_outputs(hidden_states)( y) w- R4 j6 F9 E$ [+ K
start_logits, end_logits = tf.split(logits, 2, axis=-1)
start_logits = tf.squeeze(start_logits, axis=-1)' B% k; E, i. I8 d; _8 E3 U
end_logits = tf.squeeze(end_logits, axis=-1), N! x8 R! @8 P) ]; k5 r' e
% t4 x0 y5 a6 v4 B% }) w0 _( S
return start_logits, end_logits( V% G# K, g, v* G* g
定义训练函数
def train(model, dataset, loss_fn, optimizer):0 Q, E8 ]; y) Y, A' a$ s
@tf.function
def train_step(model, inputs, y_true, loss_fn, optimizer):
with tf.GradientTape() as tape:# { {: H5 z( V! N0 }) P
y_pred = model(inputs, training=True)
loss = loss_fn(y_true[0], y_pred[0])$ D+ a( }) _8 E$ ^* W) p+ Q9 K' ~: z
loss += loss_fn(y_true[1], y_pred[1])
scaled_loss = optimizer.get_scaled_loss(loss)
, r+ g3 [8 D. J" u$ q! R& u
scaled_gradients = tape.gradient(scaled_loss, model.trainable_variables)
gradients = optimizer.get_unscaled_gradients(scaled_gradients) |" T% e$ {; W$ ?
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
return loss, y_pred$ f! O6 g+ B6 y4 Q8 b: p' k
epoch_loss = 0.
for batch_num, sample in enumerate(dataset):
loss, y_pred = train_step(model, sample[:3], sample[4:6], loss_fn, optimizer)
epoch_loss += loss
print(
f"training ... batch {batch_num+1:03d} : "; J( u$ b+ A K) C6 X4 m
f"train loss {epoch_loss/(batch_num+1):.3f} ",
end='\r'): a: O! G; Z8 _" E. M e+ U# k
定义预制函数9 M4 b E: z" ?' T
def predict(model, dataset, loss_fn, optimizer):/ |2 C, N |* }) W! i" G
@tf.function
def predict_step(model, inputs):
return model(inputs)
def to_numpy(*args):
out = []$ T2 q- c/ I9 K. w
for arg in args:, t5 d! }7 P% c% W6 Z8 X- r
if arg.dtype == tf.string:
arg = [s.decode('utf-8') for s in arg.numpy()]7 M1 @ ^% i1 D2 P. F$ p8 U; r
out.append(arg)& ^/ F9 S1 I9 ]9 w" q
else:
arg = arg.numpy()- [5 I8 H: b' M6 q' e, X3 A
out.append(arg)
return out6 i, a, z) n( w+ S- z5 v, x
3 ~! [' ^ p" C- {6 s: \' ?& s! e
offset = tf.zeros([0, 128, 2], dtype=tf.dtypes.int32)
text = tf.zeros([0,], dtype=tf.dtypes.string)
selected_text = tf.zeros([0,], dtype=tf.dtypes.string)8 \6 }, Z! j1 M
sentiment = tf.zeros([0,], dtype=tf.dtypes.string)
pred_start = tf.zeros([0, 128], dtype=tf.dtypes.float32)
pred_end = tf.zeros([0, 128], dtype=tf.dtypes.float32)
for batch_num, sample in enumerate(dataset):1 z5 [0 E" Q& p" Z/ J" w; G
print(f"predicting ... batch {batch_num+1:03d}"+" "*20, end='\r')
! e, v9 V' h* |8 c1 E
y_pred = predict_step(model, sample[:3])
# add batch to accumulators
pred_start = tf.concat((pred_start, y_pred[0]), axis=0)
pred_end = tf.concat((pred_end, y_pred[1]), axis=0), S5 @& r5 {- V$ _5 |7 Z
offset = tf.concat((offset, sample[3]), axis=0)# @/ ]0 ~8 s% p1 p+ U/ D. H/ D( C
text = tf.concat((text, sample[6]), axis=0)
selected_text = tf.concat((selected_text, sample[7]), axis=0) \" w) E# X" Z1 ~1 z
sentiment = tf.concat((sentiment, sample[8]), axis=0)& K1 P" K1 c( Y# ?1 q- L' o
x. c6 ?" |0 `* S. h& g
pred_start, pred_end, text, selected_text, sentiment, offset = \( e7 F" \) l9 X0 Z4 P; `# |
to_numpy(pred_start, pred_end, text, selected_text, sentiment, offset)
return pred_start, pred_end, text, selected_text, sentiment, offset
判断函数
这个竞赛采用单词级Jaccard系数,计算公式如下3 j/ {' D0 e1 O, g. ?- e
, n6 n3 E% k+ Y/ P+ t4 u
Jaccard系数计算的是你预测的单词在数据集中的个数,
def jaccard(str1, str2):
a = set(str1.lower().split())/ R. C' \# Z; ? ~3 @
b = set(str2.lower().split())8 g$ `$ n4 Q' p2 e4 S$ p: h+ y
c = a.intersection(b)
return float(len(c)) / (len(a) + len(b) - len(c))7 @, g Q# l/ l! u l. I
定义预测结果解码函数- U& w' @3 n3 F! Z6 O
解码函数通过模型预测拿到的start和end的index位置信息,然后和之前拿到的词向量在样本句子中的位置进行比较,将这个区间内的所有的单词都提取出来作为我们的预测结果。
w0 c, I, _, g v7 E
def decode_prediction(pred_start, pred_end, text, offset, sentiment):$ D: Z, s5 z) `: ~6 l S8 Y
' F+ I5 U1 U% Z" ?( ^
def decode(pred_start, pred_end, text, offset):
decoded_text = ""
for i in range(pred_start, pred_end+1):# |! \9 H1 V) n
decoded_text += text[offset[0]:offset[1]]
if (i+1) idx_end:; H0 f! L% @( p/ _+ g; q% D$ c
idx_end = idx_start
decoded_text = str(decode(idx_start, idx_end, text, offset))
if len(decoded_text) == 0:* B( Z0 D' L; }
decoded_text = text
decoded_predictions.append(decoded_text)% g( G6 O: m# z2 U2 y* f. ?# z
, Y* \ W7 S4 y# i1 G: Q
return decoded_predictions
开始训练) \5 N3 f" ], Q9 V
将训练数据分成5个folds,每个folds训练5个epoch,使用adam优化器,learning rate设置成3e-5,batch size使用32。
- P. y3 [: m3 K- C& _
num_folds = 5. g1 k# {; s3 s8 F e
num_epochs = 5
batch_size = 32
learning_rate = 3e-5
optimizer = tf.keras.optimizers.Adam(learning_rate)1 V8 |2 f) e- e4 \
optimizer = tf.keras.mixed_precision.experimental.LossScaleOptimizer(7 M/ N. Y0 V5 W- e' r# ]
optimizer, 'dynamic'), D1 s' ? R# g
config = BertConfig(output_hidden_states=True, num_labels=2)
model = BertModel.from_pretrained(PATH, config=config)0 N, s x4 p, b+ V
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
kfold = model_selection.KFold(/ C5 k( E% `- _' X. ^8 o
n_splits=num_folds, shuffle=True, random_state=42)
test_preds_start = np.zeros((len(test_df), 128), dtype=np.float32)
test_preds_end = np.zeros((len(test_df), 128), dtype=np.float32)
for fold_num, (train_idx, valid_idx) in enumerate(kfold.split(train_df.text)):! D$ v( ]3 @% W5 o2 b
print("\nfold %02d" % (fold_num+1))4 g7 U$ t4 V6 \/ N9 [* q
# 创建train, valid, test数据集
train_dataset = TweetDataset.create(
train_df.iloc[train_idx], batch_size, shuffle_buffer_size=2048)
valid_dataset = TweetDataset.create(4 x/ N* M2 ?4 h. F
train_df.iloc[valid_idx], batch_size, shuffle_buffer_size=-1)
test_dataset = TweetDataset.create(; ]2 K2 W4 L4 B( K
test_df, batch_size, shuffle_buffer_size=-1)
best_score = float('-inf')
for epoch_num in range(num_epochs):
print("\nepoch %03d" % (epoch_num+1))
train(model, train_dataset, loss_fn, optimizer)
% _3 U% }: H/ A; r
pred_start, pred_end, text, selected_text, sentiment, offset = \ ]4 T e$ |) Q9 r
predict(model, valid_dataset, loss_fn, optimizer)
& d- N5 ?5 }7 K- u/ a
selected_text_pred = decode_prediction(0 S2 k" [) R: W# r+ M' K' ~; x
pred_start, pred_end, text, offset, sentiment) x8 e: {$ n( M
jaccards = []% E5 t- k% I' q2 B: Q
for i in range(len(selected_text)):
jaccards.append(( a- l0 W( m8 {! U
jaccard(selected_text, selected_text_pred))9 Y" O) ^. ^) |* c; d. n
" v+ {! ]+ d4 `+ \* S* p
score = np.mean(jaccards)
print(f"valid jaccard epoch {epoch_num+1:03d}: {score}"+" "*15)
if score > best_score:
best_score = score, L* O- R7 V& j# f5 ?1 w8 u& {
# predict test set
test_pred_start, test_pred_end, test_text, _, test_sentiment, test_offset = \
predict(model, test_dataset, loss_fn, optimizer)
% I1 {8 r& j- L' ]: x* ?
test_preds_start += test_pred_start * 0.2
test_preds_end += test_pred_end * 0.29 i9 i/ p' j) @* n# x
# 重置模型,避免OOM
session = tf.compat.v1.get_default_session()
graph = tf.compat.v1.get_default_graph()! N; L' a, i3 T8 H0 U8 o# `
del session, graph, model
model = BertModel.from_pretrained(PATH, config=config)
预测测试数据,并生成提交文件
selected_text_pred = decode_prediction(% z2 l8 T( L) f, t V
test_preds_start, test_preds_end, test_text, test_offset, test_sentiment)" s; l0 ~5 m0 ]$ y" r8 U
def f(selected):
return " ".join(set(selected.lower().split()))9 f* {4 r8 p& ?( ~' e, @
submission_df.loc[:, 'selected_text'] = selected_text_pred
submission_df['selected_text'] = submission_df['selected_text'].map(f)& x, `3 H& `- v4 @
submission_df.to_csv("submission.csv", index=False)
这个方案在提交的时候在553个队伍中排名153位, 分数为0.68。
成为第一个吐槽的人