Fix error with casting negative numbers to size_t.

This commit is contained in:
Ryan Curtin
2016-06-28 18:58:22 -04:00
parent 8e740b02eb
commit e6bc4b4170
+14 -8
View File
@@ -166,7 +166,8 @@ void LSHSearch<SortPolicy>::Train(const arma::mat& referenceSet,
}
// We will store the second hash vectors in this matrix; the second hash
// vector for table i will be held in row i.
// vector for table i will be held in row i. We have to use int and not
// size_t, otherwise negative numbers are cast to 0.
arma::Mat<size_t> secondHashVectors(numTables, referenceSet.n_cols);
for (size_t i = 0; i < numTables; i++)
@@ -189,15 +190,20 @@ void LSHSearch<SortPolicy>::Train(const arma::mat& referenceSet,
hashMat /= hashWidth;
// Step V: Putting the points in the 'secondHashTable' by hashing the key.
// Now we hash every key, point ID to its corresponding bucket.
secondHashVectors.row(i) = arma::conv_to<arma::Row<size_t>>::from(
secondHashWeights.t() * arma::floor(hashMat));
// Now we hash every key, point ID to its corresponding bucket. We must
// also normalize the hashes to the range [0, secondHashSize).
arma::rowvec unmodVector = secondHashWeights.t() * arma::floor(hashMat);
for (size_t j = 0; j < secondHashVectors.n_cols; ++j)
{
double shs = (double) secondHashSize; // Convenience cast.
if (unmodVector[j] >= 0.0)
secondHashVectors[j] = size_t(fmod(unmodVector[j], shs));
else
secondHashVectors[j] = secondHashSize -
size_t(fmod(-unmodVector[j], shs));
}
}
// Normalize hashes (take modulus with secondHashSize).
secondHashVectors.transform([secondHashSize](size_t val)
{ return val % secondHashSize; });
// Now, using the hash vectors for each table, count the number of rows we
// have in the second hash table.
arma::Row<size_t> secondHashBinCounts(secondHashSize, arma::fill::zeros);