Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KMeans Sparse Init Update #2796

Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions cpp/daal/src/algorithms/kmeans/kmeans_plusplus_init_impl.i
Original file line number Diff line number Diff line change
Expand Up @@ -292,15 +292,22 @@ public:
const algorithmFPType * const pLastAddedCenter, const algorithmFPType * const aWeights,
const algorithmFPType * const pDistSqBest)
{
algorithmFPType sumOfDist2 = algorithmFPType(0);
size_t csrCursor = 0u;
algorithmFPType sumOfDist2 = algorithmFPType(0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the description to updateMinDistForITrials function.
I understand that it was not there before, but I hope that by adding couple of comments at a time we can make oneDAL's code more readable.

size_t csrCursor = 0u;
algorithmFPType pLastAddedCenterSumSq = algorithmFPType(0.);
for (size_t iCol = 0u; iCol < dim; iCol++)
{
pLastAddedCenterSumSq += pLastAddedCenter[iCol] * pLastAddedCenter[iCol];
}

for (size_t iRow = 0u; iRow < nRowsToProcess; iRow++)
{
algorithmFPType dist2 = algorithmFPType(0);
algorithmFPType dist2 = pLastAddedCenterSumSq;
const size_t nValues = rowIdx[iRow + 1] - rowIdx[iRow];
for (size_t i = 0u; i < nValues; i++, csrCursor++)
{
dist2 += (pData[csrCursor] - pLastAddedCenter[colIdx[csrCursor] - 1]) * (pData[csrCursor] - pLastAddedCenter[colIdx[csrCursor] - 1]);
dist2 += pData[csrCursor] * pData[csrCursor];
dist2 -= 2 * pData[csrCursor] * pLastAddedCenter[colIdx[csrCursor] - 1];
}
if (aWeights)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*******************************************************************************/

#include <cmath>

#include <daal/src/algorithms/kmeans/kmeans_init_kernel.h>

#include "oneapi/dal/algo/kmeans_init/backend/cpu/compute_kernel.hpp"
Expand Down Expand Up @@ -43,9 +45,15 @@ static compute_result<Task> call_daal_kernel(const context_cpu& ctx,
const std::int64_t column_count = data.get_column_count();
const std::int64_t cluster_count = desc.get_cluster_count();

const std::int64_t init_trial_count = desc.get_local_trials_count();
const auto additional = std::log(cluster_count);
const auto proposed = 2 + std::int64_t(additional);
const std::int64_t trial_count = (init_trial_count == -1) ? proposed : init_trial_count;

daal_kmeans_init::Parameter par(dal::detail::integral_cast<std::size_t>(cluster_count),
0,
dal::detail::integral_cast<std::size_t>(desc.get_seed()));
par.nTrials = trial_count;

const auto daal_data = interop::convert_to_daal_table<Float>(data);
const std::size_t len_input = 1;
Expand Down