liblinear-ruby 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +46 -0
  6. data/Rakefile +1 -0
  7. data/ext/Makefile +237 -0
  8. data/ext/blas.h +25 -0
  9. data/ext/blasp.h +430 -0
  10. data/ext/daxpy.c +49 -0
  11. data/ext/ddot.c +50 -0
  12. data/ext/dnrm2.c +62 -0
  13. data/ext/dscal.c +44 -0
  14. data/ext/extconf.rb +12 -0
  15. data/ext/liblinear_wrap.cxx +4646 -0
  16. data/ext/linear.cpp +2811 -0
  17. data/ext/linear.h +74 -0
  18. data/ext/linear.rb +357 -0
  19. data/ext/tron.cpp +235 -0
  20. data/ext/tron.h +34 -0
  21. data/lib/liblinear.rb +89 -0
  22. data/lib/liblinear/error.rb +4 -0
  23. data/lib/liblinear/model.rb +66 -0
  24. data/lib/liblinear/parameter.rb +42 -0
  25. data/lib/liblinear/problem.rb +55 -0
  26. data/lib/liblinear/version.rb +3 -0
  27. data/liblinear-1.93/COPYRIGHT +31 -0
  28. data/liblinear-1.93/Makefile +37 -0
  29. data/liblinear-1.93/Makefile.win +30 -0
  30. data/liblinear-1.93/README +531 -0
  31. data/liblinear-1.93/blas/Makefile +22 -0
  32. data/liblinear-1.93/blas/blas.a +0 -0
  33. data/liblinear-1.93/blas/blas.h +25 -0
  34. data/liblinear-1.93/blas/blasp.h +430 -0
  35. data/liblinear-1.93/blas/daxpy.c +49 -0
  36. data/liblinear-1.93/blas/daxpy.o +0 -0
  37. data/liblinear-1.93/blas/ddot.c +50 -0
  38. data/liblinear-1.93/blas/ddot.o +0 -0
  39. data/liblinear-1.93/blas/dnrm2.c +62 -0
  40. data/liblinear-1.93/blas/dnrm2.o +0 -0
  41. data/liblinear-1.93/blas/dscal.c +44 -0
  42. data/liblinear-1.93/blas/dscal.o +0 -0
  43. data/liblinear-1.93/heart_scale +270 -0
  44. data/liblinear-1.93/linear.cpp +2811 -0
  45. data/liblinear-1.93/linear.def +18 -0
  46. data/liblinear-1.93/linear.h +74 -0
  47. data/liblinear-1.93/linear.o +0 -0
  48. data/liblinear-1.93/matlab/Makefile +58 -0
  49. data/liblinear-1.93/matlab/README +197 -0
  50. data/liblinear-1.93/matlab/libsvmread.c +212 -0
  51. data/liblinear-1.93/matlab/libsvmwrite.c +106 -0
  52. data/liblinear-1.93/matlab/linear_model_matlab.c +176 -0
  53. data/liblinear-1.93/matlab/linear_model_matlab.h +2 -0
  54. data/liblinear-1.93/matlab/make.m +21 -0
  55. data/liblinear-1.93/matlab/predict.c +331 -0
  56. data/liblinear-1.93/matlab/train.c +418 -0
  57. data/liblinear-1.93/predict +0 -0
  58. data/liblinear-1.93/predict.c +245 -0
  59. data/liblinear-1.93/python/Makefile +4 -0
  60. data/liblinear-1.93/python/README +343 -0
  61. data/liblinear-1.93/python/liblinear.py +277 -0
  62. data/liblinear-1.93/python/liblinearutil.py +250 -0
  63. data/liblinear-1.93/ruby/liblinear.i +41 -0
  64. data/liblinear-1.93/ruby/liblinear_wrap.cxx +4646 -0
  65. data/liblinear-1.93/ruby/linear.h +74 -0
  66. data/liblinear-1.93/ruby/linear.o +0 -0
  67. data/liblinear-1.93/train +0 -0
  68. data/liblinear-1.93/train.c +399 -0
  69. data/liblinear-1.93/tron.cpp +235 -0
  70. data/liblinear-1.93/tron.h +34 -0
  71. data/liblinear-1.93/tron.o +0 -0
  72. data/liblinear-1.93/windows/liblinear.dll +0 -0
  73. data/liblinear-1.93/windows/libsvmread.mexw64 +0 -0
  74. data/liblinear-1.93/windows/libsvmwrite.mexw64 +0 -0
  75. data/liblinear-1.93/windows/predict.exe +0 -0
  76. data/liblinear-1.93/windows/predict.mexw64 +0 -0
  77. data/liblinear-1.93/windows/train.exe +0 -0
  78. data/liblinear-1.93/windows/train.mexw64 +0 -0
  79. data/liblinear-ruby.gemspec +24 -0
  80. metadata +152 -0
@@ -0,0 +1,235 @@
1
+ #include <math.h>
2
+ #include <stdio.h>
3
+ #include <string.h>
4
+ #include <stdarg.h>
5
+ #include "tron.h"
6
+
7
+ #ifndef min
8
+ template <class T> static inline T min(T x,T y) { return (x<y)?x:y; }
9
+ #endif
10
+
11
+ #ifndef max
12
+ template <class T> static inline T max(T x,T y) { return (x>y)?x:y; }
13
+ #endif
14
+
15
+ #ifdef __cplusplus
16
+ extern "C" {
17
+ #endif
18
+
19
+ extern double dnrm2_(int *, double *, int *);
20
+ extern double ddot_(int *, double *, int *, double *, int *);
21
+ extern int daxpy_(int *, double *, double *, int *, double *, int *);
22
+ extern int dscal_(int *, double *, double *, int *);
23
+
24
+ #ifdef __cplusplus
25
+ }
26
+ #endif
27
+
28
+ static void default_print(const char *buf)
29
+ {
30
+ fputs(buf,stdout);
31
+ fflush(stdout);
32
+ }
33
+
34
+ void TRON::info(const char *fmt,...)
35
+ {
36
+ char buf[BUFSIZ];
37
+ va_list ap;
38
+ va_start(ap,fmt);
39
+ vsprintf(buf,fmt,ap);
40
+ va_end(ap);
41
+ (*tron_print_string)(buf);
42
+ }
43
+
44
+ TRON::TRON(const function *fun_obj, double eps, int max_iter)
45
+ {
46
+ this->fun_obj=const_cast<function *>(fun_obj);
47
+ this->eps=eps;
48
+ this->max_iter=max_iter;
49
+ tron_print_string = default_print;
50
+ }
51
+
52
+ TRON::~TRON()
53
+ {
54
+ }
55
+
56
+ void TRON::tron(double *w)
57
+ {
58
+ // Parameters for updating the iterates.
59
+ double eta0 = 1e-4, eta1 = 0.25, eta2 = 0.75;
60
+
61
+ // Parameters for updating the trust region size delta.
62
+ double sigma1 = 0.25, sigma2 = 0.5, sigma3 = 4;
63
+
64
+ int n = fun_obj->get_nr_variable();
65
+ int i, cg_iter;
66
+ double delta, snorm, one=1.0;
67
+ double alpha, f, fnew, prered, actred, gs;
68
+ int search = 1, iter = 1, inc = 1;
69
+ double *s = new double[n];
70
+ double *r = new double[n];
71
+ double *w_new = new double[n];
72
+ double *g = new double[n];
73
+
74
+ for (i=0; i<n; i++)
75
+ w[i] = 0;
76
+
77
+ f = fun_obj->fun(w);
78
+ fun_obj->grad(w, g);
79
+ delta = dnrm2_(&n, g, &inc);
80
+ double gnorm1 = delta;
81
+ double gnorm = gnorm1;
82
+
83
+ if (gnorm <= eps*gnorm1)
84
+ search = 0;
85
+
86
+ iter = 1;
87
+
88
+ while (iter <= max_iter && search)
89
+ {
90
+ cg_iter = trcg(delta, g, s, r);
91
+
92
+ memcpy(w_new, w, sizeof(double)*n);
93
+ daxpy_(&n, &one, s, &inc, w_new, &inc);
94
+
95
+ gs = ddot_(&n, g, &inc, s, &inc);
96
+ prered = -0.5*(gs-ddot_(&n, s, &inc, r, &inc));
97
+ fnew = fun_obj->fun(w_new);
98
+
99
+ // Compute the actual reduction.
100
+ actred = f - fnew;
101
+
102
+ // On the first iteration, adjust the initial step bound.
103
+ snorm = dnrm2_(&n, s, &inc);
104
+ if (iter == 1)
105
+ delta = min(delta, snorm);
106
+
107
+ // Compute prediction alpha*snorm of the step.
108
+ if (fnew - f - gs <= 0)
109
+ alpha = sigma3;
110
+ else
111
+ alpha = max(sigma1, -0.5*(gs/(fnew - f - gs)));
112
+
113
+ // Update the trust region bound according to the ratio of actual to predicted reduction.
114
+ if (actred < eta0*prered)
115
+ delta = min(max(alpha, sigma1)*snorm, sigma2*delta);
116
+ else if (actred < eta1*prered)
117
+ delta = max(sigma1*delta, min(alpha*snorm, sigma2*delta));
118
+ else if (actred < eta2*prered)
119
+ delta = max(sigma1*delta, min(alpha*snorm, sigma3*delta));
120
+ else
121
+ delta = max(delta, min(alpha*snorm, sigma3*delta));
122
+
123
+ info("iter %2d act %5.3e pre %5.3e delta %5.3e f %5.3e |g| %5.3e CG %3d\n", iter, actred, prered, delta, f, gnorm, cg_iter);
124
+
125
+ if (actred > eta0*prered)
126
+ {
127
+ iter++;
128
+ memcpy(w, w_new, sizeof(double)*n);
129
+ f = fnew;
130
+ fun_obj->grad(w, g);
131
+
132
+ gnorm = dnrm2_(&n, g, &inc);
133
+ if (gnorm <= eps*gnorm1)
134
+ break;
135
+ }
136
+ if (f < -1.0e+32)
137
+ {
138
+ info("WARNING: f < -1.0e+32\n");
139
+ break;
140
+ }
141
+ if (fabs(actred) <= 0 && prered <= 0)
142
+ {
143
+ info("WARNING: actred and prered <= 0\n");
144
+ break;
145
+ }
146
+ if (fabs(actred) <= 1.0e-12*fabs(f) &&
147
+ fabs(prered) <= 1.0e-12*fabs(f))
148
+ {
149
+ info("WARNING: actred and prered too small\n");
150
+ break;
151
+ }
152
+ }
153
+
154
+ delete[] g;
155
+ delete[] r;
156
+ delete[] w_new;
157
+ delete[] s;
158
+ }
159
+
160
+ int TRON::trcg(double delta, double *g, double *s, double *r)
161
+ {
162
+ int i, inc = 1;
163
+ int n = fun_obj->get_nr_variable();
164
+ double one = 1;
165
+ double *d = new double[n];
166
+ double *Hd = new double[n];
167
+ double rTr, rnewTrnew, alpha, beta, cgtol;
168
+
169
+ for (i=0; i<n; i++)
170
+ {
171
+ s[i] = 0;
172
+ r[i] = -g[i];
173
+ d[i] = r[i];
174
+ }
175
+ cgtol = 0.1*dnrm2_(&n, g, &inc);
176
+
177
+ int cg_iter = 0;
178
+ rTr = ddot_(&n, r, &inc, r, &inc);
179
+ while (1)
180
+ {
181
+ if (dnrm2_(&n, r, &inc) <= cgtol)
182
+ break;
183
+ cg_iter++;
184
+ fun_obj->Hv(d, Hd);
185
+
186
+ alpha = rTr/ddot_(&n, d, &inc, Hd, &inc);
187
+ daxpy_(&n, &alpha, d, &inc, s, &inc);
188
+ if (dnrm2_(&n, s, &inc) > delta)
189
+ {
190
+ info("cg reaches trust region boundary\n");
191
+ alpha = -alpha;
192
+ daxpy_(&n, &alpha, d, &inc, s, &inc);
193
+
194
+ double std = ddot_(&n, s, &inc, d, &inc);
195
+ double sts = ddot_(&n, s, &inc, s, &inc);
196
+ double dtd = ddot_(&n, d, &inc, d, &inc);
197
+ double dsq = delta*delta;
198
+ double rad = sqrt(std*std + dtd*(dsq-sts));
199
+ if (std >= 0)
200
+ alpha = (dsq - sts)/(std + rad);
201
+ else
202
+ alpha = (rad - std)/dtd;
203
+ daxpy_(&n, &alpha, d, &inc, s, &inc);
204
+ alpha = -alpha;
205
+ daxpy_(&n, &alpha, Hd, &inc, r, &inc);
206
+ break;
207
+ }
208
+ alpha = -alpha;
209
+ daxpy_(&n, &alpha, Hd, &inc, r, &inc);
210
+ rnewTrnew = ddot_(&n, r, &inc, r, &inc);
211
+ beta = rnewTrnew/rTr;
212
+ dscal_(&n, &beta, d, &inc);
213
+ daxpy_(&n, &one, r, &inc, d, &inc);
214
+ rTr = rnewTrnew;
215
+ }
216
+
217
+ delete[] d;
218
+ delete[] Hd;
219
+
220
+ return(cg_iter);
221
+ }
222
+
223
+ double TRON::norm_inf(int n, double *x)
224
+ {
225
+ double dmax = fabs(x[0]);
226
+ for (int i=1; i<n; i++)
227
+ if (fabs(x[i]) >= dmax)
228
+ dmax = fabs(x[i]);
229
+ return(dmax);
230
+ }
231
+
232
+ void TRON::set_print_string(void (*print_string) (const char *buf))
233
+ {
234
+ tron_print_string = print_string;
235
+ }
@@ -0,0 +1,34 @@
1
+ #ifndef _TRON_H
2
+ #define _TRON_H
3
+
4
+ class function
5
+ {
6
+ public:
7
+ virtual double fun(double *w) = 0 ;
8
+ virtual void grad(double *w, double *g) = 0 ;
9
+ virtual void Hv(double *s, double *Hs) = 0 ;
10
+
11
+ virtual int get_nr_variable(void) = 0 ;
12
+ virtual ~function(void){}
13
+ };
14
+
15
+ class TRON
16
+ {
17
+ public:
18
+ TRON(const function *fun_obj, double eps = 0.1, int max_iter = 1000);
19
+ ~TRON();
20
+
21
+ void tron(double *w);
22
+ void set_print_string(void (*i_print) (const char *buf));
23
+
24
+ private:
25
+ int trcg(double delta, double *g, double *s, double *r);
26
+ double norm_inf(int n, double *x);
27
+
28
+ double eps;
29
+ int max_iter;
30
+ function *fun_obj;
31
+ void info(const char *fmt,...);
32
+ void (*tron_print_string)(const char *buf);
33
+ };
34
+ #endif
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'liblinear/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "liblinear-ruby"
8
+ spec.version = Liblinear::VERSION
9
+ spec.authors = ["Kei Tsuchiya"]
10
+ spec.email = ["kei.tsuchiya86@gmail.com"]
11
+ spec.description = %q{Ruby wrapper of LIBLINEAR using SWIG}
12
+ spec.summary = %q{Ruby wrapper of LIBLINEAR using SWIG}
13
+ spec.homepage = "https://github.com/kei500/liblinear-ruby"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.extensions << 'ext/extconf.rb'
24
+ end
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: liblinear-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kei Tsuchiya
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Ruby wrapper of LIBLINEAR using SWIG
42
+ email:
43
+ - kei.tsuchiya86@gmail.com
44
+ executables: []
45
+ extensions:
46
+ - ext/extconf.rb
47
+ extra_rdoc_files: []
48
+ files:
49
+ - .gitignore
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - ext/Makefile
55
+ - ext/blas.h
56
+ - ext/blasp.h
57
+ - ext/daxpy.c
58
+ - ext/ddot.c
59
+ - ext/dnrm2.c
60
+ - ext/dscal.c
61
+ - ext/extconf.rb
62
+ - ext/liblinear_wrap.cxx
63
+ - ext/linear.cpp
64
+ - ext/linear.h
65
+ - ext/linear.rb
66
+ - ext/tron.cpp
67
+ - ext/tron.h
68
+ - lib/liblinear.rb
69
+ - lib/liblinear/error.rb
70
+ - lib/liblinear/model.rb
71
+ - lib/liblinear/parameter.rb
72
+ - lib/liblinear/problem.rb
73
+ - lib/liblinear/version.rb
74
+ - liblinear-1.93/COPYRIGHT
75
+ - liblinear-1.93/Makefile
76
+ - liblinear-1.93/Makefile.win
77
+ - liblinear-1.93/README
78
+ - liblinear-1.93/blas/Makefile
79
+ - liblinear-1.93/blas/blas.a
80
+ - liblinear-1.93/blas/blas.h
81
+ - liblinear-1.93/blas/blasp.h
82
+ - liblinear-1.93/blas/daxpy.c
83
+ - liblinear-1.93/blas/daxpy.o
84
+ - liblinear-1.93/blas/ddot.c
85
+ - liblinear-1.93/blas/ddot.o
86
+ - liblinear-1.93/blas/dnrm2.c
87
+ - liblinear-1.93/blas/dnrm2.o
88
+ - liblinear-1.93/blas/dscal.c
89
+ - liblinear-1.93/blas/dscal.o
90
+ - liblinear-1.93/heart_scale
91
+ - liblinear-1.93/linear.cpp
92
+ - liblinear-1.93/linear.def
93
+ - liblinear-1.93/linear.h
94
+ - liblinear-1.93/linear.o
95
+ - liblinear-1.93/matlab/Makefile
96
+ - liblinear-1.93/matlab/README
97
+ - liblinear-1.93/matlab/libsvmread.c
98
+ - liblinear-1.93/matlab/libsvmwrite.c
99
+ - liblinear-1.93/matlab/linear_model_matlab.c
100
+ - liblinear-1.93/matlab/linear_model_matlab.h
101
+ - liblinear-1.93/matlab/make.m
102
+ - liblinear-1.93/matlab/predict.c
103
+ - liblinear-1.93/matlab/train.c
104
+ - liblinear-1.93/predict
105
+ - liblinear-1.93/predict.c
106
+ - liblinear-1.93/python/Makefile
107
+ - liblinear-1.93/python/README
108
+ - liblinear-1.93/python/liblinear.py
109
+ - liblinear-1.93/python/liblinearutil.py
110
+ - liblinear-1.93/ruby/liblinear.i
111
+ - liblinear-1.93/ruby/liblinear_wrap.cxx
112
+ - liblinear-1.93/ruby/linear.h
113
+ - liblinear-1.93/ruby/linear.o
114
+ - liblinear-1.93/train
115
+ - liblinear-1.93/train.c
116
+ - liblinear-1.93/tron.cpp
117
+ - liblinear-1.93/tron.h
118
+ - liblinear-1.93/tron.o
119
+ - liblinear-1.93/windows/liblinear.dll
120
+ - liblinear-1.93/windows/libsvmread.mexw64
121
+ - liblinear-1.93/windows/libsvmwrite.mexw64
122
+ - liblinear-1.93/windows/predict.exe
123
+ - liblinear-1.93/windows/predict.mexw64
124
+ - liblinear-1.93/windows/train.exe
125
+ - liblinear-1.93/windows/train.mexw64
126
+ - liblinear-ruby.gemspec
127
+ homepage: https://github.com/kei500/liblinear-ruby
128
+ licenses:
129
+ - MIT
130
+ metadata: {}
131
+ post_install_message:
132
+ rdoc_options: []
133
+ require_paths:
134
+ - lib
135
+ required_ruby_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - '>='
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - '>='
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ requirements: []
146
+ rubyforge_project:
147
+ rubygems_version: 2.0.2
148
+ signing_key:
149
+ specification_version: 4
150
+ summary: Ruby wrapper of LIBLINEAR using SWIG
151
+ test_files: []
152
+ has_rdoc: