Class: XNES

Inherits:
NES
  • Object
show all
Defined in:
lib/neuroevo/nes.rb

Instance Attribute Summary collapse

Attributes inherited from NES

#id, #mu, #ndims, #obj_fn, #opt_type, #rand, #sigma

Instance Method Summary collapse

Methods inherited from NES

#hansen_lrate, #hansen_popsize, #hansen_utilities, #initialize, #lrate, #move_inds, #popsize, #run, #sorted_inds, #standard_normal_samples, #unit_normal_sample, #utils

Constructor Details

This class inherits a constructor from NES

Instance Attribute Details

#log_sigmaObject (readonly)

Exponential NES


119
120
121
# File 'lib/neuroevo/nes.rb', line 119

def log_sigma
  @log_sigma
end

Instance Method Details

#convergenceObject


143
144
145
146
# File 'lib/neuroevo/nes.rb', line 143

def convergence
  # Estimate algorithm convergence as total variance
  sigma.trace
end

#initialize_distributionObject


121
122
123
124
125
126
127
# File 'lib/neuroevo/nes.rb', line 121

def initialize_distribution
  @mu = NMatrix.new([1, ndims], 0, dtype: :float64)
  @sigma = NMatrix.identity(ndims, dtype: :float64)
  # XNES mostly works with the log of sigma to avoid continuous decompositions
  # question: what is the matrix that, once exponentiated, yields identity?
  @log_sigma = NMatrix.zeros(ndims, dtype: :float64)
end

#trainObject


129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/neuroevo/nes.rb', line 129

def train
  picks = sorted_inds
  g_mu = utils.dot(picks)
  g_log_sigma = popsize.times.inject(NMatrix.zeros_like sigma) do |sum, i|
    u = utils[i]
    ind = picks.row(i)
    ind_sq = ind.outer_flat(ind, &:*)
    sum + (ind_sq - id) * u
  end
  @mu += sigma.dot(g_mu.transpose).transpose * lrate
  @log_sigma += g_log_sigma * (lrate/2)
  @sigma = log_sigma.exponential
end