Answers.Body: <p>My answer assumes your fine-tuning architecture simply stacks a single fully-connected layer on top of the BERT <code>[CLS]</code> output, as in Figure 4b of the <a href="https://arxiv.org/pdf/1810.04805.pdf" rel="nofollow noreferrer">BERT paper</a>.</p>
  <p>Generally, when working with mixed data such as continuous and categorical features, the first step is to simply concatenate all the inputs into one long vector. In your case, you would concatenate a one-hot encoding of speaker ID to the BERT [CLS] output for each example. If you're using tensorflow, you'll need to use the functional API to create a multiple input model, as I outline below.</p>
  <pre><code>import tensorflow.keras as keras
  
  # bert_input is the wordpiece tokenized input text
  # BERT is the BERT model, such as a tensorflow_hub.KerasLayer with a pretrained model from tfhub.dev
  bert_out = BERT(bert_input)
  
  # spkr_input is the one-hot speaker ID encoding.
  spkr_input = keras.Input(shape=(num_speakers,), name='spkr_input')
  dense_input = keras.layers.concatenate([bert_out, spkr_input])
  scores = keras.layers.Dense(num_classes, activation='softmax')(dense_input)
  model = keras.Model(inputs=[bert_input , spkr_input], outputs=[scores])
  </code></pre>
  <p>You could also try feeding the one-hot encoded speaker ID vectors through a separate fully-connected network first to obtain a continuous representation (i.e. a speaker embedding) and then concatenate that to the BERT [CLS] output and feed the result into your classification layer. Modifying my example above,</p>
  <pre><code>spkr_emb = keras.layers.Dense(spkr_emb_size, activation=&quot;relu&quot;)(spkr_input)
  dense_input = keras.layers.concatenate([bert_out, spkr_emb])
  </code></pre>
  <p>You can find a more detailed guide to mixed input models with Keras at <a href="https://www.pyimagesearch.com/2019/02/04/keras-multiple-inputs-and-mixed-data/" rel="nofollow noreferrer">https://www.pyimagesearch.com/2019/02/04/keras-multiple-inputs-and-mixed-data/</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-18T04:54:59.467Z
Answers.Text: Thank you for the detailed explanation. I'll try your answer, and accept it (or ask a followup question).
Answers.UserDisplayName: k4200
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-17T16:36:18.267Z
Answers.LastActivityDate: 2020-06-17T16:36:18.267Z
Answers.UserDisplayName: primussucks
Body: <p>I want to classify my corporate chat messages into a few categories such as question, answer, and report. I used a fine-tuned BERT model, and the result wasn't bad. Now, I started thinking about ways to improve it, and a rough idea came up, but I don't know what to do it exactly.</p>
  
  <p>Currently, I simply put chat text into the model, but don't use the speaker's information (who said the text, the speaker's ID in our DB). The idea is if I can use the speaker's information, the model might better understand the text and classify it better.</p>
  
  <p>The question is, are there any examples or prior researches similar to what I want to achieve? I googled for a few hours, but couldn't find anything useful. (Maybe the keywords weren't good.)</p>
  
  <p>Any advice would be appreciated.</p>
  
Tags: <natural-language-processing><classification><bert>
Title: How to use speaker's information as well as text for fine-tuning BERT?
UserDisplayName: k4200
ThreadText: # How to use speaker's information as well as text for fine-tuning BERT?
  
  (Asked by: username_0 on 2020-06-16)
  
  I want to classify my corporate chat messages into a few categories such as question, answer, and report. I used a fine-tuned BERT model, and the result wasn't bad. Now, I started thinking about ways to improve it, and a rough idea came up, but I don't know what to do it exactly.
  
  Currently, I simply put chat text into the model, but don't use the speaker's information (who said the text, the speaker's ID in our DB). The idea is if I can use the speaker's information, the model might better understand the text and classify it better.
  
  The question is, are there any examples or prior researches similar to what I want to achieve? I googled for a few hours, but couldn't find anything useful. (Maybe the keywords weren't good.)
  
  Any advice would be appreciated.
  
  **Tags:** natural-language-processing, classification, bert
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-06-17. Score: 2
  
  My answer assumes your fine-tuning architecture simply stacks a single fully-connected layer on top of the BERT `[CLS]` output, as in Figure 4b of the [BERT paper](https://arxiv.org/pdf/1810.04805.pdf).
  
  Generally, when working with mixed data such as continuous and categorical features, the first step is to simply concatenate all the inputs into one long vector. In your case, you would concatenate a one-hot encoding of speaker ID to the BERT [CLS] output for each example. If you're using tensorflow, you'll need to use the functional API to create a multiple input model, as I outline below.
  
  ```
  import tensorflow.keras as keras
  
  # bert_input is the wordpiece tokenized input text
  # BERT is the BERT model, such as a tensorflow_hub.KerasLayer with a pretrained model from tfhub.dev
  bert_out = BERT(bert_input)
  
  # spkr_input is the one-hot speaker ID encoding.
  spkr_input = keras.Input(shape=(num_speakers,), name='spkr_input')
  dense_input = keras.layers.concatenate([bert_out, spkr_input])
  scores = keras.layers.Dense(num_classes, activation='softmax')(dense_input)
  model = keras.Model(inputs=[bert_input , spkr_input], outputs=[scores])
  
  ```
  
  You could also try feeding the one-hot encoded speaker ID vectors through a separate fully-connected network first to obtain a continuous representation (i.e. a speaker embedding) and then concatenate that to the BERT [CLS] output and feed the result into your classification layer. Modifying my example above,
  
  ```
  spkr_emb = keras.layers.Dense(spkr_emb_size, activation="relu")(spkr_input)
  dense_input = keras.layers.concatenate([bert_out, spkr_emb])
  
  ```
  
  You can find a more detailed guide to mixed input models with Keras at <https://www.pyimagesearch.com/2019/02/04/keras-multiple-inputs-and-mixed-data/>
  
  > **Comments:**
  > **username_0**: Thank you for the detailed explanation. I'll try your answer, and accept it (or ask a followup question).  
  
  ---
  
  