Commit 66b09b73 authored by Franck Dary's avatar Franck Dary
Browse files

Ignoring values close to zero in entropy computation

parent 82afc4f3
......@@ -7,8 +7,15 @@ float NeuralNetworkImpl::entropy(torch::Tensor probabilities)
if (probabilities.dim() != 1)
util::myThrow("Invalid probabilities tensor");
probabilities = torch::clamp(probabilities.unsqueeze(0), 0.00000000001, 1.0);
float entropy = -torch::sum(probabilities * torch::log(probabilities)).item<float>();
float entropy = 0.0;
for (unsigned int i = 0; i < probabilities.size(0); i++)
{
if (probabilities[i].item<float>() > 0.01)
entropy -= (probabilities[i] * torch::log(probabilities[i])).item<float>();
}
if (entropy < 0.01)
entropy = 0.0;
return entropy;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment