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

Creating copy of components in PCA transform and inverse transform #4099

Merged
merged 2 commits into from
Jul 27, 2021
Merged
Changes from all 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
49 changes: 23 additions & 26 deletions cpp/src/pca/pca.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <raft/matrix/matrix.cuh>
#include <raft/stats/mean.cuh>
#include <raft/stats/mean_center.cuh>
#include <rmm/device_uvector.hpp>
#include <stats/cov.cuh>
#include <tsvd/tsvd.cuh>

Expand Down Expand Up @@ -209,26 +210,24 @@ void pcaInverseTransform(const raft::handle_t& handle,
ASSERT(prms.n_components > 0,
"Parameter n_components: number of components cannot be less than one");

std::size_t components_len = static_cast<std::size_t>(prms.n_cols * prms.n_components);
rmm::device_uvector<math_t> components_copy{components_len, stream};
raft::copy(components_copy.data(), components, prms.n_cols * prms.n_components, stream);

if (prms.whiten) {
math_t sqrt_n_samples = sqrt(prms.n_rows - 1);
math_t scalar = prms.n_rows - 1 > 0 ? math_t(1 / sqrt_n_samples) : 0;
raft::linalg::scalarMultiply(
components, components, scalar, prms.n_rows * prms.n_components, stream);
raft::linalg::scalarMultiply(components_copy.data(),
components_copy.data(),
scalar,
prms.n_cols * prms.n_components,
stream);
raft::matrix::matrixVectorBinaryMultSkipZero(
components, singular_vals, prms.n_rows, prms.n_components, true, true, stream);
components_copy.data(), singular_vals, prms.n_cols, prms.n_components, true, true, stream);
}

tsvdInverseTransform(handle, trans_input, components, input, prms, stream);
tsvdInverseTransform(handle, trans_input, components_copy.data(), input, prms, stream);
raft::stats::meanAdd(input, input, mu, prms.n_cols, prms.n_rows, false, true, stream);

if (prms.whiten) {
raft::matrix::matrixVectorBinaryDivSkipZero(
components, singular_vals, prms.n_rows, prms.n_components, true, true, stream);
math_t sqrt_n_samples = sqrt(prms.n_rows - 1);
math_t scalar = prms.n_rows - 1 > 0 ? math_t(1 / sqrt_n_samples) : 0;
raft::linalg::scalarMultiply(
components, components, scalar, prms.n_rows * prms.n_components, stream);
}
}

// TODO: implement pcaScore function
Expand Down Expand Up @@ -271,26 +270,24 @@ void pcaTransform(const raft::handle_t& handle,
ASSERT(prms.n_components > 0,
"Parameter n_components: number of components cannot be less than one");

std::size_t components_len = static_cast<std::size_t>(prms.n_cols * prms.n_components);
rmm::device_uvector<math_t> components_copy{components_len, stream};
raft::copy(components_copy.data(), components, prms.n_cols * prms.n_components, stream);

if (prms.whiten) {
math_t scalar = math_t(sqrt(prms.n_rows - 1));
raft::linalg::scalarMultiply(
components, components, scalar, prms.n_rows * prms.n_components, stream);
raft::linalg::scalarMultiply(components_copy.data(),
components_copy.data(),
scalar,
prms.n_cols * prms.n_components,
stream);
raft::matrix::matrixVectorBinaryDivSkipZero(
components, singular_vals, prms.n_rows, prms.n_components, true, true, stream);
components_copy.data(), singular_vals, prms.n_cols, prms.n_components, true, true, stream);
}

raft::stats::meanCenter(input, input, mu, prms.n_cols, prms.n_rows, false, true, stream);
tsvdTransform(handle, input, components, trans_input, prms, stream);
tsvdTransform(handle, input, components_copy.data(), trans_input, prms, stream);
raft::stats::meanAdd(input, input, mu, prms.n_cols, prms.n_rows, false, true, stream);

if (prms.whiten) {
raft::matrix::matrixVectorBinaryMultSkipZero(
components, singular_vals, prms.n_rows, prms.n_components, true, true, stream);
math_t sqrt_n_samples = sqrt(prms.n_rows - 1);
math_t scalar = prms.n_rows - 1 > 0 ? math_t(1 / sqrt_n_samples) : 0;
raft::linalg::scalarMultiply(
components, components, scalar, prms.n_rows * prms.n_components, stream);
}
}

}; // end namespace ML