tensorflow / tfjs

A WebGL accelerated JavaScript library for training and deploying ML models.
https://js.tensorflow.org
Apache License 2.0
18.51k stars 1.94k forks source link

Bad advice in README for the TFJS version of the Universal Sentence Encoder #5931

Closed ToonTalk closed 2 years ago

ToonTalk commented 2 years ago

Reading https://github.com/tensorflow/tfjs-models/tree/master/universal-sentence-encoder the sample code needs to compute the dot product of two tensors and it uses arraySync on the tensors, nested for loops, and JavaScript defined vector operations. Wouldn’t it be better practice to use tf.layers.dot? The code would be much more concise and presumably much faster. (If it is the case the sample code predated tf.layers.dot then shouldn’t it be updated?)

jasonmayes commented 2 years ago

@pyu10055 Would you be able to confirm why it was done this way? I believe you were involved in this model?

pyu10055 commented 2 years ago

@ToonTalk Thanks for reporting this, this should be computed on the gpu for efficiency.

I used tf.matMul instead:

  const scores = tf.matMul(embeddings['queryEmbedding'],
      embeddings['responseEmbedding'], false, true).dataSync();