Skip to content
Snippets Groups Projects
Commit 7be95e0f authored by Franck Dary's avatar Franck Dary
Browse files

Remove NaN and clamping of entropy

parent 5888ee1f
No related branches found
No related tags found
No related merge requests found
......@@ -8,6 +8,8 @@ float NeuralNetworkImpl::entropy(torch::Tensor probabilities)
util::myThrow("Invalid probabilities tensor");
probabilities = probabilities.unsqueeze(0);
return - torch::tensordot(probabilities, torch::log(torch::transpose(probabilities, 0, 1)), {0,1}, {0,1}).item<float>();
auto logProbs = torch::clamp(torch::log(torch::transpose(probabilities, 0, 1)), -10.0, 10.0);
logProbs.index({torch::isnan(logProbs)}) = 0.0;
return - torch::tensordot(probabilities, logProbs, {0,1}, {0,1}).item<float>();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment