
Last chance! 50% off unlimited learning
Sale ends in
Base class for all decompositions which are methods which can decompose matrix into
2 low-dimensional matrices x = f(A, B)
.
(Think of this Latent Dirichlet Allocation, Non-negative Matrix Factorization, etc).
It iherits from mlapiTransformation and additionally requires to implement components
member.
Base class for all decompositions which are methods which can decompose matrix into
2 low-dimensional matrices x = f(A, B)
incrementally.
It iherits from mlapiDecomposition and additionally requires
to implement partial_fit
method which can learn components
incrementally.
mlapiDecompositionmlapiDecompositionOnline
R6Class
object.
components
features embeddings. So if matrix is decomposed in a form x = f(A, B)
where
X = n\*m, A = n\*k, B = k\*m them B = components
components
features embeddings. So if matrix is decomposed in a form x = f(A, B)
where
X = n\*m, A = n\*k, B = k\*m them B = components
$fit_transform(x, y = NULL, ...)
$transform(x, ...)
Performs transformation of the new data (after model was trained)
$fit_transform(x, y = NULL, ...)
$partial_fit(x, y = NULL, ...)
$transform(x, ...)
Performs transformation of the new data (after model was trained)
A matrix like object, should inherit from Matrix
or matrix
.
Allowed classes should be defined in child classes.
NULL
. Optional taget variable. Usually this should be NULL
.
There few cases when it could be used.
additional parameters with default values
A matrix like object, should inherit from Matrix
or matrix
.
Allowed classes should be defined in child classes.
NULL
. Optional taget variable. Usually this should be NULL
.
There few cases when it could be used.
additional parameters with default values
# NOT RUN {
TruncatedSVD = R6::R6Class(
classname = "TruncatedSVD",
inherit = mlapi::mlapiDecomposition,
public = list(
initialize = function(rank = 10) {
private$rank = rank
super$set_internal_matrix_formats(dense = "matrix", sparse = NULL)
},
fit_transform = function(x, ...) {
x = super$check_convert_input(x)
private$n_features = ncol(x)
svd_fit = svd(x, nu = private$rank, nv = private$rank, ...)
sing_values = svd_fit$d[seq_len(private$rank)]
result = svd_fit$u %*% diag(x = sqrt(sing_values))
private$components_ = t(svd_fit$v %*% diag(x = sqrt(sing_values)))
rm(svd_fit)
rownames(result) = rownames(x)
colnames(private$components_) = colnames(x)
private$fitted = TRUE
invisible(result)
},
transform = function(x, ...) {
if (private$fitted) {
stopifnot(ncol(x) == ncol(private$components_))
lhs = tcrossprod(private$components_)
rhs = as.matrix(tcrossprod(private$components_, x))
t(solve(lhs, rhs))
}
else
stop("Fit the model first woth model$fit_transform()!")
}
),
private = list(
rank = NULL,
n_features = NULL,
fitted = NULL
)
)
set.seed(1)
model = TruncatedSVD$new(2)
x = matrix(sample(100 * 10, replace = TRUE), ncol = 10)
x_trunc = model$fit_transform(x)
dim(x_trunc)
x_trunc_2 = model$transform(x)
sum(x_trunc_2 - x_trunc)
#' check pipe-compatible S3 interface
x_trunc_2_s3 = transform(x, model)
identical(x_trunc_2, x_trunc_2_s3)
# }
Run the code above in your browser using DataLab