def evaluate_latent_semparse(): print "======================================================================" print 'LATENT SEMANTIC PARSING' # Only (input, LF root node) pairs for this task; y[0][1] indexes # into the semantics of the root node: latent_semparse_train = [[x, y[0][1]] for x, y, d in semdata.sem_train] latent_semparse_test = [[x, y[0][1]] for x, y, d in semdata.sem_test] # To make this interesting, we add a rule of type-raising for # digits, so that derivations with the predicate neg in them have # multiple derivational paths leading to the same output: First, # every digit can now be introduced in its lifted form: for word in ('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'): crude_lexicon[word] += [('Q', 'lift(%s)' % i) for i in range(1, 10)] # The new rule reversing the order of application between U and # its N (qua Q): rules.append(['U', 'Q', 'N', (1, 0)]) # Semantics for lift: functions['lift'] = (lambda x: (lambda f: f(x))) # New grammar: gram = Grammar(crude_lexicon, rules, functions) # Now train with LatentSGD, where the output transformation is # one that grabs the root node: evaluate(phi=phi_sem, optimizer=LatentSGD, train=latent_semparse_train, test=latent_semparse_test, classes=gram.gen, T=10, eta=0.1, output_transform=(lambda y: y[0][1]))
def evaluate_latent_semparse(): print "======================================================================" print 'LATENT SEMANTIC PARSING' # Only (input, LF root node) pairs for this task; y[0][1] indexes # into the semantics of the root node: latent_semparse_train = [[x,y[0][1]] for x, y, d in semdata.sem_train] latent_semparse_test = [[x,y[0][1]] for x, y, d in semdata.sem_test] # To make this interesting, we add a rule of type-raising for # digits, so that derivations with the predicate neg in them have # multiple derivational paths leading to the same output: First, # every digit can now be introduced in its lifted form: for word in ('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'): crude_lexicon[word] += [('Q', 'lift(%s)' % i) for i in range(1,10)] # The new rule reversing the order of application between U and # its N (qua Q): rules.append(['U', 'Q', 'N', (1,0)]) # Semantics for lift: functions['lift'] = (lambda x : (lambda f : f(x))) # New grammar: gram = Grammar(crude_lexicon, rules, functions) # Now train with LatentSGD, where the output transformation is # one that grabs the root node: evaluate(phi=phi_sem, optimizer=LatentSGD, train=latent_semparse_train, test=latent_semparse_test, classes=gram.gen, T=10, eta=0.1, output_transform=(lambda y : y[0][1]))
def evaluate_evenodd(): """Evaluates all three feature functions using the generic evaluation interface in learning.py.""" print "======================================================================" print 'EVEN/ODD' for phi in (phi_empty_string, phi_last_word, phi_all_words): evaluate(phi=phi, optimizer=SGD, train=evenodd_train, test=evenodd_test, # Artificially a function of input x to handle the # structure prediction cases, where classes is GEN: classes=(lambda x : (EVEN, ODD)), T=10, eta=0.1)
def evaluate_semparse( u, lf, grammar ): # We give evaluate_semparse an utterance, an lf and a grammar as arguments so wen can use it for our interactive game """Evaluate the semantic parsing set-up, where we learn from and predict logical forms. The set-up is identical to the simple example in evenodd.py, except that classes is gram.gen, which creates the set of licit parses according to the crude grammar.""" print( "======================================================================" ) print("SEMANTIC PARSING") # Only (input, lf) pairs for this task: sem_utterance = [[u, lf, grammar.sem(lf)]] # This replaces semdata semparse_train = [[x, y] for x, y, d in sem_utterance] semparse_test = [[x, y] for x, y, d in sem_utterance] weights = evaluate( phi=phi_sem, # We let evaluate return the weights and store them optimizer=SGD, train=semparse_train, test=semparse_test, classes=grammar.gen, true_or_false=grammar. sem, # We want only lf with denotation True. To test that we give this additional argument to evaluate T=10, eta=0.1) return weights # We return the weights so that we can use it for removing unlikely rules from the lexicon
def evaluate_evenodd(): """Evaluates all three feature functions using the generic evaluation interface in learning.py.""" print "======================================================================" print 'EVEN/ODD' for phi in (phi_empty_string, phi_last_word, phi_all_words): evaluate( phi=phi, optimizer=SGD, train=evenodd_train, test=evenodd_test, # Artificially a function of input x to handle the # structure prediction cases, where classes is GEN: classes=(lambda x: (EVEN, ODD)), T=10, eta=0.1)
def evaluate_semparse(): """Evaluate the semantic parsing set-up, where we learn from and predict logical forms. The set-up is identical to the simple example in evenodd.py, except that classes is gram.gen, which creates the set of licit parses according to the crude grammar.""" print "======================================================================" print "SEMANTIC PARSING" # Only (input, lf) pairs for this task: semparse_train = [[x,y] for x, y, d in semdata.sem_train] semparse_test = [[x,y] for x, y, d in semdata.sem_test] evaluate(phi=phi_sem, optimizer=SGD, train=semparse_train, test=semparse_test, classes=gram.gen, T=10, eta=0.1)
def evaluate_semparse(): """Evaluate the semantic parsing set-up, where we learn from and predict logical forms. The set-up is identical to the simple example in evenodd.py, except that classes is gram.gen, which creates the set of licit parses according to the crude grammar.""" print "======================================================================" print "SEMANTIC PARSING" # Only (input, lf) pairs for this task: semparse_train = [[x, y] for x, y, d in semdata.sem_train] semparse_test = [[x, y] for x, y, d in semdata.sem_test] evaluate(phi=phi_sem, optimizer=SGD, train=semparse_train, test=semparse_test, classes=gram.gen, T=10, eta=0.1)
def evaluate_interpretive(): """Evaluate the interpretive set-up, where we learn from and predict denotations. The only changes from evaluate_semparse are that we use LatentSGD, and output_transform handles the mapping from the logical forms we create to denotations.""" print "======================================================================" print 'INTERPRETIVE' # Only (input, denotation) pairs for this task: interpretive_train = [[x,d] for x, y, d in semdata.sem_train] interpretive_test = [[x,d] for x, y, d in semdata.sem_test] evaluate(phi=phi_sem, optimizer=LatentSGD, train=interpretive_train, test=interpretive_test, classes=gram.gen, T=10, eta=0.1, output_transform=gram.sem)
def evaluate_interpretive(): """Evaluate the interpretive set-up, where we learn from and predict denotations. The only changes from evaluate_semparse are that we use LatentSGD, and output_transform handles the mapping from the logical forms we create to denotations.""" print "======================================================================" print 'INTERPRETIVE' # Only (input, denotation) pairs for this task: interpretive_train = [[x, d] for x, y, d in semdata.sem_train] interpretive_test = [[x, d] for x, y, d in semdata.sem_test] evaluate(phi=phi_sem, optimizer=LatentSGD, train=interpretive_train, test=interpretive_test, classes=gram.gen, T=10, eta=0.1, output_transform=gram.sem)
def evaluate_semparse( u, lfs, grammar, allparses ): # We give evaluate_semparse an utterance, an lf and a grammar as arguments so wen can use it for our interactive game print( "======================================================================" ) print("SEMANTIC PARSING") # Only (input, lf) pairs for this task: sem_utterance = [[u, lf, lf.semantic] for lf in lfs] # This replaces semdata semparse_train = [[x, y] for x, y, d in sem_utterance] semparse_test = [[x, y] for x, y, d in sem_utterance] weights = evaluate( phi=phi_sem, # We let evaluate return the weights and store them optimizer=SGD, train=semparse_train, test=semparse_test, classes=allparses, true_or_false=grammar. sem, # We want only lf with denotation True. To test that we give this additional argument to evaluate T=10, eta=0.1) #0.1 return weights # We return the weights so that we can use it for removing unlikely rules from the lexicon