-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_multi_setence.py
123 lines (107 loc) · 4 KB
/
test_multi_setence.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
from Cython.Shadow import inline
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import util
import numpy as np
import tensorflow as tf
wordsList = np.load('./training_data/wordsList.npy')
wordsList = wordsList.tolist()
wordsList = [word.decode('UTF-8') for word in wordsList]
wordVectors = np.load('./training_data/wordVectors.npy')
file_index = 3
#docmatrix = np.load('./training_data/idsMatrix.npy')
user_data_path = util.get_file_path('./user_data')
#print(user_data_path)
fill_null = []
for i in range(24):
fill_null.append([1,0])
user_data_path = util.get_file_path('./user_data')
user_comment = np.zeros((250), dtype='int32')
batchSize = 24
lstmUnits = 64
numClasses = 2
iterations = 50000
numDimensions = 300
maxSeqLength = 250
tf.reset_default_graph()
labels = tf.placeholder(tf.float32, [batchSize, numClasses])
input_data = tf.placeholder(tf.int32, [batchSize, maxSeqLength])
data = tf.Variable(tf.zeros([batchSize, maxSeqLength, numDimensions]),dtype=tf.float32)
data = tf.nn.embedding_lookup(wordVectors,input_data)
lstmCell = tf.contrib.rnn.BasicLSTMCell(lstmUnits)
lstmCell = tf.contrib.rnn.DropoutWrapper(cell=lstmCell, output_keep_prob=0.75)
value, _ = tf.nn.dynamic_rnn(lstmCell, data, dtype=tf.float32)
#the parameter we need to compute by the neural network
weight = tf.Variable(tf.truncated_normal([lstmUnits, numClasses]))
bias = tf.Variable(tf.constant(0.1, shape=[numClasses]))
value = tf.transpose(value, [1, 0, 2])
last = tf.gather(value, int(value.get_shape()[0]) - 1)
prediction = (tf.matmul(last, weight) + bias)
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=labels))
optimizer = tf.train.AdamOptimizer().minimize(loss)
sess = tf.InteractiveSession()
saver = tf.train.Saver()
saver.restore(sess, tf.train.latest_checkpoint('./model_path'))
ex_add = np.zeros(shape=(1, 250))
user_comments = np.array([])
com_num = 0
for i in range(len(user_data_path)):
if user_data_path[i][-3:] == 'txt' :
com_num += 1
with open(user_data_path[i]) as f:
indexCounter = 0
line = f.readline()
cleanedLine = util.cleanSentences(line)
print(cleanedLine)
split = cleanedLine.split()
for word in split:
try:
user_comment[indexCounter] = wordsList.index(word)
except ValueError:
user_comment[indexCounter] = 399999
indexCounter = indexCounter + 1
if com_num ==1 :
user_comments = user_comment[np.newaxis, :]
#print(user_comments.shape)
else:user_comments = np.vstack([user_comments, user_comment[np.newaxis, :]])
#print(user_comments.shape)
for i in range(24-com_num):
user_comments = np.vstack([user_comments, ex_add])
#print(user_comments.shape)
pred = sess.run(prediction, {input_data: user_comments, labels: fill_null})
pred_cls = np.argmax(pred, axis=1)
for i in range(com_num):
if pred_cls[i] == 0 :
print('thanks')
else:print('sorry')
'''
with open(user_data_path[file_index]) as f:
#print(user_data_path[1])
indexCounter = 0
line = f.readline()
cleanedLine = util.cleanSentences(line)
print(cleanedLine)
split = cleanedLine.split()
for word in split:
try:
user_comment[indexCounter] = wordsList.index(word)
except ValueError:
user_comment[indexCounter] = 399999 # Vector for unknown words
indexCounter = indexCounter + 1
ex_add = np.zeros(shape=(1, 250))
# print(ex_add)
user_comment = user_comment[np.newaxis, :]
# print(user_comment[0][0])
for i in range(23):
user_comment = np.vstack([user_comment, ex_add])
# print(user_comment)
pred = sess.run(prediction, {input_data: user_comment, labels: fill_null})
# print(pred)
pred_cls = pred
pred_cls = np.argmax(pred_cls, axis=1)
#print(pred_cls[0])
if (pred_cls[0] == 0):
print('thanks')
else:
print('sorry')
'''