summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurent Mazare <laurent.mazare@gmail.com>2023-07-05 17:06:40 +0100
committerGitHub <noreply@github.com>2023-07-05 17:06:40 +0100
commit1db0fbee0dd60c85b4c1429b32f231058d35d547 (patch)
tree09ad10d1b5eaa8fc428779bf6347a64e0474db34
parenta4a60a13facc064ff8a694aad63987c322890f64 (diff)
parent174e57d216993e7928d6e383c5229cffd73f625c (diff)
downloadcandle-1db0fbee0dd60c85b4c1429b32f231058d35d547.tar.gz
candle-1db0fbee0dd60c85b4c1429b32f231058d35d547.tar.bz2
candle-1db0fbee0dd60c85b4c1429b32f231058d35d547.zip
Merge pull request #80 from LaurentMazare/bert-avg-pool
Use avg pooling before the cosine similarity.
-rw-r--r--candle-examples/examples/bert/main.rs7
1 files changed, 4 insertions, 3 deletions
diff --git a/candle-examples/examples/bert/main.rs b/candle-examples/examples/bert/main.rs
index 8795faa9..4396326d 100644
--- a/candle-examples/examples/bert/main.rs
+++ b/candle-examples/examples/bert/main.rs
@@ -743,9 +743,10 @@ async fn main() -> Result<()> {
println!("running inference on batch {:?}", token_ids.shape());
let embeddings = model.forward(&token_ids, &token_type_ids)?;
println!("generated embeddings {:?}", embeddings.shape());
- // Take the embedding for the first token of each sentence.
- // TODO: mean or max pooling?
- let embeddings = embeddings.narrow(1, 0, 1)?.squeeze(1)?;
+ // Apply some avg-pooling by taking the mean embedding value for all tokens (including padding)
+ let (_n_sentence, n_tokens, _hidden_size) = embeddings.shape().r3()?;
+ let embeddings = (embeddings.sum(&[1])? / (n_tokens as f64))?.squeeze(1)?;
+ println!("pooled embeddings {:?}", embeddings.shape());
let mut similarities = vec![];
for i in 0..n_sentences {
let e_i = embeddings.get(i)?;