首页 > 其他 > 详细

BERT使用记录

时间:2019-11-22 16:42:51      阅读:138      评论:0      收藏:0      [点我收藏+]

使用 bert 生成词向量:

##### 运行此脚本 
export BERT_BASE_DIR = ./chinese_L-12_H-768_A-12 ## 模型地址
exprot Data_Dir = ./data

python bert-master/extract_features.py \
  --input_file=$Data_Dir/train_ch.txt \
  --output_file=$Data_dir/output.json \
  --vocab_file=$BERT_BASE_DIR/vocab.txt \
  --bert_config_file=$BERT_BASE_DIR/bert_config.json \
  --init_checkpoint=$BERT_BASE_DIR/bert_model.ckpt \
  --layers=-1,-2,-3,-4 \
  --max_seq_length=128 \
  --batch_size=8

结果文件是这种形式:

 {"linex_index": 0, "features":[{"token": "[CLS]", "layers": [{"index": -1, "values":[-0.919886, 0.656876, -0.58464654]}]}]}

解码代码:

import re
import json

src = ‘‘
tgt = ‘‘

def fun(file1,file2):
  with open(file1,‘r‘,encoding=‘utf-8‘) as fl1:
    with open(file2,‘w‘,encoding=‘utf-8‘) as fl2:
      k=0
      for line in fl1.readlines():
        k+=1
        line = json.loads(line)
        temp = line.get(‘features‘)
        temp = temp[1]
        temp = temp.get(‘layers‘)
        temp = temp[1]
        temp = temp.get("values")
        fl2.write(str(temp)+‘\n‘+‘\n‘) ## 好看一些
        if k%1000==0:
          print("Done"+‘ ‘+str(k))

fun(src,tgt)  

 

BERT使用记录

原文:https://www.cnblogs.com/hanouba/p/11912107.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!