Class: Libsvmffi::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/libsvmffi/model.rb

Constant Summary collapse

TMP_MODEL_FILE =
"/tmp/libsvm_model.out"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Model

Returns a new instance of Model.



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
# File 'lib/libsvmffi/model.rb', line 16

def initialize(options = {})

  @parameters = Parameters.new      
  
  # TODO merge defaults options hash.
  @parameters[:svm_type] = C_SVC;
  @parameters[:kernel_type] = RBF;
  @parameters[:degree] = 3;
  @parameters[:gamma] = 0;  # 1/num_features
  @parameters[:coef0] = 0;
  @parameters[:nu] = 0.5;
  @parameters[:cache_size] = 100;
  @parameters[:C] = 1;
  @parameters[:eps] = 0.001;
  @parameters[:p] = 0.1;
  @parameters[:shrinking] = 1;
  @parameters[:probability] = 0;
  @parameters[:nr_weight] = 0;
  @parameters[:weight_label] = nil;
  @parameters[:weight] = nil;
  
  @elements = 0
  @examples, @labels, @features = [], [], []
  
  @nodes = []
end

Instance Attribute Details

#elementsObject

Returns the value of attribute elements.



10
11
12
# File 'lib/libsvmffi/model.rb', line 10

def elements
  @elements
end

#examplesObject

Returns the value of attribute examples.



8
9
10
# File 'lib/libsvmffi/model.rb', line 8

def examples
  @examples
end

#featuresObject

Returns the value of attribute features.



9
10
11
# File 'lib/libsvmffi/model.rb', line 9

def features
  @features
end

#labelsObject

Returns the value of attribute labels.



9
10
11
# File 'lib/libsvmffi/model.rb', line 9

def labels
  @labels
end

#nodesObject

Returns the value of attribute nodes.



12
13
14
# File 'lib/libsvmffi/model.rb', line 12

def nodes
  @nodes
end

#parametersObject

Returns the value of attribute parameters.



7
8
9
# File 'lib/libsvmffi/model.rb', line 7

def parameters
  @parameters
end

#problemObject

Returns the value of attribute problem.



7
8
9
# File 'lib/libsvmffi/model.rb', line 7

def problem
  @problem
end

#svm_modelObject

Returns the value of attribute svm_model.



7
8
9
# File 'lib/libsvmffi/model.rb', line 7

def svm_model
  @svm_model
end

#xObject

Returns the value of attribute x.



12
13
14
# File 'lib/libsvmffi/model.rb', line 12

def x
  @x
end

#x_spaceObject

Returns the value of attribute x_space.



10
11
12
# File 'lib/libsvmffi/model.rb', line 10

def x_space
  @x_space
end

Instance Method Details

#add(label, features) ⇒ Object

Adds an example which will be used upon trainining



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/libsvmffi/model.rb', line 46

def add(label, features)
  @labels.push label unless @labels.include? label
  indexed_features = {}
  features.each do |k, v|
    @features.push k unless @features.include? k
    indexed_features[@features.index(k)] = v
  end

  @examples.push({@labels.index(label) => indexed_features})

  @elements += features.length + 1 # also -1 terminator, see libsvm readme.
end

#add_from_file(filename) ⇒ Object

Add training examples from a libsvm training format file



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/libsvmffi/model.rb', line 153

def add_from_file(filename)
  f = File.open filename, 'r'
  f.lines.each do |l|
    tokens = l.split " "
    label = tokens.shift
    features = {}
    tokens.each do |t|
      index, value = t.split(":")
      features[index.to_i] = value.to_f
    end
    self.add label, features 
  end
end

#classify(features) ⇒ Object



102
103
104
105
106
# File 'lib/libsvmffi/model.rb', line 102

def classify(features)   
  nodes = fton(features) 
  label_index = Libsvmffi.svm_predict @svm_model, nodes
  @labels[label_index.to_i]
end

#fton(features) ⇒ Object

Features to array of node struct (currently factored just for debugging, only used in predict)



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/libsvmffi/model.rb', line 170

def fton(features)

 indexed_features = {}
  features.each do |k, v|
    indexed_features[@features.index(k)] = v unless !@features.include? k
  end
  
  nodes = FFI::MemoryPointer.new(Node, indexed_features.length + 1)
  indexed_features = indexed_features.merge({-1 => 0}) #terminator
  i = 0
  indexed_features.each do |k, v|
    n = Node.new nodes[i]
    n[:index] = k
    n[:value] = v
    i += 1
  end
  
  return nodes
  
end

#marshal_dumpObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/libsvmffi/model.rb', line 109

def marshal_dump
  
    d = Marshal.dump @labels
    d += "::::"
    d += Marshal.dump @features
  if !@svm_model.nil?
    # TODO surely there is a better way to do this.
    save_raw
    raw_str = File.open(TMP_MODEL_FILE, "r").read
    d += "::::"  
    d += raw_str
  end
  
  return d
end

#marshal_load(str) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/libsvmffi/model.rb', line 125

def marshal_load(str)
  @labels, @features, raw_model = str.split("::::")
  
  @labels = Marshal.load @labels
  @features = Marshal.load @features
  
  File.open(TMP_MODEL_FILE, "w") {|f| f.write raw_model}
  restore_raw
end

#restore_rawObject

Restore a model from a file.

NOTE: currently does not restore pre-train data


146
147
148
# File 'lib/libsvmffi/model.rb', line 146

def restore_raw
  @svm_model = Libsvmffi.svm_load_model FFI::MemoryPointer.from_string(TMP_MODEL_FILE)
end

#save_rawObject

Save to file



138
139
140
# File 'lib/libsvmffi/model.rb', line 138

def save_raw
  Libsvmffi.svm_save_model FFI::MemoryPointer.from_string(TMP_MODEL_FILE), @svm_model
end

#trainObject

Build libsvm model



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
# File 'lib/libsvmffi/model.rb', line 62

def train

  @problem = Problem.new
  @problem[:l] = @examples.length
  @problem[:y] = FFI::MemoryPointer.new(:double, @problem[:l])
  @x = FFI::MemoryPointer.new(:pointer, @problem[:l])
  @problem[:x] = @x.address #FFI::MemoryPointer.new(:pointer, @problem[:l])
  @x_space = FFI::MemoryPointer.new(Node, @elements) 

  y = @examples.map {|e| e.keys.first}
  @problem[:y].put_array_of_double 0, y

  i = 0
  space_index = 0
  examples.each do |ex| #TODO clean up this hash structure
    ex.each do |e|
      @x[i].write_pointer @x_space[space_index]
      i += 1          
 
      features = e.last.merge({-1 => 0}) #terminator
      features.each do |k, v|
        n = Node.new @x_space[space_index]
        n[:index] = k
        n[:value] = v
        space_index += 1
        
        @nodes.push n
      end
    end
  end
  
  @parameters[:gamma] = 1 / @features.length.to_f
  
  @svm_model = Libsvmffi.svm_train @problem.pointer, @parameters.pointer
  
end