R: Second order Factorization Machines

FactorizationMachine {rsparse} | R Documentation

## Second order Factorization Machines

### Description

Creates second order Factorization Machines model

### Methods

#### Public methods

- `FactorizationMachine$new()`

- `FactorizationMachine$partial_fit()`

- `FactorizationMachine$fit()`

- `FactorizationMachine$predict()`

- `FactorizationMachine$clone()`

#### Method `new()`

creates Creates second order Factorization Machines model

##### Usage

    FactorizationMachine$new(
      learning_rate_w = 0.2,
      rank = 4,
      lambda_w = 0,
      lambda_v = 0,
      family = c("binomial", "gaussian"),
      intercept = TRUE,
      learning_rate_v = learning_rate_w
    )

##### Arguments

`learning_rate_w`

  learning rate for features intercations

`rank`

  dimension of the latent dimensions which models features interactions

`lambda_w`

  regularization for features interactions

`lambda_v`

  regularization for features

`family`

  one of `"binomial", "gaussian"`

`intercept`

  logical, indicates whether or not include intecept to the model

`learning_rate_v`

  learning rate for features

#### Method `partial_fit()`

fits/updates model

##### Usage

    FactorizationMachine$partial_fit(x, y, weights = rep(1, length(y)), ...)

##### Arguments

`x`

  input sparse matrix. Native format is `Matrix::RsparseMatrix`. If `x` is in different format, model will try to convert it to `RsparseMatrix` with `as(x, "RsparseMatrix")`. Dimensions should be (n_samples, n_features)

`y`

  vector of targets

`weights`

  numeric vector of length 'n_samples'. Defines how to amplify SGD updates for each sample. May be useful for highly unbalanced problems.

`...`

  not used at the moment

#### Method `fit()`

shorthand for applying 'partial_fit' 'n_iter' times

##### Usage

    FactorizationMachine$fit(x, y, weights = rep(1, length(y)), n_iter = 1L, ...)

##### Arguments

`x`

  input sparse matrix. Native format is `Matrix::RsparseMatrix`. If `x` is in different format, model will try to convert it to `RsparseMatrix` with `as(x, "RsparseMatrix")`. Dimensions should be (n_samples, n_features)

`y`

  vector of targets

`weights`

  numeric vector of length 'n_samples'. Defines how to amplify SGD updates for each sample. May be useful for highly unbalanced problems.

`n_iter`

  number of SGD epochs

`...`

  not used at the moment

#### Method `predict()`

makes predictions based on fitted model

##### Usage

    FactorizationMachine$predict(x, ...)

##### Arguments

`x`

  input sparse matrix of shape (n_samples, n_featires)

`...`

  not used at the moment

#### Method `clone()`

The objects of this class are cloneable with this method.

##### Usage

    FactorizationMachine$clone(deep = FALSE)

##### Arguments

`deep`

  Whether to make a deep clone.

### Examples

    # Factorization Machines can fit XOR function!
    x = rbind(
      c(0, 0),
      c(0, 1),
      c(1, 0),
      c(1, 1)
    )
    y = c(0, 1, 1, 0)

    x = as(x, "RsparseMatrix")
    fm = FactorizationMachine$new(learning_rate_w = 10, rank = 2, lambda_w = 0,
      lambda_v = 0, family = 'binomial', intercept = TRUE)
    res = fm$fit(x, y, n_iter = 100)
    preds = fm$predict(x)
    all(preds[c(1, 4)] < 0.01)
    all(preds[c(2, 3)] > 0.99)

[Package rsparse version 0.4.0 Index]