From 4cb447754239376bd7f6291b72bb74b67cb3a9f3 Mon Sep 17 00:00:00 2001 From: david-cortes Date: Sun, 10 Jan 2021 19:58:58 +0200 Subject: [PATCH 1/3] update to new Matrix syntax (#57) --- DESCRIPTION | 2 +- R/model_FTRL.R | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 80cbeb2..c3486e8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -42,7 +42,7 @@ LazyData: true ByteCompile: true Depends: R (>= 3.6.0), methods Imports: - Matrix (>= 1.2), + Matrix (>= 1.3), Rcpp (>= 0.11), data.table (>= 1.10.0), float (>= 0.2-2), diff --git a/R/model_FTRL.R b/R/model_FTRL.R index c22cb45..472acd2 100644 --- a/R/model_FTRL.R +++ b/R/model_FTRL.R @@ -11,8 +11,7 @@ #' x = sample(c(-1, 1), 1000 * 100, TRUE) #' odd = seq(1, 99, 2) #' x[i %in% which(y == 1) & j %in% odd] = 1 -#' m = sparseMatrix(i = i, j = j, x = x, dims = c(1000, 1000), giveCsparse = FALSE) -#' x = as(m, "RsparseMatrix") +#' x = sparseMatrix(i = i, j = j, x = x, dims = c(1000, 1000), repr="R") #' #' ftrl = FTRL$new(learning_rate = 0.01, learning_rate_decay = 0.1, #' lambda = 10, l1_ratio = 1, dropout = 0) @@ -21,7 +20,7 @@ #' w = ftrl$coef() #' head(w) #' sum(w != 0) -#' p = ftrl$predict(m) +#' p = ftrl$predict(x) #' @export FTRL = R6::R6Class( classname = "FTRL", From a887082b52f4fbe5d8b22a0b19c8d2d07e630d6e Mon Sep 17 00:00:00 2001 From: david-cortes Date: Mon, 11 Jan 2021 13:35:56 +0200 Subject: [PATCH 2/3] add convenience casting functions (#58) --- NAMESPACE | 3 + R/methods.R | 185 ++++++++++++++++++++++++++++++ README.md | 2 +- man/casting.Rd | 66 +++++++++++ tests/testthat/test-conversions.R | 162 ++++++++++++++++++++++++++ 5 files changed, 417 insertions(+), 1 deletion(-) create mode 100644 man/casting.Rd create mode 100644 tests/testthat/test-conversions.R diff --git a/NAMESPACE b/NAMESPACE index 7aeb833..c1957f3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -8,6 +8,9 @@ export(PureSVD) export(ScaleNormalize) export(WRMF) export(ap_k) +export(as.coo.matrix) +export(as.csc.matrix) +export(as.csr.matrix) export(detect_number_omp_threads) export(ndcg_k) export(soft_impute) diff --git a/R/methods.R b/R/methods.R index 6e41c51..1d66d54 100644 --- a/R/methods.R +++ b/R/methods.R @@ -302,3 +302,188 @@ check_dimensions_match = function(x, y, y_transposed = FALSE) { } # nocov end + +#' Conversions between matrix types +#' +#' @description Convenience functions for converting to different sparse matrix formats. +#' +#' @details The functions internally use as(x, "?sparseMatrix"), so they might work +#' with other object classes if they register a conversion method for `Matrix` base +#' types. +#' +#' When passed a vector, the functions `as.csr.matrix` and `as.coo.matrix` will +#' assume that it is a row vector, while `as.csc.matrix` will assume it's a column vector. +#' +#' @param x A matrix which is to be converted to a different format. +#' @param binary Whether the result should be a binary-only matrix (inheriting from +#' class `nsparseMatrix` - these don't have slot `x`). +#' Supported input types are:\itemize{ +#' \item Sparse matrices from `Matrix` package, in any format. +#' \item Sparse vector from `Matrix` (class `dsparseVector`). +#' \item Dense matrix from base R. +#' \item Dense vector from base R (classes `numeric` and `integer`). +#' \item Dense matrix or vector from package `float` (class `float32`). +#' \item `data.frame` and `data.table`. +#' } +#' +#' @return A sparse matrix, with format:\itemize{ +#' \item CSR (a.k.a. `RsparseMatrix`) when calling `as.csr.matrix` +#' (class `dgRMatrix` with `binary=FALSE`, class `ngRMatrix` with `binary=TRUE`). +#' \item CSC (a.k.a. `CsparseMatrix`) when calling `as.csc.matrix` +#' (class `dgCMatrix` with `binary=FALSE`, class `ngCMatrix` with `binary=TRUE`). +#' \item COO (a.k.a. `TsparseMatrix`) when calling `as.coo.matrix` +#' (class `dgTMatrix` with `binary=FALSE`, class `ngTMatrix` with `binary=TRUE`). +#' } +#' +#' @name casting +#' @examples +#' library(Matrix) +#' library(rsparse) +#' +#' m.coo = as(matrix(1:3), "TsparseMatrix") +#' as.csr.matrix(m.coo) +#' as.csr.matrix(1:3) # <- assumes it's a row vector +#' as.csc.matrix(1:3) # <- assumes it's a column vector +#' +#' library(float) +#' m.f32 = float::fl(matrix(1:10, nrow=5)) +#' as.csr.matrix(m.f32) +#' +#' library(data.table) +#' as.coo.matrix(data.table(col1=1:3)) +NULL + +#' @rdname casting +#' @export +as.csr.matrix = function(x, binary=FALSE) { + if ((inherits(x, "dgRMatrix") && !binary) || (inherits(x, "ngRMatrix") && binary)) + return(x) + + if (inherits(x, "float32")) + x = float::dbl(x) + + if (inherits(x, c("numeric", "integer"))) + x = matrix(x, nrow=1L) + + if (inherits(x, c("data.frame", "tibble", "data.table"))) + x = as.matrix(x) + + if (inherits(x, "dsparseVector")) { + X.csr = new("dgRMatrix") + X.csr@Dim = c(1L, x@length) + X.csr@p = c(0L, length(x@i)) + X.csr@j = x@i - 1L + X.csr@x = x@x + x = X.csr + } + + if (!inherits(x, "RsparseMatrix")) + x = as(x, "RsparseMatrix") + + if (!binary && !inherits(x, "dgRMatrix")) { + X.csr = new("dgRMatrix") + X.csr@Dim = x@Dim + X.csr@Dimnames = x@Dimnames + X.csr@p = x@p + X.csr@j = x@j + if (.hasSlot(x, "x")) + X.csr@x = as.numeric(x@x) + else + X.csr@x = rep(1., length(x@j)) + x = X.csr + } + + if (binary && !inherits(x, "ngRMatrix")) { + X.csr = new("ngRMatrix") + X.csr@Dim = x@Dim + X.csr@Dimnames = x@Dimnames + X.csr@p = x@p + X.csr@j = x@j + x = X.csr + } + return(x) +} + +#' @rdname casting +#' @export +as.csc.matrix = function(x, binary=FALSE) { + if ((inherits(x, "dgCMatrix") && !binary) || (inherits(x, "ngCMatrix") && binary)) + return(x) + + if (inherits(x, "float32")) + x = float::dbl(x) + + if (inherits(x, c("numeric", "integer", "data.frame", "tibble", "data.table"))) + x = as.matrix(x) + + if (!inherits(x, "CsparseMatrix")) + x = as(x, "CsparseMatrix") + + if (!binary && !inherits(x, "dgCMatrix")) { + X.csc = new("dgCMatrix") + X.csc@Dim = x@Dim + X.csc@Dimnames = x@Dimnames + X.csc@p = x@p + X.csc@i = x@i + if (.hasSlot(x, "x")) + X.csc@x = as.numeric(x@x) + else + X.csc@x = rep(1., length(x@i)) + x = X.csc + } + + if (binary && !inherits(x, "ngCMatrix")) { + X.csc = new("ngCMatrix") + X.csc@Dim = x@Dim + X.csc@Dimnames = x@Dimnames + X.csc@p = x@p + X.csc@i = x@i + x = X.csc + } + return(x) +} + +#' @rdname casting +#' @export +as.coo.matrix = function(x, binary=FALSE) { + if ((inherits(x, "dgTMatrix") && !binary) || (inherits(x, "ngTMatrix") && binary)) + return(x) + + if (inherits(x, "float32")) + x = float::dbl(x) + + if (inherits(x, c("numeric", "integer"))) + x = matrix(x, nrow=1L) + + if (inherits(x, c("data.frame", "tibble", "data.table"))) + x = as.matrix(x) + + if (inherits(x, "dsparseVector")) + x = as.csr.matrix(x) + + if (!inherits(x, "TsparseMatrix")) + x = as(x, "TsparseMatrix") + + if (!binary && !inherits(x, "dgTMatrix")) { + X.coo = new("dgTMatrix") + X.coo@Dim = x@Dim + X.coo@Dimnames = x@Dimnames + X.coo@i = x@i + X.coo@j = x@j + if (.hasSlot(x, "x")) + X.coo@x = as.numeric(x@x) + else + X.coo@x = rep(1., length(x@j)) + x = X.coo + } + + if (binary && !inherits(x, "ngTMatrix")) { + X.coo = new("ngTMatrix") + X.coo@Dim = x@Dim + X.coo@Dimnames = x@Dimnames + X.coo@i = x@i + X.coo@j = x@j + x = X.coo + } + return(x) +} diff --git a/README.md b/README.md index b28f48f..903df8f 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ `rsparse` is an R package for statistical learning primarily on **sparse matrices** - **matrix factorizations, factorization machines, out-of-core regression**. Many of the implemented algorithms are particularly useful for **recommender systems** and **NLP**. -On top of that we provide some optimized routines to work on sparse matrices - multithreaded matrix multiplications and improved support for sparse matrices in CSR format (`Matrix::RsparseMatrix`). +On top of that we provide some optimized routines to work on sparse matrices - multithreaded matrix multiplications and improved support for sparse matrices in CSR format (`Matrix::RsparseMatrix`), as well as convenience functions to convert between matrix types. We've paid some attention to the implementation details - we try to avoid data copies, utilize multiple threads via OpenMP and use SIMD where appropriate. Package **allows to work on datasets with millions of rows and millions of columns**. diff --git a/man/casting.Rd b/man/casting.Rd new file mode 100644 index 0000000..b4787cb --- /dev/null +++ b/man/casting.Rd @@ -0,0 +1,66 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/methods.R +\name{casting} +\alias{casting} +\alias{as.csr.matrix} +\alias{as.csc.matrix} +\alias{as.coo.matrix} +\title{Conversions between matrix types} +\usage{ +as.csr.matrix(x, binary = FALSE) + +as.csc.matrix(x, binary = FALSE) + +as.coo.matrix(x, binary = FALSE) +} +\arguments{ +\item{x}{A matrix which is to be converted to a different format.} + +\item{binary}{Whether the result should be a binary-only matrix (inheriting from +class `nsparseMatrix` - these don't have slot `x`). +Supported input types are:\itemize{ +\item Sparse matrices from `Matrix` package, in any format. +\item Sparse vector from `Matrix` (class `dsparseVector`). +\item Dense matrix from base R. +\item Dense vector from base R (classes `numeric` and `integer`). +\item Dense matrix or vector from package `float` (class `float32`). +\item `data.frame` and `data.table`. +}} +} +\value{ +A sparse matrix, with format:\itemize{ +\item CSR (a.k.a. `RsparseMatrix`) when calling `as.csr.matrix` +(class `dgRMatrix` with `binary=FALSE`, class `ngRMatrix` with `binary=TRUE`). +\item CSC (a.k.a. `CsparseMatrix`) when calling `as.csc.matrix` +(class `dgCMatrix` with `binary=FALSE`, class `ngCMatrix` with `binary=TRUE`). +\item COO (a.k.a. `TsparseMatrix`) when calling `as.coo.matrix` +(class `dgTMatrix` with `binary=FALSE`, class `ngTMatrix` with `binary=TRUE`). +} +} +\description{ +Convenience functions for converting to different sparse matrix formats. +} +\details{ +The functions internally use as(x, "?sparseMatrix"), so they might work +with other object classes if they register a conversion method for `Matrix` base +types. + +When passed a vector, the functions `as.csr.matrix` and `as.coo.matrix` will +assume that it is a row vector, while `as.csc.matrix` will assume it's a column vector. +} +\examples{ +library(Matrix) +library(rsparse) + +m.coo = as(matrix(1:3), "TsparseMatrix") +as.csr.matrix(m.coo) +as.csr.matrix(1:3) # <- assumes it's a row vector +as.csc.matrix(1:3) # <- assumes it's a column vector + +library(float) +m.f32 = float::fl(matrix(1:10, nrow=5)) +as.csr.matrix(m.f32) + +library(data.table) +as.coo.matrix(data.table(col1=1:3)) +} diff --git a/tests/testthat/test-conversions.R b/tests/testthat/test-conversions.R new file mode 100644 index 0000000..ceba183 --- /dev/null +++ b/tests/testthat/test-conversions.R @@ -0,0 +1,162 @@ +library("testthat") +library("rsparse") +context("Conversion functions") + +m.base = matrix(1:10, nrow=5) +m.coo = as(m.base, "TsparseMatrix") +m.coo.b = as(m.coo, "nsparseMatrix") +m.csr = as(m.base, "RsparseMatrix") +m.csr.b = as(m.csr, "nsparseMatrix") +m.csc = as(m.base, "CsparseMatrix") +m.csc.b = as(m.csc, "nsparseMatrix") +m.f32 = float::fl(m.base) +df = as.data.frame(m.base) +dt = data.table::as.data.table(df) +v.num = as.numeric(1:3) +v.int = as.integer(v.num) +v.f32 = float::fl(v.num) +v.sp = as(v.num, "dsparseVector") + +test_that("Conversion to CSR", { + expect_true(inherits(as.csr.matrix(m.base), "dgRMatrix")) + expect_true(inherits(as.csr.matrix(m.coo), "dgRMatrix")) + expect_true(inherits(as.csr.matrix(m.coo.b), "dgRMatrix")) + expect_true(inherits(as.csr.matrix(m.csr), "dgRMatrix")) + expect_true(inherits(as.csr.matrix(m.csr.b), "dgRMatrix")) + expect_true(inherits(as.csr.matrix(m.csc), "dgRMatrix")) + expect_true(inherits(as.csr.matrix(m.csc.b), "dgRMatrix")) + expect_true(inherits(as.csr.matrix(m.f32), "dgRMatrix")) + expect_true(inherits(as.csr.matrix(df), "dgRMatrix")) + expect_true(inherits(as.csr.matrix(dt), "dgRMatrix")) + expect_true(inherits(as.csr.matrix(v.num), "dgRMatrix")) + expect_true(inherits(as.csr.matrix(v.int), "dgRMatrix")) + expect_true(inherits(as.csr.matrix(v.f32), "dgRMatrix")) + expect_true(inherits(as.csr.matrix(v.sp), "dgRMatrix")) + + expect_true(inherits(as.csr.matrix(m.base, binary=TRUE), "ngRMatrix")) + expect_true(inherits(as.csr.matrix(m.coo, binary=TRUE), "ngRMatrix")) + expect_true(inherits(as.csr.matrix(m.coo.b, binary=TRUE), "ngRMatrix")) + expect_true(inherits(as.csr.matrix(m.csr, binary=TRUE), "ngRMatrix")) + expect_true(inherits(as.csr.matrix(m.csr.b, binary=TRUE), "ngRMatrix")) + expect_true(inherits(as.csr.matrix(m.csc, binary=TRUE), "ngRMatrix")) + expect_true(inherits(as.csr.matrix(m.csc.b, binary=TRUE), "ngRMatrix")) + expect_true(inherits(as.csr.matrix(m.f32, binary=TRUE), "ngRMatrix")) + expect_true(inherits(as.csr.matrix(df, binary=TRUE), "ngRMatrix")) + expect_true(inherits(as.csr.matrix(dt, binary=TRUE), "ngRMatrix")) + expect_true(inherits(as.csr.matrix(v.num, binary=TRUE), "ngRMatrix")) + expect_true(inherits(as.csr.matrix(v.int, binary=TRUE), "ngRMatrix")) + expect_true(inherits(as.csr.matrix(v.f32, binary=TRUE), "ngRMatrix")) + expect_true(inherits(as.csr.matrix(v.sp, binary=TRUE), "ngRMatrix")) + + expect_equal(nrow(as.csr.matrix(v.num)), 1L) + expect_equal(nrow(as.csr.matrix(v.int)), 1L) + expect_equal(nrow(as.csr.matrix(v.f32)), 1L) + expect_equal(nrow(as.csr.matrix(v.sp)), 1L) + + expect_equal(dim(as.csr.matrix(m.base)), dim(m.base)) + expect_equal(dim(as.csr.matrix(m.coo)), dim(m.coo)) + expect_equal(dim(as.csr.matrix(m.coo.b)), dim(m.coo.b)) + expect_equal(dim(as.csr.matrix(m.csr)), dim(m.csr)) + expect_equal(dim(as.csr.matrix(m.csr.b)), dim(m.csr.b)) + expect_equal(dim(as.csr.matrix(m.csc)), dim(m.csc)) + expect_equal(dim(as.csr.matrix(m.csc.b)), dim(m.csc.b)) + expect_equal(dim(as.csr.matrix(m.f32)), dim(m.f32)) + expect_equal(dim(as.csr.matrix(df)), dim(df)) + expect_equal(dim(as.csr.matrix(dt)), dim(dt)) +}) + +test_that("Conversion to CSC", { + expect_true(inherits(as.csc.matrix(m.base), "dgCMatrix")) + expect_true(inherits(as.csc.matrix(m.coo), "dgCMatrix")) + expect_true(inherits(as.csc.matrix(m.coo.b), "dgCMatrix")) + expect_true(inherits(as.csc.matrix(m.csr), "dgCMatrix")) + expect_true(inherits(as.csc.matrix(m.csr.b), "dgCMatrix")) + expect_true(inherits(as.csc.matrix(m.csc), "dgCMatrix")) + expect_true(inherits(as.csc.matrix(m.csc.b), "dgCMatrix")) + expect_true(inherits(as.csc.matrix(m.f32), "dgCMatrix")) + expect_true(inherits(as.csc.matrix(df), "dgCMatrix")) + expect_true(inherits(as.csc.matrix(dt), "dgCMatrix")) + expect_true(inherits(as.csc.matrix(v.num), "dgCMatrix")) + expect_true(inherits(as.csc.matrix(v.int), "dgCMatrix")) + expect_true(inherits(as.csc.matrix(v.f32), "dgCMatrix")) + expect_true(inherits(as.csc.matrix(v.sp), "dgCMatrix")) + + expect_true(inherits(as.csc.matrix(m.base, binary=TRUE), "ngCMatrix")) + expect_true(inherits(as.csc.matrix(m.coo, binary=TRUE), "ngCMatrix")) + expect_true(inherits(as.csc.matrix(m.coo.b, binary=TRUE), "ngCMatrix")) + expect_true(inherits(as.csc.matrix(m.csr, binary=TRUE), "ngCMatrix")) + expect_true(inherits(as.csc.matrix(m.csr.b, binary=TRUE), "ngCMatrix")) + expect_true(inherits(as.csc.matrix(m.csc, binary=TRUE), "ngCMatrix")) + expect_true(inherits(as.csc.matrix(m.csc.b, binary=TRUE), "ngCMatrix")) + expect_true(inherits(as.csc.matrix(m.f32, binary=TRUE), "ngCMatrix")) + expect_true(inherits(as.csc.matrix(df, binary=TRUE), "ngCMatrix")) + expect_true(inherits(as.csc.matrix(dt, binary=TRUE), "ngCMatrix")) + expect_true(inherits(as.csc.matrix(v.num, binary=TRUE), "ngCMatrix")) + expect_true(inherits(as.csc.matrix(v.int, binary=TRUE), "ngCMatrix")) + expect_true(inherits(as.csc.matrix(v.f32, binary=TRUE), "ngCMatrix")) + expect_true(inherits(as.csc.matrix(v.sp, binary=TRUE), "ngCMatrix")) + + expect_equal(ncol(as.csc.matrix(v.num)), 1L) + expect_equal(ncol(as.csc.matrix(v.int)), 1L) + expect_equal(ncol(as.csc.matrix(v.f32)), 1L) + expect_equal(ncol(as.csc.matrix(v.sp)), 1L) + + expect_equal(dim(as.csc.matrix(m.base)), dim(m.base)) + expect_equal(dim(as.csc.matrix(m.coo)), dim(m.coo)) + expect_equal(dim(as.csc.matrix(m.coo.b)), dim(m.coo.b)) + expect_equal(dim(as.csc.matrix(m.csr)), dim(m.csr)) + expect_equal(dim(as.csc.matrix(m.csr.b)), dim(m.csr.b)) + expect_equal(dim(as.csc.matrix(m.csc)), dim(m.csc)) + expect_equal(dim(as.csc.matrix(m.csc.b)), dim(m.csc.b)) + expect_equal(dim(as.csc.matrix(m.f32)), dim(m.f32)) + expect_equal(dim(as.csc.matrix(df)), dim(df)) + expect_equal(dim(as.csc.matrix(dt)), dim(dt)) +}) + +test_that("Conversion to COO", { + expect_true(inherits(as.coo.matrix(m.base), "dgTMatrix")) + expect_true(inherits(as.coo.matrix(m.coo), "dgTMatrix")) + expect_true(inherits(as.coo.matrix(m.coo.b), "dgTMatrix")) + expect_true(inherits(as.coo.matrix(m.csr), "dgTMatrix")) + expect_true(inherits(as.coo.matrix(m.csr.b), "dgTMatrix")) + expect_true(inherits(as.coo.matrix(m.csc), "dgTMatrix")) + expect_true(inherits(as.coo.matrix(m.csc.b), "dgTMatrix")) + expect_true(inherits(as.coo.matrix(m.f32), "dgTMatrix")) + expect_true(inherits(as.coo.matrix(df), "dgTMatrix")) + expect_true(inherits(as.coo.matrix(dt), "dgTMatrix")) + expect_true(inherits(as.coo.matrix(v.num), "dgTMatrix")) + expect_true(inherits(as.coo.matrix(v.int), "dgTMatrix")) + expect_true(inherits(as.coo.matrix(v.f32), "dgTMatrix")) + expect_true(inherits(as.coo.matrix(v.sp), "dgTMatrix")) + + expect_true(inherits(as.coo.matrix(m.base, binary=TRUE), "ngTMatrix")) + expect_true(inherits(as.coo.matrix(m.coo, binary=TRUE), "ngTMatrix")) + expect_true(inherits(as.coo.matrix(m.coo.b, binary=TRUE), "ngTMatrix")) + expect_true(inherits(as.coo.matrix(m.csr, binary=TRUE), "ngTMatrix")) + expect_true(inherits(as.coo.matrix(m.csr.b, binary=TRUE), "ngTMatrix")) + expect_true(inherits(as.coo.matrix(m.csc, binary=TRUE), "ngTMatrix")) + expect_true(inherits(as.coo.matrix(m.csc.b, binary=TRUE), "ngTMatrix")) + expect_true(inherits(as.coo.matrix(m.f32, binary=TRUE), "ngTMatrix")) + expect_true(inherits(as.coo.matrix(df, binary=TRUE), "ngTMatrix")) + expect_true(inherits(as.coo.matrix(dt, binary=TRUE), "ngTMatrix")) + expect_true(inherits(as.coo.matrix(v.num, binary=TRUE), "ngTMatrix")) + expect_true(inherits(as.coo.matrix(v.int, binary=TRUE), "ngTMatrix")) + expect_true(inherits(as.coo.matrix(v.f32, binary=TRUE), "ngTMatrix")) + expect_true(inherits(as.coo.matrix(v.sp, binary=TRUE), "ngTMatrix")) + + expect_equal(nrow(as.coo.matrix(v.num)), 1L) + expect_equal(nrow(as.coo.matrix(v.int)), 1L) + expect_equal(nrow(as.coo.matrix(v.f32)), 1L) + expect_equal(nrow(as.coo.matrix(v.sp)), 1L) + + expect_equal(dim(as.coo.matrix(m.base)), dim(m.base)) + expect_equal(dim(as.coo.matrix(m.coo)), dim(m.coo)) + expect_equal(dim(as.coo.matrix(m.coo.b)), dim(m.coo.b)) + expect_equal(dim(as.coo.matrix(m.csr)), dim(m.csr)) + expect_equal(dim(as.coo.matrix(m.csr.b)), dim(m.csr.b)) + expect_equal(dim(as.coo.matrix(m.csc)), dim(m.csc)) + expect_equal(dim(as.coo.matrix(m.csc.b)), dim(m.csc.b)) + expect_equal(dim(as.coo.matrix(m.f32)), dim(m.f32)) + expect_equal(dim(as.coo.matrix(df)), dim(df)) + expect_equal(dim(as.coo.matrix(dt)), dim(dt)) +}) From 48f12a4052210a33e11154263a54aeea21abb954 Mon Sep 17 00:00:00 2001 From: david-cortes Date: Mon, 18 Jan 2021 19:58:34 +0200 Subject: [PATCH 3/3] correct nthreads passed to predict (#59) --- R/MatrixFactorizationRecommender.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/MatrixFactorizationRecommender.R b/R/MatrixFactorizationRecommender.R index 9314d1a..9a5d844 100644 --- a/R/MatrixFactorizationRecommender.R +++ b/R/MatrixFactorizationRecommender.R @@ -60,7 +60,7 @@ MatrixFactorizationRecommender = R6::R6Class( not_recommend = as(not_recommend, "RsparseMatrix") uids = rownames(user_embeddings) - indices = find_top_product(user_embeddings, item_embeddings, k, not_recommend, items_exclude, self$global_bias) + indices = find_top_product(user_embeddings, item_embeddings, k, not_recommend, items_exclude, glob_mean=self$global_bias) data.table::setattr(indices, "dimnames", list(uids, NULL)) data.table::setattr(indices, "ids", NULL)