-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentiment_classifier.py
109 lines (95 loc) · 4.79 KB
/
sentiment_classifier.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
# sentiment_classifier.py
import torch
import argparse
import sys
import time
import random
from models import *
from sentiment_data import *
from typing import List
####################################################
# DO NOT MODIFY THIS FILE IN YOUR FINAL SUBMISSION #
####################################################
def _parse_args():
"""
Command-line arguments to the system. --model switches between the main modes you'll need to use. The other arguments
are provided for convenience.
:return: the parsed args bundle
"""
parser = argparse.ArgumentParser(description='trainer.py', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--model', type=str, default='TRIVIAL', choices=('TRIVIAL', 'PERCEPTRON', 'FNN', 'RNN', 'MyNN'), help='model to run')
parser.add_argument('--feats', type=str, default='UNIGRAM', choices=('UNIGRAM', 'BETTER'), help='feats to use')
parser.add_argument('--learning_rate', type=float, default=1e-3, help='learning rate')
parser.add_argument('--epoch', type=int, default=20, help='epochs to train for')
parser.add_argument('--train_path', type=str, default='data/train.txt', help='path to train set (you should not need to modify)')
parser.add_argument('--dev_path', type=str, default='data/dev.txt', help='path to dev set (you should not need to modify)')
parser.add_argument('--blind_test_path', type=str, default='data/test-blind.txt', help='path to blind test set (you should not need to modify)')
parser.add_argument('--test_output_path', type=str, default='test-blind.output.txt', help='output path for test predictions')
parser.add_argument('--no_run_on_test', dest='run_on_test', default=True, action='store_false', help='skip printing output on the test set')
args = parser.parse_args()
return args
def evaluate(classifier, exs):
"""
Evaluates a given classifier on the given examples
:param classifier: classifier to evaluate
:param exs: the list of SentimentExamples to evaluate on
:return: None (but prints output)
"""
print_evaluation([ex.label for ex in exs], [classifier.predict(ex) for ex in exs])
def print_evaluation(golds: List[int], predictions: List[int]):
"""
Prints evaluation statistics comparing golds and predictions, each of which is a sequence of 0/1 labels.
Prints accuracy as well as precision/recall/F1 of the positive class, which can sometimes be informative if either
the golds or predictions are highly biased.
:param golds: gold labels
:param predictions: pred labels
:return:
"""
num_correct = 0
num_pos_correct = 0
num_pred = 0
num_gold = 0
num_total = 0
if len(golds) != len(predictions):
raise Exception("Mismatched gold/pred lengths: %i / %i" % (len(golds), len(predictions)))
for idx in range(0, len(golds)):
gold = golds[idx]
prediction = predictions[idx]
if prediction == gold:
num_correct += 1
if prediction == 1:
num_pred += 1
if gold == 1:
num_gold += 1
if prediction == 1 and gold == 1:
num_pos_correct += 1
num_total += 1
print("Accuracy: %i / %i = %f" % (num_correct, num_total, float(num_correct) / num_total))
prec = float(num_pos_correct) / num_pred if num_pred > 0 else 0.0
rec = float(num_pos_correct) / num_gold if num_gold > 0 else 0.0
f1 = 2 * prec * rec / (prec + rec) if prec > 0 and rec > 0 else 0.0
print("Precision (fraction of predicted positives that are correct): %i / %i = %f" % (num_pos_correct, num_pred, prec)
+ "; Recall (fraction of true positives predicted correctly): %i / %i = %f" % (num_pos_correct, num_gold, rec)
+ "; F1 (harmonic mean of precision and recall): %f" % f1)
if __name__ == '__main__':
random.seed(1)
torch.manual_seed(1)
args = _parse_args()
print(args)
# Load train, dev, and test exs and index the words.
train_exs = read_sentiment_examples(args.train_path)
dev_exs = read_sentiment_examples(args.dev_path)
test_exs_words_only = read_blind_sst_examples(args.blind_test_path)
print(repr(len(train_exs)) + " / " + repr(len(dev_exs)) + " / " + repr(len(test_exs_words_only)) + " train/dev/test examples")
# Train and evaluate
start_time = time.time()
model = train_model(args, train_exs, dev_exs)
print("=====Train Accuracy=====")
evaluate(model, train_exs)
print("=====Dev Accuracy=====")
evaluate(model, dev_exs)
print("Time for training and evaluation: %.2f seconds" % (time.time() - start_time))
# Write the test set output
if args.run_on_test:
test_exs_predicted = [SentimentExample(words, model.predict(SentimentExample(words, None))) for words in test_exs_words_only]
write_sentiment_examples(test_exs_predicted, args.test_output_path)