Skip to content

Commit

Permalink
chore: revert 824abe6
Browse files Browse the repository at this point in the history
  • Loading branch information
Anush008 authored Sep 19, 2023
1 parent 544afcb commit 472b119
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/fastembed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,21 @@ export class FlagEmbedding extends Embedding {
output.last_hidden_state.dims as number[]
);

const embeddings = lastHiddenState.map((sentence) => sentence[0]);
const embeddings = lastHiddenState.map((layer, layerIdx) => {
const weightedSum = layer.reduce((acc, tokenEmbedding, idx) => {
const attentionWeight = maskArray[layerIdx][idx];
return acc.map(
(val, i) => val + tokenEmbedding[i] * Number(attentionWeight)
);
}, new Array(layer[0].length).fill(0));

const inputMaskSum = maskArray[layerIdx].reduce(
(acc, attentionWeight) => acc + Number(attentionWeight),
0
);

return weightedSum.map((val) => val / (inputMaskSum + 1e-9));
});

yield embeddings.map(normalize);
}
Expand Down

0 comments on commit 472b119

Please sign in to comment.