Github user srowen commented on a diff in the pull request:

    https://github.com/apache/spark/pull/10152#discussion_r50676720
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/mllib/feature/Word2Vec.scala ---
    @@ -556,6 +571,7 @@ class Word2VecModel private[spark] (
           .sortBy(- _._2)
           .take(num + 1)
           .tail
    +      .map(v => (if (vecNorm == 0) v else (v._1, v._2 / vecNorm)))
    --- End diff --
    
    I agree that we should define cosine similarity with a zero vector to be 0. 
In this case the results are pretty meaningless anyway, since the dot product 
was already 0 for everything, and so the top N are random. I'd say:
    
    ```
    .map { case (word, cosVec) => (word, if (vecNorm == 0.0) 0.0 else (cosVec / 
vecNorm)) }
    ```
    For more efficiency we could only apply this map in the corner case that 
`vecNorm` is 0.0; not sure if it's simpler but it's a little less work:
    
    ```
    val result = wordList....tail
    if (vecNorm != 0.0) [
      result = result.map { case (word, cosVec) => (word, cosVec / vecNorm) }
    }
    result.toArray
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to