forked from kk7nc/HDLTex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HDLTex.py
109 lines (90 loc) · 4.81 KB
/
HDLTex.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
import os
os.environ['KERAS_BACKEND'] = 'tensorflow'
#os.environ["THEANO_FLAGS"] = "mode=FAST_RUN"
import numpy as np
from keras.models import Sequential
import Data_helper
import BuildModel
if __name__ == "__main__":
#os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
#os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
MEMORY_MB_MAX = 1600000 # maximum memory you can use
MAX_SEQUENCE_LENGTH = 500 # Maximum sequance lenght 500 words
MAX_NB_WORDS = 55000 # Maximum number of unique words
EMBEDDING_DIM = 100 #embedding dimension you can change it to {25, 100, 150, and 300} but need to change glove version
batch_size_L1 = 64 # batch size in Level 1
batch_size_L2 = 64 # batch size in Level 2
epochs = 1
L1_model =0 # 0 is DNN, 1 is CNN, and 2 is RNN for Level 1
L2_model =2 # 0 is DNN, 1 is CNN, and 2 is RNN for Level 2
np.set_printoptions(threshold=np.inf)
'''
location of input data in two ways
1: Tokenizer that is using GLOVE
1: loadData that is using couting words or tf-idf
'''
X_train, y_train, X_test, y_test, content_L2_Train, L2_Train, content_L2_Test, L2_Test, number_of_classes_L2,word_index, embeddings_index,number_of_classes_L1 = \
Data_helper.loadData_Tokenizer(MAX_NB_WORDS,MAX_SEQUENCE_LENGTH)
X_train_DNN, y_train_DNN, X_test_DNN, y_test_DNN, content_L2_Train_DNN, L2_Train_DNN, content_L2_Test_DNN, L2_Test_DNN, number_of_classes_L2_DNN = Data_helper.loadData()
print("Loading Data is Done")
#######################DNN Level 1########################
if L1_model == 0:
print('Create model of DNN')
model = BuildModel.buildModel_DNN(X_train_DNN.shape[1], number_of_classes_L1, 8, 64, dropout=0.25)
model.fit(X_train_DNN, y_train_DNN[:, 0],
validation_data=(X_test_DNN, y_test_DNN[:, 0]),
epochs=epochs,
verbose=2,
batch_size=batch_size_L1)
#######################CNN Level 1########################
if L1_model == 1:
print('Create model of CNN')
model = BuildModel.buildModel_CNN(word_index, embeddings_index,number_of_classes_L1,MAX_SEQUENCE_LENGTH,EMBEDDING_DIM,1)
model.fit(X_train, y_train[:,0],
validation_data=(X_test, y_test[:,0]),
epochs=epochs,
verbose=2,
batch_size=batch_size_L1)
#######################RNN Level 1########################
if L1_model == 2:
print('Create model of RNN')
model = BuildModel.buildModel_RNN(word_index, embeddings_index,number_of_classes_L1,MAX_SEQUENCE_LENGTH,EMBEDDING_DIM)
model.fit(X_train, y_train[:,0],
validation_data=(X_test, y_test[:,0]),
epochs=epochs,
verbose=2,
batch_size=batch_size_L1)
HDLTex = [] # Level 2 models is list of Deep Structure
######################DNN Level 2################################
if L2_model == 0:
for i in range(0, number_of_classes_L1):
print('Create Sub model of ',i)
HDLTex.append(Sequential())
HDLTex[i] = BuildModel.buildModel_DNN(content_L2_Train_DNN[i].shape[1], number_of_classes_L2_DNN[i],2, 1024, dropout=0.5)
HDLTex[i].fit(content_L2_Train_DNN[i], L2_Train_DNN[i],
validation_data=(content_L2_Test_DNN[i], L2_Test_DNN[i]),
epochs=epochs,
verbose=2,
batch_size=batch_size_L2)
######################CNN Level 2################################
if L2_model == 1:
for i in range(0, number_of_classes_L1):
print('Create Sub model of ', i)
HDLTex.append(Sequential())
HDLTex[i] = BuildModel.buildModel_CNN(word_index, embeddings_index,number_of_classes_L2[i],MAX_SEQUENCE_LENGTH,EMBEDDING_DIM,1)
HDLTex[i].fit(content_L2_Train[i], L2_Train[i],
validation_data=(content_L2_Test[i], L2_Test[i]),
epochs=epochs,
verbose=2,
batch_size=batch_size_L2)
######################RNN Level 2################################
if L2_model == 2:
for i in range(0, number_of_classes_L1):
print('Create Sub model of ', i)
HDLTex.append(Sequential())
HDLTex[i] = BuildModel.buildModel_RNN(word_index, embeddings_index,number_of_classes_L2[i],MAX_SEQUENCE_LENGTH,EMBEDDING_DIM)
HDLTex[i].fit(content_L2_Train[i], L2_Train[i],
validation_data=(content_L2_Test[i], L2_Test[i]),
epochs=epochs,
verbose=2,
batch_size=batch_size_L2)