Class: Cmfrec::Recommender

Inherits:
Object
  • Object
show all
Defined in:
lib/cmfrec/recommender.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(factors: 8, epochs: 10, verbose: true, user_bias: true, item_bias: true, add_implicit_features: false) ⇒ Recommender

Returns a new instance of Recommender.



5
6
7
8
9
10
11
12
13
14
# File 'lib/cmfrec/recommender.rb', line 5

def initialize(factors: 8, epochs: 10, verbose: true, user_bias: true, item_bias: true, add_implicit_features: false)
  set_params(
    k: factors,
    niter: epochs,
    verbose: verbose,
    user_bias: user_bias,
    item_bias: item_bias,
    add_implicit_features: add_implicit_features
  )
end

Instance Attribute Details

#global_meanObject (readonly)

Returns the value of attribute global_mean.



3
4
5
# File 'lib/cmfrec/recommender.rb', line 3

def global_mean
  @global_mean
end

Instance Method Details

#fit(train_set, user_info: nil, item_info: nil) ⇒ Object



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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/cmfrec/recommender.rb', line 16

def fit(train_set, user_info: nil, item_info: nil)
  train_set = to_dataset(train_set)

  @implicit = !train_set.any? { |v| v[:rating] }
  unless @implicit
    ratings = train_set.map { |o| o[:rating] }
    check_ratings(ratings)
  end

  check_training_set(train_set)
  create_maps(train_set)

  x_row = []
  x_col = []
  x_val = []
  value_key = @implicit ? :value : :rating
  train_set.each do |v|
    x_row << @user_map[v[:user_id]]
    x_col << @item_map[v[:item_id]]
    x_val << (v[value_key] || 1)
  end

  @m = @user_map.size
  @n = @item_map.size
  nnz = train_set.size

  x_row = int_ptr(x_row)
  x_col = int_ptr(x_col)
  x = real_ptr(x_val)

  x_full = nil
  weight = nil
  lam_unique = nil
  l1_lambda = 0
  l1_lam_unique = nil

  uu = nil
  ii = nil

  @user_info_map = {}
  u_row, u_col, u_sp, nnz_u, @m_u, p_ = process_info(, @user_map, @user_info_map, :user_id)

  @item_info_map = {}
  i_row, i_col, i_sp, nnz_i, @n_i, q = process_info(item_info, @item_map, @item_info_map, :item_id)

  @precompute_for_predictions = false

  # initialize w/ normal distribution
  reset_values = true

  @a = Fiddle::Pointer.malloc([@m, @m_u].max * (@k_user + @k + @k_main) * Fiddle::SIZEOF_DOUBLE)
  @b = Fiddle::Pointer.malloc([@n, @n_i].max * (@k_item + @k + @k_main) * Fiddle::SIZEOF_DOUBLE)
  @c = p_ > 0 ? Fiddle::Pointer.malloc(p_ * (@k_user + @k) * Fiddle::SIZEOF_DOUBLE) : nil
  @d = q > 0 ? Fiddle::Pointer.malloc(q * (@k_item + @k) * Fiddle::SIZEOF_DOUBLE) : nil

  @bias_a = nil
  @bias_b = nil

  u_colmeans = Fiddle::Pointer.malloc(p_ * Fiddle::SIZEOF_DOUBLE)
  i_colmeans = Fiddle::Pointer.malloc(q * Fiddle::SIZEOF_DOUBLE)

  if @implicit
    @w_main_multiplier = 1.0
    @alpha = 1.0
    @adjust_weight = false # downweight?
    @apply_log_transf = false

    # different defaults
    @lambda_ = 1e0
    @w_user = 10
    @w_item = 10
    @finalize_chol = false

    args = [
      @a, @b,
      @c, @d,
      reset_values, @random_state,
      u_colmeans, i_colmeans,
      @m, @n, @k,
      x_row, x_col, x, nnz,
      @lambda_, lam_unique,
      l1_lambda, l1_lam_unique,
      uu, @m_u, p_,
      ii, @n_i, q,
      u_row, u_col, u_sp, nnz_u,
      i_row, i_col, i_sp, nnz_i,
      @na_as_zero_user, @na_as_zero_item,
      @k_main, @k_user, @k_item,
      @w_main, @w_user, @w_item, real_ptr([@w_main_multiplier]),
      @alpha, @adjust_weight, @apply_log_transf,
      @niter, @nthreads, @verbose, @handle_interrupt,
      @use_cg, @max_cg_steps, @finalize_chol,
      @nonneg, @max_cd_steps, @nonneg_c, @nonneg_d,
      @precompute_for_predictions,
      nil, #precomputedBtB,
      nil, #precomputedBeTBe,
      nil  #precomputedBeTBeChol
    ]
    check_status FFI.fit_collective_implicit_als(*fiddle_args(args))

    @global_mean = 0
  else
    @bias_a = Fiddle::Pointer.malloc([@m, @m_u].max * Fiddle::SIZEOF_DOUBLE) if @user_bias
    @bias_b = Fiddle::Pointer.malloc([@n, @n_i].max * Fiddle::SIZEOF_DOUBLE) if @item_bias

    if @add_implicit_features
      @ai = Fiddle::Pointer.malloc([@m, @m_u].max * (@k + @k_main) * Fiddle::SIZEOF_DOUBLE)
      @bi = Fiddle::Pointer.malloc([@n, @n_i].max * (@k + @k_main) * Fiddle::SIZEOF_DOUBLE)
    else
      @ai = nil
      @bi = nil
    end

    glob_mean = Fiddle::Pointer.malloc(Fiddle::SIZEOF_DOUBLE)

    center = true
    scale_lam = false
    scale_lam_sideinfo = false

    args = [
      @bias_a, @bias_b,
      @a, @b,
      @c, @d,
      @ai, @bi,
      @add_implicit_features,
      reset_values, @random_state,
      glob_mean,
      u_colmeans, i_colmeans,
      @m, @n, @k,
      x_row, x_col, x, nnz,
      x_full,
      weight,
      @user_bias, @item_bias, center,
      @lambda_, lam_unique,
      l1_lambda, l1_lam_unique,
      scale_lam, scale_lam_sideinfo,
      uu, @m_u, p_,
      ii, @n_i, q,
      u_row, u_col, u_sp, nnz_u,
      i_row, i_col, i_sp, nnz_i,
      @na_as_zero, @na_as_zero_user, @na_as_zero_item,
      @k_main, @k_user, @k_item,
      @w_main, @w_user, @w_item, @w_implicit,
      @niter, @nthreads, @verbose, @handle_interrupt,
      @use_cg, @max_cg_steps, @finalize_chol,
      @nonneg, @max_cd_steps, @nonneg_c, @nonneg_d,
      @precompute_for_predictions,
      @include_all_x,
      nil, #B_plus_bias,
      nil, #precomputedBtB,
      nil, #precomputedTransBtBinvBt,
      nil, #precomputedBtXbias
      nil, #precomputedBeTBeChol,
      nil, #precomputedBiTBi,
      nil, #precomputedTransCtCinvCt,
      nil  #precomputedCtCw
    ]
    check_status FFI.fit_collective_explicit_als(*fiddle_args(args))

    @global_mean = real_array(glob_mean).first
  end

  @u_colmeans = real_array(u_colmeans)
  @i_colmeans = real_array(i_colmeans)
  @u_colmeans_ptr = u_colmeans

  self
end

#item_biasObject



270
271
272
# File 'lib/cmfrec/recommender.rb', line 270

def item_bias
  read_bias(@bias_b) if @bias_b
end

#item_factorsObject



262
263
264
# File 'lib/cmfrec/recommender.rb', line 262

def item_factors
  read_factors(@b, [@n, @n_i].max, @k_item + @k + @k_main)
end

#new_user_recs(data, count: 5, user_info: nil) ⇒ Object

TODO add item_ids



251
252
253
254
255
256
# File 'lib/cmfrec/recommender.rb', line 251

def new_user_recs(data, count: 5, user_info: nil)
  check_fit

  a_vec, a_bias = factors_warm(data, user_info: )
  top_n(a_vec: a_vec, a_bias: a_bias, count: count)
end

#predict(data) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/cmfrec/recommender.rb', line 185

def predict(data)
  check_fit

  data = to_dataset(data)

  u = data.map { |v| @user_map[v[:user_id]] || @user_map.size }
  i = data.map { |v| @item_map[v[:item_id]] || @item_map.size }

  row = int_ptr(u)
  col = int_ptr(i)
  n_predict = data.size
  predicted = Fiddle::Pointer.malloc(n_predict * Fiddle::SIZEOF_DOUBLE)

  if @implicit
    check_status FFI.predict_X_old_collective_implicit(
      row, col, predicted, n_predict,
      @a, @b,
      @k, @k_user, @k_item, @k_main,
      @m, @n,
      @nthreads
    )
  else
    check_status FFI.predict_X_old_collective_explicit(
      row, col, predicted, n_predict,
      @a, @bias_a,
      @b, @bias_b,
      @global_mean,
      @k, @k_user, @k_item, @k_main,
      @m, @n,
      @nthreads
    )
  end

  predictions = real_array(predicted)
  predictions.map! { |v| v.nan? ? @global_mean : v } if @implicit
  predictions
end

#user_biasObject



266
267
268
# File 'lib/cmfrec/recommender.rb', line 266

def user_bias
  read_bias(@bias_a) if @bias_a
end

#user_factorsObject



258
259
260
# File 'lib/cmfrec/recommender.rb', line 258

def user_factors
  read_factors(@a, [@m, @m_u].max, @k_user + @k + @k_main)
end

#user_recs(user_id, count: 5, item_ids: nil) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/cmfrec/recommender.rb', line 223

def user_recs(user_id, count: 5, item_ids: nil)
  check_fit
  user = @user_map[user_id]

  if user
    if item_ids
      # remove missing ids
      item_ids = item_ids.select { |v| @item_map[v] }

      data = item_ids.map { |v| {user_id: user_id, item_id: v} }
      scores = predict(data)

      item_ids.zip(scores).map do |item_id, score|
        {item_id: item_id, score: score}
      end
    else
      a_vec = @a[user * @k * Fiddle::SIZEOF_DOUBLE, @k * Fiddle::SIZEOF_DOUBLE]
      a_bias = @bias_a ? @bias_a[user * Fiddle::SIZEOF_DOUBLE, Fiddle::SIZEOF_DOUBLE].unpack1("d") : 0
      top_n(a_vec: a_vec, a_bias: a_bias, count: count)
    end
  else
    # no items if user is unknown
    # TODO maybe most popular items
    []
  end
end