Skip to content

Commit

Permalink
fix incorrect zero comparison of floating point vars (#911)
Browse files Browse the repository at this point in the history
  • Loading branch information
MelLain committed Apr 26, 2018
1 parent 1f40e03 commit 74b81bc
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/artm/core/collection_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ CollectionParserInfo CollectionParser::ParseDocwordBagOfWordsUci(TokenMap* token
BOOST_THROW_EXCEPTION(ArgumentOutOfRangeException("wordID", token_id, ss.str()));
}

if (token_weight == 0.0f) {
if (isZero(token_weight)) {
token_weight_zero++;
continue;
}
Expand Down
8 changes: 8 additions & 0 deletions src/artm/core/helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,5 +244,13 @@ void Helpers::SaveMessage(const std::string& full_filename,
fout.close();
}

bool isZero(double value, double tol) {
return std::abs(value) < tol;
}

bool isZero(float value, float tol) {
return std::abs(value) < tol;
}

} // namespace core
} // namespace artm
4 changes: 4 additions & 0 deletions src/artm/core/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,9 @@ class Helpers {
const ::google::protobuf::Message& message);
};

bool isZero(double value, double tol = 1e-37);

bool isZero(float value, float tol = 1e-16f);

} // namespace core
} // namespace artm
8 changes: 4 additions & 4 deletions src/artm/core/processor_transaction_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,15 @@ void ProcessorTransactionHelpers::TransactionInferThetaAndUpdateNwtSparse(
}
}

float p_dx_val = 0;
float p_dx_val = 0.0f;
for (int k = 0; k < num_topics; ++k) {
p_dx_val += p_xt_local[k] * theta_ptr[k];
}
if (p_dx_val == 0) {
if (isZero(p_dx_val)) {
continue;
}

const float alpha = sparse_ndx.val()[i] / p_dx_val;
const double alpha = sparse_ndx.val()[i] / p_dx_val;
for (int k = 0; k < num_topics; ++k) {
ntd_ptr[k] += alpha * p_xt_local[k];
}
Expand Down Expand Up @@ -243,7 +243,7 @@ void ProcessorTransactionHelpers::TransactionInferThetaAndUpdateNwtSparse(
for (int i = sparse_nxd.row_ptr()[transaction_index]; i < sparse_nxd.row_ptr()[transaction_index + 1]; ++i) {
int d = sparse_nxd.col_ind()[i];
float p_xd_val = blas->sdot(num_topics, &p_xt_local[0], 1, &(*theta_matrix)(0, d), 1); // NOLINT
if (p_xd_val == 0) {
if (isZero(p_xd_val)) {
continue;
}

Expand Down
5 changes: 3 additions & 2 deletions src/artm/score/perplexity.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <sstream>

#include "artm/core/exceptions.h"
#include "artm/core/helpers.h"
#include "artm/core/protobuf_helpers.h"

#include "artm/score/perplexity.h"
Expand Down Expand Up @@ -145,7 +146,7 @@ void Perplexity::AppendScore(
}

float token_weight = tt_weight * item.token_weight(token_index);
if (token_weight == 0.0f) {
if (core::isZero(token_weight)) {
continue;
}

Expand All @@ -167,7 +168,7 @@ void Perplexity::AppendScore(
sum += theta[topic_index] * phi_values[topic_index];
}

if (sum == 0.0f) {
if (core::isZero(sum)) {
if (use_document_unigram_model) {
sum = token_weight / (use_tt ? normalizer_map[tt] : normalizer);
} else {
Expand Down
9 changes: 6 additions & 3 deletions src/bigartm/srcmain.cc
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
// Copyright 2018, Additive Regularization of Topic Models.

#include <stdlib.h>

#include <cstring>
#include <ctime>
#include <cmath>

#include <algorithm>
#include <chrono>
#include <ctime>
#include <fstream>
#include <future>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <set>
Expand Down Expand Up @@ -1338,7 +1341,7 @@ int execute(const artm_options& options, int argc, char* argv[]) {
}

master_config.add_class_id(class_id.first);
master_config.add_class_weight(class_id.second == 0.0f ? 1.0f : class_id.second);
master_config.add_class_weight(std::abs(class_id.second) < 1e-16 ? 1.0f : class_id.second);
}

master_config.set_opt_for_avx(!options.b_disable_avx_opt);
Expand Down

0 comments on commit 74b81bc

Please sign in to comment.