【问题标题】:How to use bert layer for Multiple instance learning using TimeDistributed Layer?如何使用 TimeDistributed Layer 使用 bert 层进行多实例学习?
【发布时间】:2020-12-22 15:16:12
【问题描述】:

我想使用 Bert 执行多实例学习。一袋实例包含 40 个句子。每个句子都应该输出一个标签,最终的标签应该是所有标签的平均值。

我曾尝试使用 tensorflow_hub 的 bert 层。但我不知道如何将它与 TimeDistributed 一起使用。

bert_layer = hub.KerasLayer("https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/1",trainable=True)

pooled_output, sequence_output = bert_layer([input_word_ids, input_mask, segment_ids])

任何建议/解决方法将不胜感激

【问题讨论】:

    标签: tensorflow keras bert-language-model


    【解决方案1】:

    免责声明:我不是专家,因此可能有一些问题需要弄清楚,但它应该给你一个提示。 除了 BERT 编码器,你应该对你的文本进行一些预处理:

    bert_preprocess = hub.KerasLayer("https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/3")
    bert_encoder = hub.KerasLayer("https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/4", trainable=True)  
    # please keep in mind that the encoder is updated
    
    sentence_input = keras.Input(shape=(), dtype=tf.string, name='sentences')
    preprocessed = bert_preprocess(sentence_input)
    encoded = bert_encoder(preprocessed)
    outputs = encoded['pooled_output']  # this returns only single token for each sentence
    bert_model = Model(inputs=sentence_input, outputs=outputs)
    
    x = TimeDistributed(bert_model)(your_inputs)
    

    在时间分布层之后,您应该添加一个平均池化层(请记住,x 的维度取决于 BERT 模型的大小,我猜在您的情况下应该是 (40,786)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-17
      • 2017-02-10
      • 1970-01-01
      • 1970-01-01
      • 2021-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多