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.

Instance Attribute Summary collapse

Attributes inherited from VectorQuantization

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

Returns a new instance of IncrDictVQ.



12
13
14
15
16
17
18
19
20
21
22
# 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]}`" unless opts[:simil_type] == :dot
  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: :dot})

  @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



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

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:

  • (Integer)

    index of new centroid



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/machine_learning_workbench/compressor/incr_dict_vq.rb', line 31

def train_one vec, eps: equal_simil
  # NOTE:  novelty needs to be re-computed for each image, as after each
  # training the novelty signal changes!

# NOTE the reconstruction error here depends once more on the _color_
# this is wrong and should be taken out of the equation
# NOTE: this is fixed if I use the differences sparse coding method
  residual_img = reconstr_error(vec)
  rec_err = residual_img.mean
  return -1 if rec_err < eps
  puts "Creating centr #{ncentrs} (rec_err: #{rec_err})"
  # norm_vec = vec / NLinalg.norm(vec)
  # @centrs = centrs.concatenate norm_vec
  # @centrs = centrs.concatenate vec
  @centrs = centrs.concatenate residual_img
  # HACK: make it more general by using `code_size`
  @utility = @utility.concatenate [0] * (encoding_type == :sparse_coding_v1 ? 2 : 1)
  ncentrs
end