class CMADataLogger(interfaces.BaseDataLogger):
Constructor: CMADataLogger(name_prefix, modulo, append, expensive_modulo)
data logger for class CMAEvolutionStrategy
.
The logger is identified by its name prefix and (over-)writes or reads according data files. Therefore, the logger must be considered as global variable with unpredictable side effects, if two loggers with the same name and on the same working folder are used at the same time.
Examples
import cma es = cma.CMAEvolutionStrategy(12 * [3], 4) es.optimize(cma.ff.elli, callback=es.logger.plot)
plots into the current matplotlib
figure (or opens one if none
exists) via a generic callback. es.optimize already adds by default
es.logger.add to its callback list to add data to the logger. This
call:
x, es = cma.fmin2(cma.ff.elli, 12 * [3], 4, {'verb_plot': 1})
is very similar, but plots hard-coded into figure number 324:
es = cma.CMAEvolutionStrategy(...) logger = cma.CMADataLogger().register(es) while not es.stop(): ... logger.add() # add can also take an argument logger.plot() # or a short cut can be used: cma.plot() # plot data from logger with default name into new figure logger2 = cma.CMADataLogger('just_another_filename_prefix').load() logger2.plot() logger2.disp() import cma from matplotlib.pylab import * res = cma.fmin(cma.ff.sphere, rand(10), 1e-0) logger = res[-1] # the CMADataLogger logger.load() # by "default" data are on disk semilogy(logger.f[:,0], logger.f[:,5]) # plot f versus iteration, see file header cma.s.figshow()
Details
After loading data, the logger has the attributes xmean
, xrecent
,
std
, f
, D
and corrspec
corresponding to xmean,
xrecentbest, stddev, fit, axlen and axlencorr
filename trails.
See Also | |
disp (), plot () |
Method | __init__ |
initialize logging of data from a CMAEvolutionStrategy instance. |
Method | add |
append some logging data from CMAEvolutionStrategy class instance es , if number_of_times_called % modulo equals to zero, never if modulo==0. |
Method | disp |
displays selected data from (files written by) the class CMADataLogger . |
Method | disp |
Undocumented |
Method | downsampling |
rude downsampling of a CMADataLogger data file by factor , keeping also the first first entries. This function is a stump and subject to future changes. Return self. |
Method | figclose |
Undocumented |
Method | initialize |
reset logger, overwrite original files, modulo : log only every modulo call |
Method | load |
load (or reload) data from output files, load is called in plot and disp . |
Method | plot |
plot data from a CMADataLogger (using the files written by the logger). |
Method | plot |
plot data from a CMADataLogger (using the files written by the logger). |
Method | plot |
Undocumented |
Method | plot |
spectrum of correlation or precision matrix and percentiles of off-diagonal entries |
Method | plot |
plot fitness, sigma, axis ratio... |
Method | plot |
Undocumented |
Method | plot |
plot (outer) scaling from diagonal decoding. |
Method | plot |
iabscissa=1 means vs function evaluations, idx picks variables to plot |
Method | plot |
Undocumented |
Method | register |
register a CMAEvolutionStrategy instance for logging, append=True appends to previous data logged under the same name, by default previous data are overwritten. |
Method | save |
data are saved to disk the moment they are added |
Method | save |
saves logger data to a different set of files, for switch=True also the loggers name prefix is switched to the new value |
Method | select |
keep only data of iteration_indices |
Method | zip |
write logger data to file filename [+ ...] + '.tar.gz'. |
Class Variable | default |
Undocumented |
Instance Variable | append |
append to previous data |
Instance Variable | counter |
number of calls to add |
Instance Variable | es |
Undocumented |
Instance Variable | expensive |
log also values that need an eigendecomposition to be generated every expensive iteration |
Instance Variable | fighandle |
Undocumented |
Instance Variable | file |
used in load, however hard-coded in add, because data must agree with name |
Instance Variable | key |
used in load, however hard-coded in plot |
Instance Variable | last |
Undocumented |
Instance Variable | last |
Undocumented |
Instance Variable | last |
Undocumented |
Instance Variable | last |
Undocumented |
Instance Variable | modulo |
how often to record data, allows calling add without args |
Instance Variable | name |
Undocumented |
Instance Variable | original |
Undocumented |
Instance Variable | persistent |
Undocumented |
Instance Variable | registered |
Undocumented |
Instance Variable | relative |
Undocumented |
Instance Variable | skip |
Undocumented |
Instance Variable | timer |
Undocumented |
Instance Variable | timer |
Undocumented |
Instance Variable | x |
Undocumented |
Property | data |
return dictionary with data. |
Method | _enter |
assumes that a figure is open |
Method | _finalize |
Undocumented |
Method | _plot |
see def plot(... |
Method | _unzip |
to unzip use "tar -xf filename" in a system shell |
Method | _xlabel |
Undocumented |
Instance Variable | _eigen |
Undocumented |
Instance Variable | _key |
used in load to add one data row to be modified in plot |
Inherited from BaseDataLogger
:
Instance Variable | filename |
file to save to or load from unless specified otherwise |
Instance Variable | optim |
object instance to be logging data from |
Instance Variable | _data |
dict of logged data |
cma.interfaces.BaseDataLogger.__init__
initialize logging of data from a CMAEvolutionStrategy
instance.
Default modulo=1 means logging with each call of add
. If
append is True data is appended to already existing data of a
logger with the same name. Additional eigendecompositions are
allowed only every expensive_modulo
-th logged iteration.
cma.interfaces.BaseDataLogger.add
append some logging data from CMAEvolutionStrategy
class instance es
,
if number_of_times_called % modulo equals to zero, never if modulo==0.
more_data is a list of additional data to be recorded where each data entry must have the same length.
When used for a different optimizer class, this function can be (easily?) adapted by changing the assignments under INTERFACE in the implemention.
cma.interfaces.BaseDataLogger.disp
displays selected data from (files written by) the class
CMADataLogger
.
Arguments
idx
- indices corresponding to rows in the data file; if idx is a scalar (int), the first two, then every idx-th, and the last three rows are displayed. Too large index values are removed. If idx=='header', the header line is printed.
Example
>>> import cma, numpy as np >>> res = cma.fmin(cma.ff.elli, 7 * [0.1], 1, {'verb_disp':1e9}) # generate data ... #doctest: +ELLIPSIS (4... >>> assert res[1] < 1e-9 >>> assert res[2] < 4400 >>> l = cma.CMADataLogger() # == res[-1], logger with default name, "points to" above data >>> l.disp([0,-1]) # first and last ... #doctest: +ELLIPSIS Iterat Nfevals function value axis ratio maxstd minstd... >>> l.disp(20) # some first/last and every 20-th line ... #doctest: +ELLIPSIS Iterat Nfevals function value axis ratio maxstd minstd... >>> l.disp(np.r_[0:999999:100, -1]) # every 100-th and last ... #doctest: +ELLIPSIS Iterat Nfevals function value axis ratio maxstd minstd... >>> l.disp(np.r_[0, -10:0]) # first and ten last ... #doctest: +ELLIPSIS Iterat Nfevals function value axis ratio maxstd minstd... >>> cma.disp(l.name_prefix, np.r_[0:9999999:100, -10:]) # the same as l.disp(...) ... #doctest: +ELLIPSIS Iterat Nfevals function value axis ratio maxstd minstd...
Details
The data line with the best f-value is displayed as last line.
Use CMADataLogger.disp
if the logger does not have the default
name.
See Also | |
CMADataLogger.disp , CMADataLogger.disp |
rude downsampling of a CMADataLogger
data file by factor
,
keeping also the first first
entries. This function is a
stump and subject to future changes. Return self.
Arguments
factor
-- downsampling factorfirst
-- keep firstfirst
entries
switch
-- switch the new logger to the downsampled logger- original_name+'down'
Details
self.name_prefix+'down' files are written
Example
import cma cma.downsampling() # takes outcma/cma* files cma.plot('outcma/cmadown')
cma.interfaces.BaseDataLogger.load
load (or reload) data from output files, load
is called in
plot
and disp
.
Argument filenameprefix
is the filename prefix of data to be
loaded (5-8 files), by default 'outcmaes/' (where / stands
for the OS-specific path seperator).
Return self with (added) attributes xrecent
, xmean
,
f
, D
, std
, corrspec
, sigvec
.
cma.interfaces.BaseDataLogger.plot
plot data from a CMADataLogger
(using the files written
by the logger).
Arguments
fig
- figure number, by default starting from 325
iabscissa
- 0==plot versus iteration count, 1==plot versus function evaluation number
iteridx
- iteration indices to plot, e.g. range(100) for the first 100 evaluations.
x_opt
- If
isscalar(x_opt)
it is interpreted as iteration number and the difference of x to the respective iteration is plotted. If it is a negative scalar the respective index rather than the iteration is used. Namely in particular, x_opt=0 subtracts the initial solution X0 and x_opt=-1 subtracts the final solution of the data. If len(x_opt) == dimension, the difference tox_opt
is plotted, otherwise the first row of x_opt are the indices of the variables to be plotted and the second row, if present, is used to take the difference. fshift
- is added to all displayed fitness values
Return CMADataLogger
itself.
Examples
import cma logger = cma.CMADataLogger() # with default name # try to plot the "default logging" data (e.g. # from previous fmin calls, which is essentially what # also cma.plot() does) logger.plot() cma.s.figsave('fig325.png') # save current figure logger.figclose()
Dependencies: matlabplotlib.pyplot
plot data from a CMADataLogger
(using the files written by the logger).
Superseded by plot
?
Arguments
fig
- figure number, by default 425
iabscissa
- 0==plot versus iteration count, 1==plot versus function evaluation number
iteridx
- iteration indices to plot
x_opt
- If
isscalar(x_opt)
it is interpreted as iteration number and the difference of x to the respective iteration is plotted. If it is a negative scalar the respective index rather than the iteration is used. Namely in particular, x_opt=0 subtracts the initial solution X0 and x_opt=-1 subtracts the final solution of the data. If len(x_opt) == dimension, the difference tox_opt
is plotted, otherwise the first row of x_opt are the indices of the variables to be plotted and the second row, if present, is used to take the difference.
Return CMADataLogger
itself.
Examples
import cma logger = cma.CMADataLogger() # with default name # try to plot the "default logging" data (e.g. # from previous fmin calls, which is essentially what # also cma.plot() does) logger.plot_all() cma.s.figsave('fig425.png') # save current figure logger.s.figclose()
Dependencies: matlabplotlib/pyplot.
plot fitness, sigma, axis ratio...
Parameters | |
iabscissa | 0 means vs evaluations, 1 means vs iterations |
foffset | added to f-value after abs(f) is taken |
fshift | Undocumented |
message | Undocumented |
See Also | |
plot |
plot (outer) scaling from diagonal decoding.
iabscissa=1 plots vs function evaluations
idx
picks variables to plot if len(idx) < N, otherwise it picks
iteration indices (in case, after downsampling).
Undocumented
cma.interfaces.BaseDataLogger.register
register a CMAEvolutionStrategy
instance for logging,
append=True appends to previous data logged under the same name,
by default previous data are overwritten.
saves logger data to a different set of files, for switch=True also the loggers name prefix is switched to the new value
write logger data to file filename [+ ...] + '.tar.gz'.
filename
defaults to self.name_prefix.
When unique
is true, filename
is furnished with a unique time
stamp.
Return the path (relative or absolute) of the output file.
This function does in essence just tar -czf filename.tar.gz folder where folder defaults to the current plot data folder and filename is created to be unique.
TODO: we may want to be able to add a time stamp to the folder name too, to prevent conficts later?
log also values that need an eigendecomposition to be generated every expensive
iteration
cma.interfaces.BaseDataLogger.data
return dictionary with data.
If data entries are None or incomplete, consider calling .load().data to (re-)load the data from files first.
see def plot(...
to unzip use "tar -xf filename" in a system shell
and afterwards:
logger = cma.CMADataLogger(extacted_folder_name).plot()
in a Python shell.