-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
154 lines (137 loc) · 4.66 KB
/
test.py
File metadata and controls
154 lines (137 loc) · 4.66 KB
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
import keras
from keras.models import *
import training_data
import tokenizer
import word2vec
import numpy as np
from keras.preprocessing.sequence import pad_sequences
import argparse
import os
import json
from metrics import *
import tensorflow as tf
def predictOutput(input_dir, output_dir, type_model):
""" Makes prediction
Parameters
----------
input_dir : string
path of input directory, where instances.jsonl is located
output_dir : string
path of output dir, where result.jsonl is written
type_model : string
name of the model
"""
def prec(y_true, y_pred):
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
precision = true_positives / (predicted_positives + K.epsilon())
return precision
def rec(y_true, y_pred):
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
recall = true_positives / (possible_positives + K.epsilon())
return recall
def f_one(y_true, y_pred, beta=1):
if beta < 0:
raise ValueError(
'The lowest choosable beta is zero (only precision).')
# If there are no true positives, fix the F score at 0 like sklearn.
if K.sum(K.round(K.clip(y_true, 0, 1))) == 0:
return 0
p = prec(y_true, y_pred)
r = rec(y_true, y_pred)
bb = beta**2
fbeta_score = (1 + bb) * (p * r) / (bb * p + r + K.epsilon())
return fbeta_score
for fil in os.listdir(input_dir):
if fil == "instances.jsonl":
print(fil)
instances = input_dir + "/" + fil
print(instances)
"""
Read test datas
"""
datas = training_data.getTestData(instances)
"""
Clean test datas
"""
datas = tokenizer.special_char(datas, "", test=True)
"""
Tokenize test datas
"""
tokens, post_pos, pos, ids = tokenizer.tokenizer(datas)
"""
Vectorize words
"""
vect_dataset = word2vec.vectorizeDataset(tokens)
"""
Vectorize linguistic features
"""
ling_feat = word2vec.lingFeatures(post_pos)
"""
Load models
"""
dir = "longTraining/models/"
if type_model == "FullNet":
model_path = dir + "fullNet/mse0.032553340215849914_accuracy0.842464196462473_dropout0.5_momentum0.0_lrate0.3_gru128.hdf5"
if type_model == "FullNetPost":
model_path = dir + "fullNetConc/mse0.032816953588701375_accuracy0.8392816549757715_dropout0.2_momentum0.0_lrate0.3_gru128.hdf5"
if type_model == "LingNet":
model_path = dir + "lingNet/mse0.040141911279121346_accuracy0.8167765401498543_dropout0.0_momentum0.0_lrate0.4_gru128.hdf5"
if type_model == "WordEmbNet":
model_path = dir + "weNet/mse0.03307730689472056_accuracy0.8388270061784073_dropout0.5_momentum0.0_lrate0.3_gru128.hdf5"
model = keras.models.load_model(
model_path, custom_objects={
'prec': prec,
'rec': rec,
'f_one': f_one
})
keras_model = "./" + model_path
saver = tf.train.Saver()
sess = keras.backend.get_session()
saver.restore(sess, keras_model)
"""
Prepare data to be passed to the network
"""
x_LING = ling_feat
x_LING = (np.array(x_LING)).astype(np.float32)
x_LING_exp = np.expand_dims(x_LING, axis=2)
x_TR = vect_dataset
x_TR = (pad_sequences(
x_TR,
maxlen=x_LING.shape[1],
dtype='int32',
padding='post',
truncating='post',
value=0.0)).astype(np.float32)
x_CONCAT = np.concatenate((x_TR, x_LING_exp), axis=2)
""" Predict
"""
if type_model == "FullNet":
outputPred = model.predict(x_CONCAT)
if type_model == "FullNetPost":
outputPred = model.predict([x_TR, x_LING])
if type_model == "LingNet":
outputPred = model.predict(x_LING_exp)
if type_model == "WordEmbNet":
outputPred = model.predict(x_TR)
"""
Write results
"""
with open(
os.path.join(output_dir, "results.jsonl"), 'w',
encoding="utf-8") as output:
for i in range(len(ids)):
output.write(
json.dumps({
"id": ids[i],
"clickbaitScore": float(outputPred[i])
}) + '\n')
def __main__():
parser = argparse.ArgumentParser()
parser.add_argument('-i', dest="input_dir")
parser.add_argument('-o', dest="output_dir")
parser.add_argument('-m', dest="type_model")
args = parser.parse_args()
predictOutput(args.input_dir, args.output_dir, args.type_model)
__main__()