-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbot.py
68 lines (51 loc) · 1.99 KB
/
chatbot.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
""" Chatbot based on Recurrent Neural Nets and trained on OpenSubtitles.
Original inspiration from: http://arxiv.org/pdf/1506.05869v1.pdf
Usage:
chatbot.py <FILE>
Options:
<FILE> File containing parsed subtitles (e.g., data/lacollectioneuse.txt)
"""
import json
from docopt import docopt
from numpy import vstack, where
from keras.models import Sequential, model_from_json
from keras.optimizers import Adagrad
from keras.layers.core import Dense, Dropout, Activation, TimeDistributedDense
from keras.layers.embeddings import Embedding
from keras.layers.recurrent import LSTM
from keras.preprocessing import text
args = docopt(__doc__)
max_features = 512
tokenizer = text.Tokenizer(max_features) # keep top-1000 words
fh = open(args["<FILE>"], 'ro')
tokenizer.fit_on_texts(fh)
fh.seek(0) # reset the file pointer
X = tokenizer.texts_to_matrix(fh)
fh.close()
X_train = vstack((X[1:], X[:-1]))
Y_train = vstack((X[:-1], X[1:]))
print len(where(X_train[0] == 1)[0])
sys.exit
try:
fh = open('keras.model.json', 'rb')
model = model_from_json(fh.read())
fh.close()
except:
model = Sequential()
# Add a mask_zero=True to the Embedding connstructor if 0 is a left-padding value in your data
model.add(Embedding(max_features, 64))
model.add(LSTM(64, 64, activation='sigmoid', inner_activation='hard_sigmoid', return_sequences=True))
model.add(TimeDistributedDense(64, 64, activation='sigmoid',))
model.add(Dropout(0.5))
model.add(LSTM(64, 64, activation='sigmoid', inner_activation='hard_sigmoid'))
model.add(Dropout(0.5))
model.add(Dense(64, max_features))
model.add(Activation('sigmoid'))
adagrad = Adagrad(lr=0.01, epsilon=1e-6, clipnorm=1.)
model.compile(loss='binary_crossentropy', optimizer=adagrad)
fh = open('keras.model.json', 'wb')
fh.write(model.to_json())
fh.close()
model.fit(X_train, Y_train, batch_size=16, nb_epoch=3, verbose=1.)
print model.predict(X[:2], verbose=1.)
# score = model.evaluate(X_test, Y_test, batch_size=16)