class MachineLearningWorkbench::Compressor::CopyVQ

Train-less VQ, copying new images into centroids Optimized for online training.

Attributes

equal_simil[R]
next_train[R]

Public Class Methods

new(**opts) click to toggle source
Calls superclass method
# File lib/machine_learning_workbench/compressor/copy_vq.rb, line 10
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]
  # 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({lrate: nil, simil_type: nil})
  @ntrains << 0 # to count duplicates, images we skip the train on
  @next_train = 0 # pointer to the next centroid to train
end

Public Instance Methods

check_lrate(lrate;) click to toggle source

Overloading lrate check from original VQ

# File lib/machine_learning_workbench/compressor/copy_vq.rb, line 25
def check_lrate lrate; nil; end
ntrains() click to toggle source
# File lib/machine_learning_workbench/compressor/copy_vq.rb, line 21
def ntrains; @ntrains[0...-1]; end
ntrains_skip() click to toggle source
# File lib/machine_learning_workbench/compressor/copy_vq.rb, line 22
def ntrains_skip; @ntrains.last; end
train_one(vec, eps: equal_simil) click to toggle source

Train on one vector:

  • train only if the image is not already in dictionary

  • find the next untrained centroid

  • training is just overwriting it

@return [Integer] index of trained centroid

# File lib/machine_learning_workbench/compressor/copy_vq.rb, line 32
def train_one vec, eps: equal_simil
  mses = centrs.map do |centr|
    ((centr-vec)**2).sum / centr.size
  end
  # BEWARE: I am currently not handling the case where we run out of centroids!
  # => Will be addressed directly by dynamic dictionary size
  # return -1 if mses.min < eps
  return -1 if mses.min < eps || next_train == ncentrs
  trg_idx = next_train
  @next_train += 1
  # require 'pry'; binding.pry if next_train == ncentrs
  puts "Overwriting centr #{next_train}"
  # norm_vec = vec / NLinalg.norm(vec)
  # centrs[trg_idx, true] = norm_vec
  centrs[trg_idx, true] = vec
  trg_idx
end