Skip to content

Commit

Permalink
[bug] Fix zero division error
Browse files Browse the repository at this point in the history
* add edge case handling in cosine_similarity function: in case of zero-valued tensor

Signed-off-by: skykongkong8 <[email protected]>
  • Loading branch information
skykongkong8 authored and jijoongmoon committed Aug 9, 2023
1 parent ff5503f commit 24d0d5b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
2 changes: 1 addition & 1 deletion nntrainer/layers/fc_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void FullyConnectedLayer::finalize(InitLayerContext &context) {
context.setEffDimFlagInputDimension(0, 0b1001);
context.setDynDimFlagInputDimension(0, 0b1000);

bool is_nchw = (getTensorType() == Tformat::NCHW);
bool is_nchw = (context.getFormat() == Tformat::NCHW);
/** set output dimensions */
auto const &in_dim = context.getInputDimensions()[0];
output_dims[0] = in_dim;
Expand Down
5 changes: 4 additions & 1 deletion test/include/nntrainer_test_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,9 @@ double cosine_similarity(Ta *A /*Predict*/, Tb *B /*Reference */,
denom_b += ref * ref;
}

if (sqrt(denom_a) == 0 && sqrt(denom_b) == 0)
return 1;

double cosine_sim = dot / (sqrt(denom_a) * sqrt(denom_b));
return cosine_sim;
}
Expand All @@ -279,7 +282,7 @@ float mse(Ta *A /* Predicted */, Tb *B /* Reference */, uint32_t size) {
Ta pred;
Tb ref;
float mse_error = 0;
for (int i = 0; i < size; i++) {
for (uint32_t i = 0; i < size; i++) {
pred = A[i];
ref = B[i];
float diff = pred - ref;
Expand Down

0 comments on commit 24d0d5b

Please sign in to comment.