-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSampler.pm
203 lines (165 loc) · 4.33 KB
/
Sampler.pm
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
# Matt Post <[email protected]>
# This file contains the Sampler package, a base class defining
# features common to different Gibbs samplers on treebanks. It is
# general enough to support two subclasses: the TSG inference
# described in Post & Gildea (2008) [Sampler/TSG.pm], and work in
# progress for inducing grammars from raw text [Sampler/Learner.pm].
package Sampler;
use strict;
use Exporter;
use vars qw|$VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS|;
# our($lexicon,%rules,%deps,%PARAMS,$base_measure);
@ISA = qw|Exporter|;
@EXPORT = qw|compress_files decrement random_multinomial|;
@EXPORT_OK = ();
use strict;
use Carp qw|croak|;
use warnings;
use threads;
use threads::shared;
use POSIX qw|strftime|;
use List::Util qw|reduce min shuffle sum|;
# use Memoize;
my $basedir;
BEGIN {
$basedir = $ENV{DPTSG};
unshift @INC, $basedir;
}
# import common subtree operations and other support functions
use TSG;
# constructor
sub new {
my $class = shift;
my %params = @_;
# default values
my $self = {
iters => 100,
stop => 0.9,
rundir => $ENV{PWD},
verbosity => 1,
alpha => 1, # default alpha
};
map { $self->{$_} = $params{$_} } keys %params;
bless($self,$class);
return $self;
}
# sets the corpus (expects a list reference)
sub corpus {
my ($self,$corpus) = @_;
$self->{corpus} = $corpus;
}
sub count {
die "* FATAL: didn't override count()!";
}
my $debug = 0;
my $loghandle;
# sample_all
#
# Calls walk() on each tree in the corpus, passing along any functions
# that are given to it. This function is the entry point for the
# Gibbs sampler.
sub sample_all {
my ($self,$iter,@funcs) = @_;
$self->{iter} = $iter;
$self->{treeno} = 1;
$| = 1;
foreach my $tree (@{$self->{corpus}}) {
print "ITER $iter TREE $self->{treeno}\n"
if $self->{verbosity} and (! ($self->{treeno} % 1000));
walk($tree, \@funcs, $self);
$self->{treeno}++;
}
}
# decrement
#
# Decrements the value of a key in a hash, deleting the key if the
# count reaches 0. The deletion is done to save on memory, since the
# sampler may be creating and deleting many different keys. This
# proves essential to running within reasonable amounts of memory (and
# not consuming the machine's entire memory due to the long tail of
# potential subtrees).
sub decrement {
my ($hash,$key) = @_;
if (exists $hash->{$key}) {
$hash->{$key}--;
delete $hash->{$key} if (0 == $hash->{$key});
}
}
# rand_transition
#
# Make a binary decision based on a probability. The argument is the
# probability of TRUE.
sub rand_transition {
my $prob = shift;
return (rand() < $prob) ? 1 : 0;
}
# compress_files
#
# Uses bzip to compress a list of files (given in @_)
sub compress_files {
my $bzip = "/usr/bin/bzip2";
map { system("$bzip -9 -f $_") } @_;
}
# dump_corpus
#
# Dumps the corpus of trees to disk.
sub dump_corpus {
my ($self,$dir) = @_;
mkdir $dir unless -d $dir;
my @corpus = map { build_subtree_oneline($_,1) } @{$self->{corpus}};
my $file = "$dir/corpus";
open DUMP, ">$file" or warn "can't dump to $file";
map { print DUMP $_, $/ } @corpus;
close DUMP;
compress_files($file);
}
# dump_counts
#
# Dumps the event counts to disk. This must be defined on a
# class-by-class basis.
sub dump_counts {
print "* WARNING: dump_counts() not implemented!\n";
}
# check_counts
#
# This is called once per iteration, at the end of the iteration, and
# allows for sanity checks.
sub check_counts {
print "* WARNING: check_counts() not implemented\n";
}
# random_multinomial
#
# Receives a list of numbers and randomly chooses one according to
# their normalized frequencies.
sub random_multinomial {
my ($list) = @_;
my $len = scalar @$list;
my $total = sum(@$list);
my $prob = rand($total);
my $sum = 0.0;
my $which = 0;
for (;;) {
$sum += $list->[$which];
last if $sum > $prob;
$which++;
last if $which >= $len;
}
# print " RANDOM(" . join(",",map {$_/$total} @$list) . ") = $which\n";
return $which;
}
sub AUTOLOAD;
# sub AUTOLOAD {
# my ($self) = @_;
# my $type = ref ($self) || croak "$self is not an object";
# my $field = $AUTOLOAD;
# $field =~ s/.*://;
# unless (exists $self->{$field}) {
# croak "$field does not exist in object/class $type";
# }
# if (@_) {
# return $self->($name) = shift;
# } else {
# return $self->($name);
# }
# }
1;