def buildModel_RNN(word_index, embeddings_index, nclasses, MAX_SEQUENCE_LENGTH=500, EMBEDDING_DIM=50, dropout=0.5): word_index in word index , embeddings_index is embeddings index, look at data_helper.py nClasses is number of classes, MAX_SEQUENCE_LENGTH is maximum lenght
(word_index, embeddings_index, nclasses, MAX_SEQUENCE_LENGTH=500, EMBEDDING_DIM=50, dropout=0.5)
| 43 | |
| 44 | |
| 45 | def Build_Model_RNN_Text(word_index, embeddings_index, nclasses, MAX_SEQUENCE_LENGTH=500, EMBEDDING_DIM=50, dropout=0.5): |
| 46 | """ |
| 47 | def buildModel_RNN(word_index, embeddings_index, nclasses, MAX_SEQUENCE_LENGTH=500, EMBEDDING_DIM=50, dropout=0.5): |
| 48 | word_index in word index , |
| 49 | embeddings_index is embeddings index, look at data_helper.py |
| 50 | nClasses is number of classes, |
| 51 | MAX_SEQUENCE_LENGTH is maximum lenght of text sequences |
| 52 | """ |
| 53 | |
| 54 | model = Sequential() |
| 55 | hidden_layer = 3 |
| 56 | gru_node = 256 |
| 57 | |
| 58 | embedding_matrix = np.random.random((len(word_index) + 1, EMBEDDING_DIM)) |
| 59 | for word, i in word_index.items(): |
| 60 | embedding_vector = embeddings_index.get(word) |
| 61 | if embedding_vector is not None: |
| 62 | # words not found in embedding index will be all-zeros. |
| 63 | if len(embedding_matrix[i]) != len(embedding_vector): |
| 64 | print("could not broadcast input array from shape", str(len(embedding_matrix[i])), |
| 65 | "into shape", str(len(embedding_vector)), " Please make sure your" |
| 66 | " EMBEDDING_DIM is equal to embedding_vector file ,GloVe,") |
| 67 | exit(1) |
| 68 | embedding_matrix[i] = embedding_vector |
| 69 | model.add(Embedding(len(word_index) + 1, |
| 70 | EMBEDDING_DIM, |
| 71 | weights=[embedding_matrix], |
| 72 | input_length=MAX_SEQUENCE_LENGTH, |
| 73 | trainable=True)) |
| 74 | |
| 75 | |
| 76 | print(gru_node) |
| 77 | for i in range(0,hidden_layer): |
| 78 | model.add(GRU(gru_node,return_sequences=True, recurrent_dropout=0.2)) |
| 79 | model.add(Dropout(dropout)) |
| 80 | model.add(GRU(gru_node, recurrent_dropout=0.2)) |
| 81 | #model.add(Dense(, activation='relu')) |
| 82 | model.add(Dense(nclasses, activation='softmax')) |
| 83 | |
| 84 | |
| 85 | model.compile(loss='sparse_categorical_crossentropy', |
| 86 | optimizer='adam', |
| 87 | metrics=['accuracy']) |
| 88 | return model |
| 89 | |
| 90 | |
| 91 |