print("Building features...")
    feature_mapper = idf.IDFeatures(train, mode=mode)
    #feature_mapper = exfc2.ExtendedFeatures(train,mode=mode)
    feature_mapper.build_features()

    #pdb.set_trace()

    print("Init Struc Perceptron")
    epochs = 5
    learning_rate = 1
    reg = 0  # extended feat
    #reg = 0.001
    sp = spc.StructuredPerceptron(train.x_dict,
                                  train.y_dict,
                                  feature_mapper,
                                  num_epochs=epochs,
                                  learning_rate=learning_rate,
                                  reg_param=reg)
    print("Training...")
    sp.train_supervised_bigram(train)

    print("Predicting...")
    print("::Training...")
    pred_train = sp.viterbi_decode_corpus_bigram(train)
    print("::Valalidation...")
    pred_val = sp.viterbi_decode_corpus_bigram(val)
    print("::Testing...")
    pred_test = sp.viterbi_decode_corpus_bigram(test)

    print("Evaluating...")
    eval_train = sp.evaluate_corpus(train, pred_train)
Ejemplo n.º 2
0
pred_dev = crf_online.viterbi_decode_corpus(dev_seq)
pred_test = crf_online.viterbi_decode_corpus(test_seq)
eval_train = crf_online.evaluate_corpus(train_seq, pred_train)
eval_dev = crf_online.evaluate_corpus(dev_seq, pred_dev)
eval_test = crf_online.evaluate_corpus(test_seq, pred_test)

print "CRF - Extended Features Accuracy Train: %.3f Dev: %.3f Test: %.3f"%(eval_train, eval_dev,eval_test)



print "Perceptron Exercise"

feature_mapper = idfc.IDFeatures(train_seq)
feature_mapper.build_features()

sp = spc.StructuredPerceptron(corpus.word_dict, corpus.tag_dict, feature_mapper)
sp.num_epochs = 20
sp.train_supervised(train_seq)

pred_train = sp.viterbi_decode_corpus(train_seq)
pred_dev = sp.viterbi_decode_corpus(dev_seq)
pred_test = sp.viterbi_decode_corpus(test_seq)
eval_train = sp.evaluate_corpus(train_seq, pred_train)
eval_dev = sp.evaluate_corpus(dev_seq, pred_dev)
eval_test = sp.evaluate_corpus(test_seq, pred_test)

print "Structured Perceptron - ID Features Accuracy Train: %.3f Dev: %.3f Test: %.3f"%(eval_train,eval_dev,eval_test)


feature_mapper = exfc.ExtendedFeatures(train_seq)
feature_mapper.build_features()