libmf 0.2.6 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +11 -0
- data/LICENSE.txt +24 -21
- data/README.md +71 -6
- data/lib/libmf/ffi.rb +8 -1
- data/lib/libmf/model.rb +42 -7
- data/lib/libmf/version.rb +1 -1
- data/lib/libmf.rb +3 -3
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b264767ceb43c4a0407896e2bcf2373a47296e1e0580c65ec2ef08471589f661
|
4
|
+
data.tar.gz: addfb47cc44989620f847eac0392aef7431e596af08095ddd56ed227c4b99274
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b22bcab875b5c2e8b7432c137f90b3ea37a8c16c3e64aa0fb4c15f824f8345a3105ca981a4ea1ea091396dcd0ce10320eab52f56e6ede75601ea2192e5bc61ac
|
7
|
+
data.tar.gz: 2e946522f35f77a46ab3880d74da38a0efdcaf432eabc1945d35af344d5cafb2c7712a3e0660352f8cf84fbd44af5c9c4e1cc6110ef5b908cd66c3393e124a99
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,14 @@
|
|
1
|
+
## 0.4.0 (2024-10-22)
|
2
|
+
|
3
|
+
- Dropped support for Ruby < 3.1
|
4
|
+
|
5
|
+
## 0.3.0 (2022-08-07)
|
6
|
+
|
7
|
+
- Added metrics
|
8
|
+
- Prefer `save` over `save_model`
|
9
|
+
- Prefer `load` over `load_model`
|
10
|
+
- Dropped support for Ruby < 2.7
|
11
|
+
|
1
12
|
## 0.2.6 (2021-12-02)
|
2
13
|
|
3
14
|
- Improved ARM detection
|
data/LICENSE.txt
CHANGED
@@ -1,30 +1,33 @@
|
|
1
1
|
BSD 3-Clause License
|
2
2
|
|
3
|
-
Copyright (c) 2014-2015
|
4
|
-
Copyright (c) 2019-
|
3
|
+
Copyright (c) 2014-2015 The LIBMF Project.
|
4
|
+
Copyright (c) 2019-2024 Andrew Kane.
|
5
5
|
All rights reserved.
|
6
6
|
|
7
7
|
Redistribution and use in source and binary forms, with or without
|
8
|
-
modification, are permitted provided that the following conditions
|
8
|
+
modification, are permitted provided that the following conditions
|
9
|
+
are met:
|
9
10
|
|
10
|
-
1. Redistributions of source code must retain the above copyright
|
11
|
-
|
11
|
+
1. Redistributions of source code must retain the above copyright
|
12
|
+
notice, this list of conditions and the following disclaimer.
|
12
13
|
|
13
|
-
2. Redistributions in binary form must reproduce the above copyright
|
14
|
-
|
15
|
-
|
14
|
+
2. Redistributions in binary form must reproduce the above copyright
|
15
|
+
notice, this list of conditions and the following disclaimer in the
|
16
|
+
documentation and/or other materials provided with the distribution.
|
16
17
|
|
17
|
-
3. Neither
|
18
|
-
|
19
|
-
|
18
|
+
3. Neither name of copyright holders nor the names of its contributors
|
19
|
+
may be used to endorse or promote products derived from this software
|
20
|
+
without specific prior written permission.
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
OR
|
30
|
-
|
22
|
+
|
23
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
24
|
+
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
25
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
26
|
+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
|
27
|
+
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
28
|
+
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
29
|
+
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
30
|
+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
31
|
+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
32
|
+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
33
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
CHANGED
@@ -4,14 +4,14 @@
|
|
4
4
|
|
5
5
|
Check out [Disco](https://github.com/ankane/disco) for higher-level collaborative filtering
|
6
6
|
|
7
|
-
[![Build Status](https://github.com/ankane/libmf-ruby/workflows/build/badge.svg
|
7
|
+
[![Build Status](https://github.com/ankane/libmf-ruby/actions/workflows/build.yml/badge.svg)](https://github.com/ankane/libmf-ruby/actions)
|
8
8
|
|
9
9
|
## Installation
|
10
10
|
|
11
11
|
Add this line to your application’s Gemfile:
|
12
12
|
|
13
13
|
```ruby
|
14
|
-
gem
|
14
|
+
gem "libmf"
|
15
15
|
```
|
16
16
|
|
17
17
|
## Getting Started
|
@@ -54,13 +54,13 @@ model.bias
|
|
54
54
|
Save the model to a file
|
55
55
|
|
56
56
|
```ruby
|
57
|
-
model.
|
57
|
+
model.save("model.txt")
|
58
58
|
```
|
59
59
|
|
60
60
|
Load the model from a file
|
61
61
|
|
62
62
|
```ruby
|
63
|
-
model.
|
63
|
+
model = Libmf::Model.load("model.txt")
|
64
64
|
```
|
65
65
|
|
66
66
|
Pass a validation set
|
@@ -99,7 +99,7 @@ Libmf::Model.new(
|
|
99
99
|
lambda_q1: 0, # coefficient of L1-norm regularization on Q
|
100
100
|
lambda_q2: 0.1, # coefficient of L2-norm regularization on Q
|
101
101
|
learning_rate: 0.1, # learning rate
|
102
|
-
alpha:
|
102
|
+
alpha: 1, # importance of negative entries
|
103
103
|
c: 0.0001, # desired value of negative entries
|
104
104
|
nmf: false, # perform non-negative MF (NMF)
|
105
105
|
quiet: false # no outputs to stdout
|
@@ -126,6 +126,71 @@ For one-class matrix factorization
|
|
126
126
|
- `:one_class_col` - column-oriented pair-wise logarithmic loss
|
127
127
|
- `:one_class_l2` - squared error (L2-norm)
|
128
128
|
|
129
|
+
## Metrics
|
130
|
+
|
131
|
+
Calculate RMSE (for real-valued MF)
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
model.rmse(data)
|
135
|
+
```
|
136
|
+
|
137
|
+
Calculate MAE (for real-valued MF)
|
138
|
+
|
139
|
+
```ruby
|
140
|
+
model.mae(data)
|
141
|
+
```
|
142
|
+
|
143
|
+
Calculate generalized KL-divergence (for non-negative real-valued MF)
|
144
|
+
|
145
|
+
```ruby
|
146
|
+
model.gkl(data)
|
147
|
+
```
|
148
|
+
|
149
|
+
Calculate logarithmic loss (for binary MF)
|
150
|
+
|
151
|
+
```ruby
|
152
|
+
model.logloss(data)
|
153
|
+
```
|
154
|
+
|
155
|
+
Calculate accuracy (for binary MF)
|
156
|
+
|
157
|
+
```ruby
|
158
|
+
model.accuracy(data)
|
159
|
+
```
|
160
|
+
|
161
|
+
Calculate MPR (for one-class MF)
|
162
|
+
|
163
|
+
```ruby
|
164
|
+
model.mpr(data, transpose)
|
165
|
+
```
|
166
|
+
|
167
|
+
Calculate AUC (for one-class MF)
|
168
|
+
|
169
|
+
```ruby
|
170
|
+
model.auc(data, transpose)
|
171
|
+
```
|
172
|
+
|
173
|
+
## Example
|
174
|
+
|
175
|
+
Download the [MovieLens 100K dataset](https://grouplens.org/datasets/movielens/100k/) and use:
|
176
|
+
|
177
|
+
```ruby
|
178
|
+
require "csv"
|
179
|
+
|
180
|
+
train_set = Libmf::Matrix.new
|
181
|
+
valid_set = Libmf::Matrix.new
|
182
|
+
|
183
|
+
CSV.foreach("u.data", col_sep: "\t").with_index do |row, i|
|
184
|
+
data = i < 80000 ? train_set : valid_set
|
185
|
+
data.push(row[0].to_i, row[1].to_i, row[2].to_f)
|
186
|
+
end
|
187
|
+
|
188
|
+
model = Libmf::Model.new(factors: 20)
|
189
|
+
model.fit(train_set, eval_set: valid_set)
|
190
|
+
|
191
|
+
puts model.rmse(valid_set)
|
192
|
+
```
|
193
|
+
|
129
194
|
## Performance
|
130
195
|
|
131
196
|
For performance, read data directly from files
|
@@ -172,7 +237,7 @@ Everyone is encouraged to help improve this project. Here are a few ways you can
|
|
172
237
|
To get started with development:
|
173
238
|
|
174
239
|
```sh
|
175
|
-
git clone
|
240
|
+
git clone https://github.com/ankane/libmf-ruby.git
|
176
241
|
cd libmf-ruby
|
177
242
|
bundle install
|
178
243
|
bundle exec rake vendor:all
|
data/lib/libmf/ffi.rb
CHANGED
@@ -59,7 +59,14 @@ module Libmf
|
|
59
59
|
attach_function :mf_destroy_model, [:pointer], :void
|
60
60
|
attach_function :mf_train, [Problem.by_ref, Parameter.by_value], Model.auto_ptr
|
61
61
|
attach_function :mf_train_with_validation, [Problem.by_ref, Problem.by_ref, Parameter.by_value], Model.auto_ptr
|
62
|
-
attach_function :mf_predict, [Model.by_ref, :int, :int], :float
|
63
62
|
attach_function :mf_cross_validation, [Problem.by_ref, :int, Parameter.by_value], :double
|
63
|
+
attach_function :mf_predict, [Model.by_ref, :int, :int], :float
|
64
|
+
attach_function :calc_rmse, [Problem.by_ref, Model.by_ref], :double
|
65
|
+
attach_function :calc_mae, [Problem.by_ref, Model.by_ref], :double
|
66
|
+
attach_function :calc_gkl, [Problem.by_ref, Model.by_ref], :double
|
67
|
+
attach_function :calc_logloss, [Problem.by_ref, Model.by_ref], :double
|
68
|
+
attach_function :calc_accuracy, [Problem.by_ref, Model.by_ref], :double
|
69
|
+
attach_function :calc_mpr, [Problem.by_ref, Model.by_ref, :bool], :double
|
70
|
+
attach_function :calc_auc, [Problem.by_ref, Model.by_ref, :bool], :double
|
64
71
|
end
|
65
72
|
end
|
data/lib/libmf/model.rb
CHANGED
@@ -35,6 +35,13 @@ module Libmf
|
|
35
35
|
status = FFI.mf_save_model(model, path)
|
36
36
|
raise Error, "Cannot save model" if status != 0
|
37
37
|
end
|
38
|
+
alias_method :save, :save_model
|
39
|
+
|
40
|
+
def self.load(path)
|
41
|
+
model = Model.new
|
42
|
+
model.load_model(path)
|
43
|
+
model
|
44
|
+
end
|
38
45
|
|
39
46
|
def load_model(path)
|
40
47
|
@model = FFI.mf_load_model(path)
|
@@ -65,6 +72,34 @@ module Libmf
|
|
65
72
|
_factors(model[:q], columns, format)
|
66
73
|
end
|
67
74
|
|
75
|
+
def rmse(data)
|
76
|
+
FFI.calc_rmse(create_problem(data), model)
|
77
|
+
end
|
78
|
+
|
79
|
+
def mae(data)
|
80
|
+
FFI.calc_mae(create_problem(data), model)
|
81
|
+
end
|
82
|
+
|
83
|
+
def gkl(data)
|
84
|
+
FFI.calc_gkl(create_problem(data), model)
|
85
|
+
end
|
86
|
+
|
87
|
+
def logloss(data)
|
88
|
+
FFI.calc_logloss(create_problem(data), model)
|
89
|
+
end
|
90
|
+
|
91
|
+
def accuracy(data)
|
92
|
+
FFI.calc_accuracy(create_problem(data), model)
|
93
|
+
end
|
94
|
+
|
95
|
+
def mpr(data, transpose)
|
96
|
+
FFI.calc_mpr(create_problem(data), model, transpose)
|
97
|
+
end
|
98
|
+
|
99
|
+
def auc(data, transpose)
|
100
|
+
FFI.calc_auc(create_problem(data), model, transpose)
|
101
|
+
end
|
102
|
+
|
68
103
|
private
|
69
104
|
|
70
105
|
def _factors(ptr, n, format)
|
@@ -106,13 +141,13 @@ module Libmf
|
|
106
141
|
options[:bins] ||= 25 unless options[:nr_bins]
|
107
142
|
options[:copy_data] = false unless options.key?(:copy_data)
|
108
143
|
options_map = {
|
109
|
-
:
|
110
|
-
:
|
111
|
-
:
|
112
|
-
:
|
113
|
-
:
|
114
|
-
:
|
115
|
-
:
|
144
|
+
loss: :fun,
|
145
|
+
factors: :k,
|
146
|
+
threads: :nr_threads,
|
147
|
+
bins: :nr_bins,
|
148
|
+
iterations: :nr_iters,
|
149
|
+
learning_rate: :eta,
|
150
|
+
nmf: :do_nmf
|
116
151
|
}
|
117
152
|
options.each do |k, v|
|
118
153
|
k = options_map[k] if options_map[k]
|
data/lib/libmf/version.rb
CHANGED
data/lib/libmf.rb
CHANGED
@@ -2,9 +2,9 @@
|
|
2
2
|
require "ffi"
|
3
3
|
|
4
4
|
# modules
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
require_relative "libmf/matrix"
|
6
|
+
require_relative "libmf/model"
|
7
|
+
require_relative "libmf/version"
|
8
8
|
|
9
9
|
module Libmf
|
10
10
|
class Error < StandardError; end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: libmf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kane
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-10-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ffi
|
@@ -58,14 +58,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
58
58
|
requirements:
|
59
59
|
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
61
|
+
version: '3.1'
|
62
62
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
63
|
requirements:
|
64
64
|
- - ">="
|
65
65
|
- !ruby/object:Gem::Version
|
66
66
|
version: '0'
|
67
67
|
requirements: []
|
68
|
-
rubygems_version: 3.
|
68
|
+
rubygems_version: 3.5.16
|
69
69
|
signing_key:
|
70
70
|
specification_version: 4
|
71
71
|
summary: Large-scale sparse matrix factorization for Ruby
|