fm.train: Fitting Factorization Machine Models

Usage Arguments Examples

View source: R/fm_train.R

Usage

1
fm.train(data, normalize = TRUE, control)

Arguments

data
normalize

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
51
# 1. classification

library(FMwR)
data("airquality")
airquality <- airquality[complete.cases(airquality),]
airquality <- airquality[sample(1:nrow(airquality), 1000, TRUE),]
airquality$Ozone <- ifelse(airquality$Ozone > 60, 1, -1)
idx <- sample(1:nrow(airquality), ceiling(nrow(airquality)*0.6))

train <- fm.matrix(airquality[idx, 2:6], airquality[idx, 1])
test <- fm.matrix(airquality[-idx, 2:6], airquality[-idx, 1])

fm_fit <- fm.train(train, control = list(track.control(step_size = 1000),
                                         solver.control(solver = TDAP.solver(random_step = 10, gamma = 1e-5), max_iter = 12000),
                                         model.control(L2.w1 = 0.1)))
# or
# fm_fit <- fm.train(train, control = list(track.control(step_size = 1),
#                                          solver.control(solver = MCMC.solver(), max_iter = 20)))

fm_track <- fm.track(fm_fit, train, test, evaluate.metric = "ACC")
plot(fm_track)
fm_pred <- predict(fm_fit, test)
table(test$labels, fm_pred > 0.5)

fm_fit_update <- fm.update(fm_fit, train, control = list(track.control(step_size = 1000), solver.control(max_iter = 5000)))
fm_track_update <- fm.track(fm_fit_update, train, test, evaluate.metric = "LL")
fm_best <- fm.select(fm_fit_update, fm_track_update, drop.trace = TRUE)


# 2. regression

data("airquality")
airquality <- airquality[complete.cases(airquality),]
airquality <- airquality[sample(1:nrow(airquality), 1000, TRUE),]
idx <- sample(1:nrow(airquality), ceiling(nrow(airquality)*0.6))

train <- fm.matrix(airquality[idx, 2:6], airquality[idx, 1])
test <- fm.matrix(airquality[-idx, 2:6], airquality[-idx, 1])

fm_fit <- fm.train(train, normalize = T, control = list(track.control(step_size = 1000, evaluate.metric = "RMSE", convergence = 0),
                                                        solver.control(solver = SGD.solver(random_step = 10, learn_rate = 0.0001), max_iter = 12000),
                                                        model.control(task = "RE", factor.number = 3, L2.v = 0.5)))

# or
# fm_fit <- fm.train(train, normalize = T, control = list(track.control(step_size = 1, evaluate.metric = "RMSE"),
#                                                         solver.control(solver = ALS.solver(), max_iter = 20),
#                                                         model.control(task = "RE", factor.number = 2)))

fm_track <- fm.track(fm_fit, train, test, evaluate.metric = "MAE")
plot(fm_track)
fm_pred <- predict(fm_fit, test)

evanwang1990/FMwR documentation built on May 13, 2017, 10:03 p.m.