forked from VowpalWabbit/vowpal_wabbit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vw-convergence
executable file
·300 lines (263 loc) · 9.16 KB
/
vw-convergence
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/perl -w
# vim: tabstop=4 shiftwidth=4 expandtab nosmarttab
#
# Generate & display convergence charts from vw progress outputs
# Requires R to generate the charts
#
use Getopt::Std;
use vars qw ($opt_d $opt_x $opt_y $opt_t
$opt_w $opt_h $opt_a $opt_q $opt_p $opt_Q
$opt_o $opt_v $opt_l
);
my $TmpImgFile = '/tmp/vw-convergence.png';
my $TmpRFile = '/tmp/vw-convergence.R';
my $LossStr;
my $DisplayProg = 'display';
my ($DefaultWidth, $DefaultHeight) = (800, 600);
sub find_image_viewer() {
# If your favorite OS image viewer isn't here, please add it
foreach my $iv (
$DisplayProg, 'display', 'gwenview', 'kuickshow', # Linux
'xee', 'preview' # OS-X alternatives
) {
my $path = `which $iv`; chomp($path);
if (-x $path) {
$DisplayProg = $iv;
v("find_image_viewer: found executable '%s' @ '%s'\n", $iv, $path);
return;
}
}
# if we get here without finding anything, we can't display
warn "$0: find_image_viewer: couldn't find an image-viewer in this env\n";
}
sub v(@) {
return unless $opt_v;
if (@_ == 1) {
print STDERR @_;
} else {
printf STDERR @_;
}
}
sub usage(@) {
print STDERR @_, "\n" if (@_);
die "Usage: $0 [options] [input_files...]
If input_file is missing, input is read from stdin.
Assuming inputs are one or more 'vw' progress reports
Requires R to generate the chart
Options:
-v Verbose/debug
-d No display (only creates image file)
-a Convert from squared-loss X to abs-loss (apply sqrt(X))
-p Convert from squared-loss X to abs-percent (100*sqrt(X))
-Q Convert from squared-loss X to (exp(sqrt(X)) - 1.0) * 100
-q Convert from quantile-loss X to (exp(X) - 1.0) * 100
-l Use since-last (2nd) instead of average-loss (1st) column
Note: this option ignores the final avg loss
-o<IMG> Output chart to <IMG> file
-x<XL> Use <XL> as X-axis label in chart
-y<YL> Use <YL> as Y-axis label in chart
-t<T> Use <T> as title of chart
-w<W> set image width in pixels (default $DefaultWidth)
-h<W> set image height in pixels (default $DefaultHeight)
-o <image_file> is optional, if not given, will create a temp-file:
$TmpImgFile
and display it.
";
}
#
# Map from squared-error to %percent error
#
sub sqlosslog2pct($) {
my $arg = shift;
# return 0 unless (defined $arg);
(exp(sqrt($arg)) - 1.0) * 100;
}
#
# Map from quantile-error to %percent error
#
sub losslog2pct($) {
my $arg = shift;
# return 0 unless (defined $arg);
(exp($arg) - 1.0) * 100;
}
sub vector_max(@) {
my $max = 0; # all our loss values are non-negative
foreach my $val (@_) {
$max = $val if (defined($val) && $val > $max);
}
$max;
}
sub transform_loss($) {
my $loss = shift;
return losslog2pct($loss) if ($opt_q); # quantile on log scale
return sqlosslog2pct($loss) if ($opt_Q); # squared on log scale
return sqrt($loss) if ($opt_a); # squared to absolute
return (100.0*sqrt($loss)) if ($opt_p);
$loss;
}
#
# input: vw progress (can concatenate multiple) from STDIN or file
# output: a list of vectors of avg loss values
#
my $YMax = 0;
sub average_loss_arrays() {
my @avg_losses_array = ();
my @avg_loss = ();
my $gap = 0;
while (<>) {
# progress lines
my ($n1, $n2) = (/^([0-9]+\.[0-9]+)\s+([0-9]+\.[0-9]+)/);
if (defined $n1) {
push(@avg_loss, transform_loss($opt_l ? $n2 : $n1));
next;
}
# summary line
if (! $opt_l && /^average loss\s*=\s*([0-9.e+-]+)/i) {
push(@avg_loss, transform_loss($1));
if (@avg_loss) {
push(@avg_losses_array, [ @avg_loss ]);
$Ymax = vector_max($Ymax, @avg_loss);
}
@avg_loss = ();
$gap = 0;
next;
}
# 2 empty lines or more are also a data-set separator
if (/^\s*$/ && ++$gap > 1) {
if (@avg_loss) {
push(@avg_losses_array, [ @avg_loss ]);
$Ymax = vector_max($Ymax, @avg_loss);
}
@avg_loss = ();
$gap = 0;
next;
}
}
# if summary line wasn't included, and we have avg_loss data,
# use whatever we have
if (@avg_loss) {
$Ymax = vector_max($Ymax, @avg_loss);
push(@avg_losses_array, [ @avg_loss ]);
@avg_loss = ();
}
usage("Couldn't identify 'vw' progress report(s) in input")
unless (@avg_losses_array);
v("=== Found %d progress runs in input\n", scalar(@avg_losses_array));
@avg_losses_array;
}
sub do_plot($;$) {
my ($loss_vec_ref, $imgfile) = @_;
my $set_r_device_line =
($imgfile =~ /\.e?ps$/) ?
"postscript(file='$imgfile', width=$opt_w, height=$opt_h)"
: ($imgfile =~ /\.jpg$/) ?
"jpeg(file='$imgfile', width=$opt_w, height=$opt_h)"
: # default: if you don't have 'Cairo', just use 'png' instead
"library(graphics); autoload('CairoPNG', 'Cairo');\n" .
"if (exists('Cairo', mode='function')) {\n" .
"CairoPNG(file='$imgfile', width=$opt_w, height=$opt_h)\n" .
"} else {\n" .
"png(file='$imgfile', width=$opt_w, height=$opt_h)\n" .
"}"
;
my $R_input = "$set_r_device_line;\n";
my $lineno = 0;
my @pchs = ();
$R_input .= "colors = colors()[c(26, 554, 257, 115, 644, 132, 92)]\n";
$R_input .= "col.list = vector()\n";
foreach my $lossref (@$loss_vec_ref) {
my $R_losses_array = sprintf('c(%s)', join(',', @$lossref));
$R_input .= "loss = $R_losses_array\n" .
"color = colors[1 + ($lineno %% length(colors))]\n";
$R_input .=
($lineno == 0) ?
"par(mar=c(5,6,3.5,1))\n" .
"plot(loss, pch=20, t='o', col=color, lwd=2, cex=1.3,\n" .
"\tlab=c(20,20,7), xlab='$opt_x', ylab='$opt_y\n', las=1,\n" .
"\tylim=c(0, $Ymax), cex.lab=1.5, font.lab=4,\n" .
"\tpanel.first=grid(col='gray63'));\n"
:
"lines(loss, pch=20, t='o', col=color, lwd=2, cex=1.3);\n";
$R_input .= "col.list[1+$lineno] = color\n";
push(@namelist, sprintf("%.4f", $lossref->[-1]));
push(@pchs, 20);
$lineno++;
}
$R_input .= "title(main='$opt_t', cex.main=1.5, col='black')\n";
$R_input .= sprintf(
"legend('topright', " .
"legend=c(%s), col=col.list, text.col=col.list, pch=c(%s), %s);\n",
join(',', @namelist),
join(',', @pchs),
"inset=0.05, title='final mean $LossStr', pt.cex=2, lwd=2.5"
);
my $verbosity = $opt_v ? '--verbose' : '--slave -q --silent 2>/dev/null';
open(RIN, "|R --vanilla --no-readline $verbosity");
print RIN $R_input;
close RIN;
# For debugging
if ($opt_v) {
open(RSAV, ">$TmpRFile") || die "$0: $TmpRFile: $!\n";
print RSAV $R_input;
close RSAV;
v("Wrote R file for debug: $TmpRFile\n");
}
}
sub get_args() {
$0 =~ s{.*/}{};
getopts('dx:y:t:w:h:laqQpo:vi:') || usage();
$opt_l = 0 unless ((defined $opt_l) && $opt_l > 0);
$LossStr = ($opt_Q || $opt_p || $opt_q) ? '%loss' : 'loss';
$LossStr .= ' since last' if ($opt_l);
$opt_x = 'vw progress iteration (log-scale)' unless (defined $opt_x);
$opt_y = "mean $LossStr"
unless (defined $opt_y);
$opt_t = "online training $opt_y convergence" unless (defined $opt_t);
$opt_w = $DefaultWidth unless (defined $opt_w);
$opt_h = $DefaultHeight unless (defined $opt_h);
$opt_o = $TmpImgFile
unless (defined $opt_o);
unlink($TmpImgFile) if (-e $TmpImgFile);
my @file_args = ();
foreach my $arg (@ARGV) {
if ($arg =~ /\.(?:png|jpe?g)$/) {
$opt_o = $arg;
next;
}
if (-f $arg) {
push(@file_args, $arg);
} else {
usage("$0: $arg: $!");
}
}
@ARGV = @file_args;
if (-e $opt_o && $opt_o ne $TmpImgFile) {
warn "$0: image file '$opt_o' already exists: moving to .prev\n";
rename($opt_o, "$opt_o.prev") ||
die "$0: rename($opt_o, $opt_o.prev) failed: $!\n";
}
}
sub display($) {
my $imgfile = $_[0];
find_image_viewer();
die "$0: display($imgfile): $! - must be a bug\n"
unless (-e $imgfile);
if ($opt_d) {
# User requested no display,
# Be friendly, give a hint that everything is OK
printf STDERR "image file is: %s\n", $imgfile;
return;
}
# Postscript files are generated in portrait: need 90-degrees rotation
# 'display' supports a rotate arg, YMMV
my $rotate = ($imgfile =~ /ps$/ && $DisplayProg =~ /display$/)
? '-rotate 90'
: '';
$opt_i = '' unless (defined $opt_i);
system("$DisplayProg $opt_i $rotate $imgfile");
}
# -- main
get_args();
my @losses = average_loss_arrays();
do_plot(\@losses, $opt_o);
display($opt_o);