Class: MachineLearningWorkbench::Compressor::CopyVQ

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

Overview

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

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, #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) ⇒ CopyVQ

Returns a new instance of CopyVQ.



8
9
10
11
12
13
14
15
16
17
# File 'lib/machine_learning_workbench/compressor/copy_vq.rb', line 8

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

Instance Attribute Details

#equal_similObject (readonly)

Returns the value of attribute equal_simil.



6
7
8
# File 'lib/machine_learning_workbench/compressor/copy_vq.rb', line 6

def equal_simil
  @equal_simil
end

#next_trainObject (readonly)

Returns the value of attribute next_train.



6
7
8
# File 'lib/machine_learning_workbench/compressor/copy_vq.rb', line 6

def next_train
  @next_train
end

Instance Method Details

#check_lrate(lrate) ⇒ Object

Overloading lrate check from original VQ



23
# File 'lib/machine_learning_workbench/compressor/copy_vq.rb', line 23

def check_lrate lrate; nil; end

#ntrainsObject



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

def ntrains; @ntrains[0...-1]; end

#ntrains_skipObject



20
# File 'lib/machine_learning_workbench/compressor/copy_vq.rb', line 20

def ntrains_skip; @ntrains.last; end

#train_one(vec, eps: equal_simil) ⇒ Integer

Train on one vector:

  • train only if the image is not already in dictionary

  • find the next untrained centroid

  • training is just overwriting it

Returns:

  • (Integer)

    index of trained centroid



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

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}"
  centrs[trg_idx] = vec
  trg_idx
end