Class: MachineLearningWorkbench::Compressor::DecayingLearningRateVQ

Inherits:
VectorQuantization show all
Defined in:
lib/machine_learning_workbench/compressor/decaying_learning_rate_vq.rb

Overview

VQ with per-centroid decaying learning rates. Optimized for online training.

Instance Attribute Summary collapse

Attributes inherited from VectorQuantization

#centrs, #dims, #encoding_type, #init_centr_vrange, #ncodes, #ntrains, #rng, #simil_type, #utility, #vrange

Instance Method Summary collapse

Methods inherited from VectorQuantization

#code_size, #encode, #init_centrs, #most_similar_centr, #ncentrs, #new_centr, #reconstr_error, #reconstruction, #similarities, #train

Constructor Details

#initialize(**opts) ⇒ DecayingLearningRateVQ

Returns a new instance of DecayingLearningRateVQ.



10
11
12
13
14
15
16
# File 'lib/machine_learning_workbench/compressor/decaying_learning_rate_vq.rb', line 10

def initialize **opts
  puts "Ignoring learning rate: `lrate: #{opts[:lrate]}`" if opts[:lrate]
  @lrate_min = opts.delete(:lrate_min) || 0.001
  @lrate_min_den = opts.delete(:lrate_min_den) || 1
  @decay_rate = opts.delete(:decay_rate) || 1
  super **opts.merge({lrate: nil})
end

Instance Attribute Details

#decay_rateObject (readonly)

Returns the value of attribute decay_rate.



8
9
10
# File 'lib/machine_learning_workbench/compressor/decaying_learning_rate_vq.rb', line 8

def decay_rate
  @decay_rate
end

#lrate_minObject (readonly)

Returns the value of attribute lrate_min.



8
9
10
# File 'lib/machine_learning_workbench/compressor/decaying_learning_rate_vq.rb', line 8

def lrate_min
  @lrate_min
end

#lrate_min_denObject (readonly)

Returns the value of attribute lrate_min_den.



8
9
10
# File 'lib/machine_learning_workbench/compressor/decaying_learning_rate_vq.rb', line 8

def lrate_min_den
  @lrate_min_den
end

Instance Method Details

#check_lrate(lrate) ⇒ Object

Overloading lrate check from original VQ



19
# File 'lib/machine_learning_workbench/compressor/decaying_learning_rate_vq.rb', line 19

def check_lrate lrate; nil; end

#lrate(centr_idx, min_den: lrate_min_den, lower_bound: lrate_min, decay: decay_rate) ⇒ Object

Note:

nicely overloads the ‘attr_reader` of parent class

Decaying per-centroid learning rate.

Parameters:

  • centr_idx (Integer)

    index of the centroid

  • lower_bound (Float) (defaults to: lrate_min)

    minimum learning rate



25
26
27
28
# File 'lib/machine_learning_workbench/compressor/decaying_learning_rate_vq.rb', line 25

def lrate centr_idx, min_den: lrate_min_den, lower_bound: lrate_min, decay: decay_rate
  [1.0/(ntrains[centr_idx]*decay+min_den), lower_bound].max
  .tap { |l| puts "centr: #{centr_idx}, ntrains: #{ntrains[centr_idx]}, lrate: #{l}" }
end

#train_one(vec, eps: nil) ⇒ Integer

Train on one vector

Returns:

  • (Integer)

    index of trained centroid



32
33
34
35
36
37
38
39
# File 'lib/machine_learning_workbench/compressor/decaying_learning_rate_vq.rb', line 32

def train_one vec, eps: nil
  # NOTE: ignores epsilon if passed
  trg_idx, _simil = most_similar_centr(vec)
  # norm_vec = vec / NLinalg.norm(vec)
  # centrs[trg_idx, true] = centrs[trg_idx, true] * (1-lrate(trg_idx)) + norm_vec * lrate(trg_idx)
  centrs[trg_idx, true] = centrs[trg_idx, true] * (1-lrate(trg_idx)) + vec * lrate(trg_idx)
  trg_idx
end