【问题标题】:extract_features sentence embedding BERTextract_features 句子嵌入 BERT
【发布时间】:2020-05-18 08:26:09
【问题描述】:

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

`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 来说太长了。 谢谢。

【问题讨论】:

    标签: python google-colaboratory embedding bert-language-model


    【解决方案1】:

    我解决了。 我在 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 文件中

    【讨论】:

      猜你喜欢
      • 2020-01-29
      • 1970-01-01
      • 2021-11-29
      • 1970-01-01
      • 2020-04-07
      • 2020-12-07
      • 1970-01-01
      • 1970-01-01
      • 2020-07-01
      相关资源
      最近更新 更多