-
Notifications
You must be signed in to change notification settings - Fork 10
/
3nn.py
37 lines (31 loc) · 1.35 KB
/
3nn.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
import json
import os
import gensim
import keras
import numpy as np
from utils import INPUTS_DIR, OUTPUTS_DIR, TRAINING_SET
ldamodel = gensim.models.ldamulticore.LdaMulticore.load(os.environ.get('LDA_MODEL', './models/lda/trained_lda.txt'))
dictionary = ldamodel.id2word
X = []
for i in TRAINING_SET:
with open(os.path.join(INPUTS_DIR, i)) as f:
d = json.load(f)
empty = np.zeros(len(dictionary))
for k, v in d.items():
empty[int(k)] = float(v)
X.append(empty)
X = np.array(X)
Y = []
for i in TRAINING_SET:
with open(os.path.join(OUTPUTS_DIR, i)) as f:
Y.append(json.load(f))
Y = np.array(Y)
dnnmodel = keras.models.Sequential()
dnnmodel.add(keras.layers.Dense(ldamodel.num_topics * 3, input_dim=len(dictionary), activation='tanh'))
dnnmodel.add(keras.layers.Dense(ldamodel.num_topics * 2, activation='tanh'))
dnnmodel.add(keras.layers.Dense(ldamodel.num_topics, activation='softmax'))
dnnmodel.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
dnnmodel.fit(X, Y, validation_split=0.2, nb_epoch=1000, callbacks=[
keras.callbacks.EarlyStopping(monitor='val_loss', patience=50, verbose=True, mode='auto'),
keras.callbacks.ModelCheckpoint(os.environ.get('_3NN_MODEL', './models/dnn/trained_3nn.txt'), monitor='val_loss', verbose=True, save_best_only=True, save_weights_only=False, mode='auto')
])