forked from VowpalWabbit/vowpal_wabbit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logistic
executable file
·57 lines (45 loc) · 960 Bytes
/
logistic
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
#!/usr/bin/perl -w
#
# Replace vw linear regressor prediction by the logistic link
# Prediction/label is assumed to be the first item on each line
# Rest of line (if present) remains intact
#
use Getopt::Std;
use vars qw($opt_0 $opt_1);
#
# traditional logistic function: [0..1] range
#
sub logistic_01($) {
1.0 / (1.0 + exp(-$_[0]));
}
#
# logistic function mapped to [-1 .. 1] range
#
sub logistic_11($) {
2.0 / (1.0 + exp(-$_[0])) - 1.0;
}
my $LinkFunc = \&logistic_11;
sub usage {
die "Usage: $0 [Options] [files...]
Maps label x (1st number on line) to logistic(x)
Options:
-0 Use logistic(x) -> [0 .. 1] range
-1 Use logistic(x) -> [-1 .. 1] range (default)
";
}
sub init {
$0 =~ s{.*/}{};
usage() if (@ARGV == 0 && -t STDIN);
getopts('01');
if ($opt_0) {
$LinkFunc = \&logistic_01;
}
}
#
# -- main
#
init();
while (<>) {
s/^(\S+)/$LinkFunc->($1)/e;
print;
}