extract_features句子嵌入BERT

我正在使用此代码来获取数据集中句子的嵌入(我正在使用预先训练的模型)。

`python extract_features.py \
  --input_file=/tmp/input.txt \
  --output_file=/tmp/output.jsonl \
  --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=32`

但是,有一个问题:有一种方法可以更快地获得嵌入?因为花了2000句花了6个小时。我的数据集包含20000个句子; 60小时对于colab来说太长了。 谢谢。

a786782040 回答:extract_features句子嵌入BERT

我解决了。 我将所有句子写在input.txt中,然后使用此代码:

import jsonlines
df_emb=pd.DataFrame()
with jsonlines.open('/content/tmp/output.jsonl') as f:
    for line in f.iter():
        s=line['features'][0]['layers'][0]['values']
        df_tmp=pd.DataFrame(s).T
        df_emb=df_emb.append(df_tmp,ignore_index=True)

之后,我将数据帧保存在csv文件中

本文链接:https://www.f2er.com/2330167.html

大家都在问