2020年3月参加了“百度架构师手把手教深度学习”课程,在NLP的文本分类比赛中有一些心得体会:
1.使用预训练的模型结果肯定是好于自建网络,毕竟站在巨人的肩膀上,不过可能并不利于真正的学习掌握基础知识。
2.单个模型比不上模型融合或者结果的再投票,但是在现实应用场景中不大可能有这么大容量的环境支撑。
数据预处理
数据预处理部分,我花了不少精力
1.去异常数据
统一全角和半角,然后把一些爬取失败的,不可识别的字符去掉。不通的预处理模型,对中文、英文、其它字符有不同的处理方式,但是预先做个处理还是有必要的。
def DBC2SBC(ustring):
rstring =""
for uchar in ustring:
inside_code = ord(uchar)
if inside_code == 0x3000:
inside_code = 0x0020
else:
inside_code -= 0xfee0
if not (0x0021 <= inside_code and inside_code <= 0x7e):
rstring += uchar
continue
rstring += chr(inside_code)
return rstring
def formatText(temptext):
temptext=temptext.strip()
temptext=re.sub("�" ,"",temptext)#无效数据
temptext=re.sub("\x7f","",temptext)#
temptext=re.sub(" "," ",temptext)
temptext=DBC2SBC(temptext)#全角变半角
temptext=re.sub("\u3000"," ",temptext)
temptext=re.sub("・"," ",temptext)
temptext=re.sub("…"," ",temptext)
temptext=temptext.lower()
temptext=temptext.strip()
return temptext
2.去停
按字对训练集、测试集建立字典,然后排序。验证后,排在后面的也都是一些生僻字,这个字词我认为是没有训练意义的。
f_train=open('./Train.txt','r',encoding='utf-8').read()
for i,line in enumerate(f_train.split("\n")):
if len(line)>0:
content = line.split('\t')
temptext=content[2]
temptext=formatText(temptext)
for key in en_dic_key:
match=re.search(key,temptext)
if match:
train_dic[key]+=1
textl=list(temptext)
for c in textl:
if c not in train_dic:
train_dic[c]=1
else:
train_dic[c]+=1
all_dic=dict.fromkeys(train_dic.keys(),0)
3.语义强化
由于新闻标题文本很少,携带信息量也很少,我尝试做语义强化,把符号,数字做统一化处理。效果并不理想。
temptext=re.sub("【","["