Ver código fonte

divide by 0 errors for 0 norms

Summary: Some functions don't check for 0 before dividing.

Reviewed By: EdouardGrave

Differential Revision: D5557786

fbshipit-source-id: 24863e0ff0f1a280749e2a08a8ca3700b6bdf163
Christian Puhrsch 8 anos atrás
pai
commit
6b74dfc399
1 arquivos alterados com 14 adições e 7 exclusões
  1. 14 7
      src/fasttext.cc

+ 14 - 7
src/fasttext.cc

@@ -376,11 +376,16 @@ void FastText::sentenceVectors() {
     int32_t count = 0;
     while(iss >> word) {
       getVector(vec, word);
-      vec.mul(1.0 / vec.norm());
-      svec.addVector(vec);
-      count++;
+      real norm = vec.norm();
+      if (norm > 0) {
+        vec.mul(1.0 / norm);
+        svec.addVector(vec);
+        count++;
+      }
+    }
+    if (count > 0) {
+      svec.mul(1.0 / count);
     }
-    svec.mul(1.0 / count);
     std::cout << sentence << " " << svec << std::endl;
   }
 }
@@ -430,14 +435,16 @@ void FastText::printSentenceVectors() {
 void FastText::precomputeWordVectors(Matrix& wordVectors) {
   Vector vec(args_->dim);
   wordVectors.zero();
-  std::cout << "Pre-computing word vectors...";
+  std::cerr << "Pre-computing word vectors...";
   for (int32_t i = 0; i < dict_->nwords(); i++) {
     std::string word = dict_->getWord(i);
     getVector(vec, word);
     real norm = vec.norm();
-    wordVectors.addRow(vec, i, 1.0 / norm);
+    if (norm > 0) {
+      wordVectors.addRow(vec, i, 1.0 / norm);
+    }
   }
-  std::cout << " done." << std::endl;
+  std::cerr << " done." << std::endl;
 }
 
 void FastText::findNN(const Matrix& wordVectors, const Vector& queryVec,