mlapiDecomposition: Base abstract class for all decompositions

Description Usage Format Fields Methods Arguments Examples

Description

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.

Usage

1
2
3

Format

R6Class object.

Fields

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

Methods

$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)

Arguments

x

A matrix like object, should inherit from Matrix or matrix. Allowed classes should be defined in child classes.

y

NULL. Optional taget variable. Usually this should be NULL. There few cases when it could be used.

...

additional parameters with default values

x

A matrix like object, should inherit from Matrix or matrix. Allowed classes should be defined in child classes.

y

NULL. Optional taget variable. Usually this should be NULL. There few cases when it could be used.

...

additional parameters with default values

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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)

mlapi documentation built on May 2, 2019, 6:59 a.m.