class ML::Learner::CyclicDescentLearner

Implementation of cyclic coordinate descent learner

Public Class Methods

new(dim, model = :basis) click to toggle source

Initialize a learner

@param [Integer] dim dimension

# File lib/method/cyclic_descent.rb, line 13
def initialize dim, model = :basis
  @dim = dim
  @model = model
end

Public Instance Methods

train!(data, iteration = 1000) click to toggle source

Train with a supervised data

@param [Hash] data supervised input data (mapping from array to integer) @param [Integer] iteration the desired iteration number

# File lib/method/cyclic_descent.rb, line 22
def train! data, iteration = 1000
  self.current_vector = Matrix.column_vector(Array.new(@dim + 1, 0))
  iteration.times do |i|
    v = calc_v i
    eta = calc_eta data, v
    self.current_vector += eta * v
  end
end

Private Instance Methods

calc_eta(data, v) click to toggle source
# File lib/method/cyclic_descent.rb, line 33
def calc_eta data, v
  v_t = v.transpose
  w_t = self.current_vector.transpose
  train = {}

  for xn, yn in data
    x_n = Matrix.column_vector(xn)
    dot = (v_t * x_n)[0,0] * yn
    thr = (w_t * x_n)[0,0] * (-yn) / dot
    
    next if dot == 0
    if dot > 0
      train[[thr]] = 1
    else
      train[[thr]] = -1
    end
  end

  learner = DecisionStumpLearner.new(1)
  learner.train! train
  learner.hypothesis[2]
end
calc_v(iteration) click to toggle source
# File lib/method/cyclic_descent.rb, line 56
def calc_v iteration
  v = Array.new(@dim + 1, 0)
  if @model == :basis
    v[iteration % @dim] = 1
  else
    v[iteration % @dim] = Util.normal_distribution 0,1
  end
  Matrix.column_vector(v)
end