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.

Instance Attribute Summary collapse

Attributes inherited from VectorQuantization

#centrs, #dims, #encoding_type, #init_centr_vrange, #lrate, #ncodes, #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) ⇒ CopyVQ

Returns a new instance of CopyVQ.



10
11
12
13
14
15
16
17
18
19
# 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

Instance Attribute Details

#equal_similObject (readonly)

Returns the value of attribute equal_simil.



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

def equal_simil
  @equal_simil
end

#next_trainObject (readonly)

Returns the value of attribute next_train.



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

def next_train
  @next_train
end

Instance Method Details

#check_lrate(lrate) ⇒ Object

Overloading lrate check from original VQ



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

def check_lrate lrate; nil; end

#ntrainsObject



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

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

#ntrains_skipObject



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

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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