This repository has been archived by the owner on Jan 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnaacl_evaluate.py
executable file
·66 lines (51 loc) · 2.22 KB
/
naacl_evaluate.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
#!/usr/bin/env python3
import corpus
import evaluate
import features
import utils
from keras.models import load_model
from keras import backend as kerasbackend
from keras.preprocessing.text import Tokenizer
from keras.utils import to_categorical
# Global configuration
MAX_SENTENCE_LENGTH = 50
EMBEDDING_DIM = 300
KERAS_BATCH_SIZE = 32
WEIGHT_SMOOTHING = 0.0
print('Loading Word Embeddings')
# embeddings = features.Magnitudes()
# embeddings = features.Word2Vec()
embeddings = features.DummyEmbeddings(dimensions=EMBEDDING_DIM)
# Generate test Corpus object and get word embeddings for it
c_test = corpus.VUAMC('source/vuamc_corpus_test.csv', 'source/verb_tokens_test.csv', 'source/vuamc_corpus_test_pos.csv', mode='test')
c_test.validate_corpus()
# Sentences, Labels, POS Tags - I could just use better variable names
x_test, y_test, z_pos = features.generate_input_and_labels(c_test.sentences, Vectors=embeddings)
# POS Tags to numerical sequences
pos_tokenizer = Tokenizer()
pos_tokenizer.fit_on_texts(z_pos)
pos_sequences = pos_tokenizer.texts_to_sequences(z_pos)
z_test = to_categorical(pos_sequences)
# TODO: Needs to come from the train I assume
class_weights = list(utils.get_class_weights(c_test.label_list, WEIGHT_SMOOTHING).values())
print('loss_weight {}'.format(class_weights))
# Load model and Embeddings
model = load_model('naacl_metaphor.h5',
custom_objects={
'loss': utils.weighted_categorical_crossentropy(class_weights),
'f1': utils.f1,
'precision': utils.precision,
'recall': utils.recall
})
# Generate list of label predictions for each sentence
float_predictions = model.predict(x_test, batch_size=KERAS_BATCH_SIZE)
binary_predictions = kerasbackend.argmax(float_predictions)
label_predictions = kerasbackend.eval(binary_predictions)
# Write prediction to CSV file
predictions_file = 'predictions.csv'
standard_file = 'source/verb_tokens_test_gold_labels.csv'
# Evaluate and print result
rows = evaluate.corpus_evaluation(c_test, label_predictions, MAX_SENTENCE_LENGTH)
evaluate.csv_evalutation(rows, predictions_file)
results = evaluate.precision_recall_f1(predictions_file, standard_file)
print(results)