-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcfg_tree.py
282 lines (249 loc) · 11.9 KB
/
pcfg_tree.py
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
'''
Created on May 14, 2013
Abstract class for Probabilistic Context Free Grammar Parse Tree
@author: gerdogan
'''
from treelib import Node, Tree
import numpy as np
import scipy.special as sp
from copy import deepcopy
class PCFG:
"""
Definition of Probabilistic Context Free Grammar
Look into rational_rules.py for a sample grammar specification
"""
def __init__(self, terminals, nonterminals, start_symbol, rules, prod_probabilities, terminating_rule_ids):
self.terminals = terminals
self.nonterminals = nonterminals
self.start_symbol = start_symbol
self.rules = rules
self.prod_probabilities = prod_probabilities
self.terminating_rule_ids = terminating_rule_ids
class ParseNode:
"""
Represents a node in parse tree
A simple class that contains the symbol from the
grammar and the index of the production rule used to generate
the children of the node
"""
def __init__(self, symbol='', rule=''):
self.symbol = symbol
self.rule = rule
def __str__(self):
return self.symbol + ' ' + repr(self.rule)
def __deepcopy(self):
return ParseNode(symbol=self.symbol, rule=self.rule)
class PCFGTree:
"""
PCFG Parse Tree for MCMC
Prior and acceptance probabilities defined according to
Reference: Goodman, N. D., Tenenbaum, J. B., Feldman, J., & Griffiths, T. L. (2008).
A rational analysis of rule-based concept learning. Cognitive science, 32(1), 108-54.
"""
def __init__(self, grammar, data=None, ll_params=None, initial_tree=None, maximum_depth=None):
"""
grammar: PCFG object that defines the grammar
data: Data, used for calculatng likelihood
ll_params: Likelihood specific parameters
"""
self.grammar = grammar
self.data = data
self.ll_params = ll_params
# subclasses may define a maximum allowed tree depth
# if there are no such definitions, we set a limit here
if hasattr(self, 'MAXIMUM_DEPTH') is False:
self.MAXIMUM_DEPTH = maximum_depth
if self.MAXIMUM_DEPTH is None:
self.MAXIMUM_DEPTH = 99
if initial_tree is None:
self.tree = self._get_random_tree(start=self.grammar.start_symbol, max_depth=self.MAXIMUM_DEPTH)
else:
self.tree = initial_tree
# available moves (proposals)
# subclasses may define their own moves, so we need to check if moves is alredy defined
if hasattr(self, 'moves') is False:
self.moves = [self.subtree_proposal]
# WARNING
# Note that we are calling self._prior() etc. here
# that means we are calling super class's methods
# this means that we need to initialize any information
# that are used in these methods before we call base
# class's init
self.prior = self._prior()
self.derivation_prob = self._derivation_prob()
self.likelihood = self._likelihood()
def _get_random_tree(self, start, max_depth=999):
"""
Returns a random tree from PCFG starting with symbol 'start'
depth: the maximum depth of tree
"""
t = Tree()
t.create_node(ParseNode(start,''))
# get ids of not expanded nonterminals in tree
nodes_to_expand, depth = self.__get_nodes_to_expand_and_depth(t)
while len(nodes_to_expand) > 0:
# for each non terminal, choose a random rule and apply it
for node in nodes_to_expand:
symbol = t[node].tag.symbol
# if tree exceeded the allowed depth, expand nonterminals
# using rules from terminating_rule_ids
if depth >= (max_depth-1):
# choose from rules for nonterminal from terminating_rule_ids
rhsix = np.random.choice(self.grammar.terminating_rule_ids[symbol], size=1)
else:
# choose from rules for nonterminal according to production probabilities
rhsix = np.random.choice(len(self.grammar.rules[symbol]), p=self.grammar.prod_probabilities[symbol], size=1)
t[node].tag.rule = rhsix[0] # index of production rule used when expanding this node
rhs = self.grammar.rules[symbol][rhsix[0]]
for s in rhs:
t.create_node(tag=ParseNode(s,''), parent=node)
nodes_to_expand, depth = self.__get_nodes_to_expand_and_depth(t)
return t
def __get_nodes_to_expand_and_depth(self, tree):
"""
Gets the nodes that should be expanded and the depth of
tree. Used by _get_random_tree
"""
nodes_to_expand = []
depths = {}
for node in tree.expand_tree(mode=Tree.WIDTH):
# if node is a nonterminal and it has no children, it should be expanded
if tree[node].tag.symbol in self.grammar.nonterminals and len(tree[node].fpointer)==0:
nodes_to_expand.append(node)
# if root, depth is 0
if tree[node].bpointer is None:
depths[node] = 0
else:
depths[node] = depths[tree[node].bpointer] + 1
return nodes_to_expand, max(depths.values())
def subtree_proposal_propose_tree(self):
"""
Proposes a new tree based on current state's tree
Chooses a non-terminal node randomly, prunes its subtree and
replaces it with a random new subtree
"""
proposed_state = deepcopy(self.tree)
nonterminal_nodes = [node for node in proposed_state.expand_tree(mode=Tree.WIDTH)
if proposed_state[node].tag.symbol in self.grammar.nonterminals]
chosen_node = np.random.choice(nonterminal_nodes)
chosen_symbol = proposed_state[chosen_node].tag.symbol
# get depth of current tree to find out the maximum allowed depth for
# proposed tree
ne, depth = self.__get_nodes_to_expand_and_depth(self.tree)
max_depth = self.MAXIMUM_DEPTH - depth if (self.MAXIMUM_DEPTH - depth)>0 else 1
new_subtree = self._get_random_tree(chosen_symbol, max_depth)
if chosen_node == proposed_state.root:
proposed_state = new_subtree
else:
# Tree.paste method does not care about the order of children
# and appends the new subtree as the last child to parent_node
# we want to paste the subtree to the exact location we pruned
# that's why we do not use Tree.paste method
parent_node_id = proposed_state[chosen_node].bpointer
parent_node = proposed_state[parent_node_id]
parent_child_location = parent_node.fpointer.index(chosen_node)
proposed_state.remove_node(chosen_node)
new_subtree[new_subtree.root].bpointer = parent_node_id
parent_node.fpointer.insert(parent_child_location, new_subtree.root)
proposed_state.nodes.update(new_subtree.nodes)
return proposed_state
def subtree_proposal(self):
"""
Propose new state based on current state using subtree move
Proposes a new tree using propose_tree function and
instantiates a new instance of PCFGTree with it,
then returns it and its acceptance probability
You should override this method if your state representation
contains extra data other than tree
"""
# propose new state
new_tree = self.subtree_proposal_propose_tree()
proposal = self.__class__(self.grammar, data=self.data, ll_params=self.ll_params, initial_tree=new_tree)
acc_prob = self._subtree_proposal_acceptance_probability(proposal)
return proposal, acc_prob
def _subtree_proposal_acceptance_probability(self, proposal):
# calculate acceptance probability
acc_prob = 1
nt_current = [node for node in self.tree.expand_tree(mode=Tree.WIDTH)
if self.tree[node].tag.symbol in self.grammar.nonterminals]
nt_proposal = [node for node in proposal.tree.expand_tree(mode=Tree.WIDTH)
if proposal.tree[node].tag.symbol in self.grammar.nonterminals]
acc_prob = acc_prob * proposal.prior * proposal.likelihood * len(nt_current) * self.derivation_prob
acc_prob = acc_prob / (self.prior * self.likelihood * len(nt_proposal) * proposal.derivation_prob)
return acc_prob
def _prior(self):
"""
Prior probability for state
Calculated using Eq. 13 in ref.
Here we marginalize out the production rule probabilities assuming
they are all uniform
"""
prior = 1.00
nonterminal_nr = [[self.tree[node].tag.symbol, self.tree[node].tag.rule] for node in self.tree.expand_tree(mode=Tree.WIDTH)
if self.tree[node].tag.symbol in self.grammar.nonterminals]
used_nonterminals = set([nr[0] for nr in nonterminal_nr])
for nt in used_nonterminals:
used_rules = [nr[1] for nr in nonterminal_nr if nr[0]==nt]
rule_counts = np.bincount(used_rules, minlength=len(self.grammar.rules[nt]))
prior = prior * self.__mult_beta(rule_counts + np.ones_like(rule_counts))
prior = prior / self.__mult_beta(np.ones_like(rule_counts))
return prior
def _derivation_prob(self):
"""
Probability of a state given grammar and production probabilities
Calculated using Eq. 3 in ref.
Here we do not marginalize out production rule probabilities.
This function is used for calculating acceptance probability.
"""
prob = 1.00
nonterminal_nr = [[self.tree[node].tag.symbol, self.tree[node].tag.rule] for node in self.tree.expand_tree(mode=Tree.WIDTH)
if self.tree[node].tag.symbol in self.grammar.nonterminals]
for nt, rule in nonterminal_nr:
prob = prob * self.grammar.prod_probabilities[nt][rule]
return prob
def _likelihood(self):
"""
Likelihood function
Should be overridden in super class
"""
pass
def __mult_beta(self, vect):
"""
Multinomial beta function (normalization term for Dirichlet)
mbeta(x,y,z) = gamma(x)*gamma(y)*gamma(z) / gamma(x+y+z)
"""
ret = 1.0
for i in vect:
ret = ret * sp.gamma(i)
ret = ret / sp.gamma(np.sum(vect))
return ret
def __eq__(self, other):
"""
Checks for equality between two trees
Override this in superclass if you want to
be able to check for equality between MCMC
states. This is useful for MCMSSampler class
to find samples with highest probability.
"""
pass
def __ne__(self, other):
"""
Checks for inequality between two trees
Override this in superclass if you want to
be able to check for equality between MCMC
states. This is useful for MCMSSampler class
to find samples with highest probability.
"""
pass
pass
def __repr__(self):
return "".join(self.tree[node].tag.symbol for node in self.tree.expand_tree(mode=Tree.DEPTH) if len(self.tree[node].fpointer) == 0)
def __str__(self):
return "".join(self.tree[node].tag.symbol for node in self.tree.expand_tree(mode=Tree.DEPTH) if len(self.tree[node].fpointer) == 0)
def __getstate__(self):
"""
Return data to be pickled.
moves cannot be pickled because it contains instancemethod objects, that's
why we remove it from data to be pickled
"""
return dict((k,v) for k, v in self.__dict__.iteritems() if k is not 'moves')