Commit 29fa49c8 authored by Davis King's avatar Davis King

Added a missing check for division by zero to these classes. If someone added

the zero vector to them as the first training example a division by zero could result.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403282
parent 938e41a9
...@@ -367,15 +367,24 @@ namespace dlib ...@@ -367,15 +367,24 @@ namespace dlib
const scalar_type kx = kernel(x,x); const scalar_type kx = kernel(x,x);
if (alpha.size() == 0) if (alpha.size() == 0)
{ {
// set initial state since this is the first training example we have seen // just ignore this sample if it is the zero vector (or really close to being zero)
if (std::abs(kx) > std::numeric_limits<scalar_type>::epsilon())
{
// set initial state since this is the first training example we have seen
K_inv.set_size(1,1); K_inv.set_size(1,1);
K_inv(0,0) = 1/kx; K_inv(0,0) = 1/kx;
K.set_size(1,1); K.set_size(1,1);
K(0,0) = kx; K(0,0) = kx;
alpha.push_back(xscale); alpha.push_back(xscale);
dictionary.push_back(x); dictionary.push_back(x);
}
else
{
// the distance from an empty kcentroid and the zero vector is zero by definition.
return 0;
}
} }
else else
{ {
......
...@@ -108,14 +108,18 @@ namespace dlib ...@@ -108,14 +108,18 @@ namespace dlib
const scalar_type kx = kernel(x,x); const scalar_type kx = kernel(x,x);
if (dictionary.size() == 0) if (dictionary.size() == 0)
{ {
// set initial state since this is the first sample we have seen // just ignore this sample if it is the zero vector (or really close to being zero)
K_inv.set_size(1,1); if (std::abs(kx) > std::numeric_limits<scalar_type>::epsilon())
K_inv(0,0) = 1/kx; {
// set initial state since this is the first sample we have seen
K_inv.set_size(1,1);
K_inv(0,0) = 1/kx;
K.set_size(1,1); K.set_size(1,1);
K(0,0) = kx; K(0,0) = kx;
dictionary.push_back(x); dictionary.push_back(x);
}
} }
else else
{ {
......
Markdown is supported
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