-
Notifications
You must be signed in to change notification settings - Fork 50
/
eval_pred.py
190 lines (160 loc) · 8.42 KB
/
eval_pred.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#! /usr/bin/env python
# -*- coding: UTF-8 -*-
"""
$ ~/anaconda3/bin/python eval_pred.py --evaluate --checkpoint_dir="./runs/1523240176/checkpoints/"
$ ~/anaconda3/bin/python eval_pred.py --predict --checkpoint_dir="./runs/1523240176/checkpoints/"
"""
import numpy as np
import pandas as pd
import os
import time
import csv
import yaml
import datetime
import tensorflow as tf
from tensorflow.contrib import learn
from sklearn import metrics
import jieba
import jieba.posseg as pseg
import data_helpers
def zh_tokenizer(iterator):
for value in iterator:
yield list(jieba.cut(value, cut_all=False))
def softmax(x):
"""Compute softmax values for each sets of scores in x."""
if x.ndim == 1:
x = x.reshape((1, -1))
max_x = np.max(x, axis=1).reshape((-1, 1))
exp_x = np.exp(x - max_x)
return exp_x / np.sum(exp_x, axis=1).reshape((-1, 1))
with open("config.yml", 'r') as ymlfile:
cfg = yaml.load(ymlfile)
# Parameters
# ==================================================
# Data Parameters
tf.flags.DEFINE_string("model_type", "clf", "The type of model, classification or regression (default: clf)") # clf/reg
# Evaluating Parameters
tf.flags.DEFINE_integer("batch_size", 64, "Batch Size (default: 64)")
tf.flags.DEFINE_string("checkpoint_dir", "", "Checkpoint directory from training run")
tf.flags.DEFINE_boolean("evaluate", False, "Evaluate on all training data")
tf.flags.DEFINE_boolean("predict", False, "Predict on test dataset")
# Misc Parameters
tf.flags.DEFINE_boolean("allow_soft_placement", True, "Allow device soft device placement")
tf.flags.DEFINE_boolean("log_device_placement", False, "Log placement of ops on devices")
FLAGS = tf.flags.FLAGS
FLAGS._parse_flags()
print("\nParameters:")
for attr, value in sorted(FLAGS.__flags.items()):
print("{}={}".format(attr.upper(), value))
print("")
# CHANGE THIS: Load data. Load your own evaluating set or testing set here
datasets = None
dataset_name = cfg["datasets"]["default"]
if FLAGS.evaluate:
if dataset_name == "mrpolarity":
datasets = data_helpers.get_datasets_mrpolarity(cfg["datasets"][dataset_name]["positive_data_file"]["path"],
cfg["datasets"][dataset_name]["negative_data_file"]["path"])
elif dataset_name == "20newsgroup":
datasets = data_helpers.get_datasets_20newsgroup(subset="test",
categories=cfg["datasets"][dataset_name]["categories"],
shuffle=cfg["datasets"][dataset_name]["shuffle"],
random_state=cfg["datasets"][dataset_name]["random_state"])
elif dataset_name == "financenews":
datasets = data_helpers.get_datasets_financenews(cfg["datasets"][dataset_name]["path"])
elif dataset_name == "scoringdocuments":
datasets = data_helpers.get_datasets_scoringdocuments(cfg["datasets"][dataset_name]["path"])
if FLAGS.model_type == 'clf':
x_raw, y_test = data_helpers.load_data_labels(datasets)
y_test = np.argmax(y_test, axis=1)
elif FLAGS.model_type == 'reg':
x_raw, y_test = data_helpers.load_data_label(datasets)
elif FLAGS.predict:
if dataset_name == "mrpolarity":
datasets = {"target_names": ['positive_examples', 'negative_examples']}
x_raw = ["a masterpiece four years in the making", "everything is off."]
y_test = None
elif dataset_name == "20newsgroup":
datasets = {"target_names": ['alt.atheism', 'comp.graphics', 'sci.med', 'soc.religion.christian']}
x_raw = ["The number of reported cases of gonorrhea in Colorado increased",
"I am in the market for a 24-bit graphics card for a PC"]
y_test = None
elif dataset_name == "financenews":
datasets = {"target_names": ['strong_neg_examples', 'weak_neg_examples', 'neutral_examples', 'weak_pos_examples', 'strong_pos_examples']}
datasets = data_helpers.get_datasets_financenews_test(cfg["datasets"][dataset_name]["test_path"])
x_raw = data_helpers.load_data(datasets)
y_test = None
elif dataset_name == "scoringdocuments":
datasets = {"target_names": ['document_score']}
datasets = data_helpers.get_datasets_scoringdocuments_test(cfg["datasets"][dataset_name]["test_path"])
x_raw = data_helpers.load_data(datasets)
y_test = None
# Map data into vocabulary
vocab_path = os.path.join(FLAGS.checkpoint_dir, "..", "vocab")
vocab_processor = learn.preprocessing.VocabularyProcessor.restore(vocab_path)
x_test = np.array(list(vocab_processor.transform(x_raw)))
print("\nPredicting...\n")
# Evaluation
# ==================================================
checkpoint_file = tf.train.latest_checkpoint(FLAGS.checkpoint_dir)
graph = tf.Graph()
with graph.as_default():
session_conf = tf.ConfigProto(
allow_soft_placement=FLAGS.allow_soft_placement,
log_device_placement=FLAGS.log_device_placement)
sess = tf.Session(config=session_conf)
with sess.as_default():
# Load the saved meta graph and restore variables
saver = tf.train.import_meta_graph("{}.meta".format(checkpoint_file))
saver.restore(sess, checkpoint_file)
# Get the placeholders from the graph by name
input_x = graph.get_operation_by_name("input_x").outputs[0]
# input_y = graph.get_operation_by_name("input_y").outputs[0]
dropout_keep_prob = graph.get_operation_by_name("dropout_keep_prob").outputs[0]
# Tensors we want to evaluate
scores = graph.get_operation_by_name("output/scores").outputs[0]
# Tensors we want to evaluate
predictions = graph.get_operation_by_name("output/predictions").outputs[0]
# Generate batches for one epoch
batches = data_helpers.batch_iter(list(x_test), FLAGS.batch_size, 1, shuffle=False)
# Collect the predictions here
all_predictions = []
all_probabilities = None
for index, x_test_batch in enumerate(batches):
batch_predictions_scores = sess.run([predictions, scores], {input_x: x_test_batch, dropout_keep_prob: 1.0})
all_predictions = np.concatenate([all_predictions, batch_predictions_scores[0]])
if FLAGS.model_type == 'clf':
probabilities = softmax(batch_predictions_scores[1])
elif FLAGS.model_type == 'reg':
probabilities = batch_predictions_scores[1]
if all_probabilities is not None:
all_probabilities = np.concatenate([all_probabilities, probabilities])
else:
all_probabilities = probabilities
time_str = datetime.datetime.now().isoformat()
print("{}: step {}".format(time_str, (index+1)*FLAGS.batch_size))
# Print accuracy if y_test is defined
if y_test is not None and FLAGS.model_type == 'clf':
y_test = y_test[:len(y_test)-len(y_test)%FLAGS.batch_size]
correct_predictions = float(sum(all_predictions == y_test))
print("Total number of test examples: {}".format(len(y_test)))
print("Accuracy: {:g}".format(correct_predictions/float(len(y_test))))
print(metrics.classification_report(y_test, all_predictions, target_names=datasets['target_names']))
print(metrics.confusion_matrix(y_test, all_predictions))
# Save the evaluation result or testing result to a csv
x_raw = x_raw[:len(x_raw)-len(x_raw)%FLAGS.batch_size]
if FLAGS.model_type == 'clf':
predictions_human_readable = np.column_stack((np.array(x_raw),
[int(prediction)+1 for prediction in all_predictions],
["{}".format(probability) for probability in all_probabilities]))
predict_results = pd.DataFrame(predictions_human_readable, columns=['Content','Label','Probabilities'])
elif FLAGS.model_type == 'reg':
predictions_human_readable = np.column_stack((np.array(x_raw),
["{}".format(prediction) for prediction in all_predictions],
[probability[0] for probability in all_probabilities]))
predict_results = pd.DataFrame(predictions_human_readable, columns=['Content','Value','Score'])
if FLAGS.evaluate:
out_path = os.path.join(FLAGS.checkpoint_dir, "..", "evaluation.csv")
elif FLAGS.predict:
out_path = os.path.join(FLAGS.checkpoint_dir, "..", "prediction.csv")
print("Saving evaluation to {0}".format(out_path))
predict_results.to_csv(out_path, index=False)