-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·63 lines (44 loc) · 1.42 KB
/
main.py
File metadata and controls
executable file
·63 lines (44 loc) · 1.42 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
import gensim
from gensim.utils import tokenize
import numpy as np
from keras.layers import LSTM, Dropout, Dense
from keras.models import Sequential
print('Loading w2v...')
w2v = gensim.models.KeyedVectors.load_word2vec_format('./model/GoogleNews-vectors-negative300.bin', binary=True)
timesteps = 400
dimensions = 300
batch_size = 32
epochs_number = 1
print "Creating the model..."
model = Sequential()
model.add(LSTM(200, input_shape=(timesteps, dimensions), return_sequences=False))
model.add(Dropout(0.2))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss="binary_crossentropy", optimizer='rmsprop', metrics=['accuracy'])
fname = './model/lstm2.h5'
model.load_weights(fname)
def predict(text):
x = np.zeros((1, timesteps, 300), dtype=np.float32)
tokens = tokenize(text)
mj = 0
for w in tokens:
if(mj < timesteps):
try:
x[0][mj] = w2v.word_vec(w)
mj += 1
except:
continue
else:
break
return model.predict(x)
while True:
input = raw_input("Enter text: ")
if input == "exit":
exit()
prediction = predict(input)[0][0]
pred_word = "POSITIVE"
percentage = prediction * 100.0
if round(prediction) == 0:
pred_word = "NEGATIVE"
percentage = 100 - percentage
print('My prediction is that this text is {}. I am {:.2f}% sure.'.format(pred_word, percentage))