Exemplo n.º 1
0
def rnnFromTree(tree, vocabulary, wordReduction=False, grammarBased=False):
    if tree.height() > 2:
        if grammarBased:
            cat = tree.label() + ' -> ' + ' '.join(
                [child.label() for child in tree])
        else:
            cat = 'composition'
        children = [
            rnnFromTree(child, vocabulary, wordReduction) for child in tree
        ]
        return NN.Node(children, cat, 'tanh')
    else:  #preterminal node
        words = tree.leaves()
        if len(words) == 1: word = words[0]
        else: 'Not exactly one leaf?!', tree
        try:
            index = vocabulary.index(word)
        except:
            index = 0
        leaf = NN.Leaf('wordIM', index, word)

        if wordReduction:
            # wordReduction adds an extra layer to reduce high-dimensional words
            # to the dimensionality of the inner representations
            if grammarBased: cat = tree.label()
            else: cat = 'preterminal'
            return NN.Node([leaf], cat, 'tanh')
        else:
            return leaf
Exemplo n.º 2
0
 def __init__(self, children, labels, fixed):
     #    print 'CLassifier.init', children
     if fixed:
         children = [NN.Leaf([], ('word', ), i) for i in range(children)]
     comparison = NN.Node(children, [self], ('comparison', ), 'ReLU')
     #    leafs = [NN.Leaf([comparison],('word',),i) for i in range(n)]
     #    comparison.inputs=leafs
     NN.Node.__init__(self, [comparison], [], ('classify', ), 'softmax')
     self.labels = labels