-
Notifications
You must be signed in to change notification settings - Fork 2
/
prediction.py
90 lines (74 loc) · 2.89 KB
/
prediction.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
import ml
import tensorflow as tf
import numpy as np
import myconstants
from myconstants import get_constant, get_save_name, CLASS_LEN, CLASS_LEN_TEST
from ml.tf_training import sentence_mean
import gensim
def predict(sentence, classification, mode=myconstants.Mode.MODE_MEAN):
print 'your review is : %s' % sentence
sentence = ml.del_punctuations(sentence)
sentence = sentence.lower()
sentence = ml.del_stopwords(sentence)
while sentence == '':
sentence = raw_input("Meaningless sentence! Please enter another sentence :")
print 'your review is : %s' % sentence
sentence = ml.del_punctuations(sentence)
sentence = sentence.lower()
sentence = ml.del_stopwords(sentence)
print 'your key words for vector are : %s' % sentence
# load model
test = False
accuracy_path = get_constant('ACCURACY_PATH', test)
w2v_model_path = get_constant('W2V_MODEL_PATH', test)
d2v_model_path = get_constant('D2V_MODEL_PATH', test)
# load model
if mode == myconstants.Mode.MODE_PCA:
vec_dim = myconstants.Mode.PCA_COMPONENTS * myconstants.W2V_DIM
model = gensim.models.Word2Vec.load(w2v_model_path)
elif mode == myconstants.Mode.MODE_MEAN:
vec_dim = myconstants.W2V_DIM
model = gensim.models.Word2Vec.load(w2v_model_path)
elif mode == myconstants.Mode.MODE_D2V:
vec_dim = myconstants.D2V_DIM
model = gensim.models.Doc2Vec.load(d2v_model_path)
vector = np.array(sentence_mean(sentence, model))
vector.shape = [1, vec_dim]
sess = tf.Session()
x = tf.Variable(vector)
try:
W = tf.Variable(np.load(get_save_name(myconstants.NUMPY_W, mode=mode, classification=classification)))
b = tf.Variable(np.load(get_save_name(myconstants.NUMPY_B, mode=mode, classification=classification)))
except:
print 'you have not trained your model yet!'
y = tf.argmax(tf.nn.softmax(tf.matmul(x, W) + b), 1)
init = tf.initialize_all_variables()
sess.run(init)
result = sess.run(y)
total_class = classification
# import pdb; pdb.set_trace()
print 'predicted rating : ', total_class[result]
print ''
return total_class[result]
def form_classes(classes):
formed_classes = []
for cla in classes:
formed_classes.append([cla])
return formed_classes
classification = [[1,2,3],[4,5]]
# classification = [[1],[2],[3],[4],[5]]
mode = myconstants.Mode.MODE_MEAN
if __name__ == '__main__':
while 1:
sentence = raw_input("Please enter a sentence (Enter nothing to end prediction):")
if sentence == '':
print 'Prediction end.'
exit()
else:
temp_classes = classification
while 1:
if len(temp_classes) > 1:
p = predict(sentence, temp_classes, mode)
temp_classes = form_classes(p)
else:
break