Class: MachineLearningWorkbench::Compressor::IncrDictVQ

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

Overview

Incremental Dictionary Train-less VQ, creating new centroids rather than training Optimized for online training. TODO: as the deadline grows nigh, the hacks grow foul. Refactor all VQs together.

Constant Summary

Constants inherited from VectorQuantization

VectorQuantization::SIMIL

Instance Attribute Summary collapse

Attributes inherited from VectorQuantization

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

Instance Method Summary collapse

Methods inherited from VectorQuantization

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

Constructor Details

#initialize(**opts) ⇒ IncrDictVQ

Returns a new instance of IncrDictVQ.



12
13
14
15
16
17
18
19
20
21
# File 'lib/machine_learning_workbench/compressor/incr_dict_vq.rb', line 12

def initialize **opts
  puts "Ignoring learning rate: `lrate: #{opts[:lrate]}`" if opts[:lrate]
  puts "Ignoring similarity: `simil_type: #{opts[:simil_type]}`" if opts[:simil_type]
  puts "Ignoring ncentrs: `ncentrs: #{opts[:ncentrs]}`" if opts[:ncentrs]
  # TODO: try different epsilons to reduce the number of states
  # for example, in qbert we care what is lit and what is not, not the colors
  @equal_simil = opts.delete(:equal_simil) || 0.0
  super **opts.merge({ncentrs: 1, lrate: nil, simil_type: nil})
  @ntrains = nil # will disable the counting
end

Instance Attribute Details

#equal_similObject (readonly)

Returns the value of attribute equal_simil.



9
10
11
# File 'lib/machine_learning_workbench/compressor/incr_dict_vq.rb', line 9

def equal_simil
  @equal_simil
end

Instance Method Details

#check_lrate(lrate) ⇒ Object

Overloading lrate check from original VQ



24
# File 'lib/machine_learning_workbench/compressor/incr_dict_vq.rb', line 24

def check_lrate lrate; nil; end

#train_one(vec, eps: equal_simil) ⇒ Integer

Train on one vector:

  • train only if the image is not already in dictionary

  • create new centroid from the image

Returns:

  • index of new centroid



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/machine_learning_workbench/compressor/incr_dict_vq.rb', line 30

def train_one vec, eps: equal_simil
  mses = centrs.map do |centr|
    ((centr-vec)**2).sum / centr.size # uhm get rid of division maybe? squares?
  end
  min_mse = mses.min
  # skip training if the centr with smallest mse (most similar) has less than eps error (equal)
  # TODO: maintain an average somewhere, make eps dynamic
  return if min_mse < eps
  puts "Creating centr #{ncentrs} (min_mse: #{min_mse})"
  centrs << vec
  @utility = @utility.concatenate 0
  @ncentrs.tap{ @ncentrs += 1}
end