def backprop(self, params, sentence): """ Calculate delta values depending place in tree. """ #backprop doesn't need to calculate deltas for leaves if self.isLeaf(): return (W1,b1,W2,b2,Wlabel) = self.params W2top = W2[0:100,:] W2bottom = W2[100:200,:] W1left = W1[:,0:100] W1right = W1[:,100:200] leftWeight = float(self.c1.numLeaves() + 1)/(self.c1.numLeaves() + self.c2.numLeaves() + 2) rightWeight = float(self.c2.numLeaves() + 1)/(self.c1.numLeaves() + self.c2.numLeaves() + 2) if self.isRoot(): self.delta = -util.dtanh(self.a) * (leftWeight*W2top.transpose().dot(self.c1.p - self.c1Prime)+ rightWeight * W2bottom.transpose().dot(self.c2.p - self.c2Prime)) else: if self.isLeftChild(): properW = W1left else: properW = W1right self.delta = util.dtanh(self.a) * (properW.dot(self.parent.delta)) self.c1.backprop(params,sentence) self.c2.backprop(params,sentence)
def backpropAsRoot(self, params, sentence): """ Calculate delta values depending place in tree, pretending this node is the root and moving on down. """ (W1,b1,W2,b2,Wlabel) = self.params W2top = W2[0:100,:] W2bottom = W2[100:200,:] leftWeight = float(self.c1.numLeaves() + 1)/(self.c1.numLeaves() + self.c2.numLeaves() + 2) rightWeight = float(self.c2.numLeaves() + 1)/(self.c1.numLeaves() + self.c2.numLeaves() + 2) self.delta = -util.dtanh(self.a) * (leftWeight*W2top.transpose().dot(self.c1.p - self.c1Prime)+ rightWeight * W2bottom.transpose().dot(self.c2.p - self.c2Prime)) self.c1.backprop(params,sentence) self.c2.backprop(params,sentence)