Skip to content

Commit 3ce4254

Browse files
authored
Update README.rst
1 parent 30b1478 commit 3ce4254

1 file changed

Lines changed: 259 additions & 0 deletions

File tree

README.rst

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1946,6 +1946,265 @@ Hierarchical Attention Networks
19461946
Recurrent Convolutional Neural Networks (RCNN)
19471947
---------------------------------------------
19481948

1949+
ecurrent Convolutional Neural Networks (RCNN) is used for text classification. The main idea of this technique is capturing contextual information with the recurrent structure and constructs the representation of text using a convolutional neural network. This architecture is a combination of RNN and CNN to use advantages of both technique in a model.
1950+
1951+
1952+
1953+
import packages:
1954+
1955+
.. code:: python
1956+
1957+
from keras.preprocessing import sequence
1958+
from keras.models import Sequential
1959+
from keras.layers import Dense, Dropout, Activation
1960+
from keras.layers import Embedding
1961+
from keras.layers import GRU
1962+
from keras.layers import Conv1D, MaxPooling1D
1963+
from keras.datasets import imdb
1964+
from sklearn.datasets import fetch_20newsgroups
1965+
import numpy as np
1966+
from sklearn import metrics
1967+
from keras.preprocessing.text import Tokenizer
1968+
from keras.preprocessing.sequence import pad_sequences
1969+
1970+
1971+
1972+
Convert text to word embedding (Using GloVe):
1973+
1974+
.. code:: python
1975+
1976+
def loadData_Tokenizer(X_train, X_test,MAX_NB_WORDS=75000,MAX_SEQUENCE_LENGTH=500):
1977+
np.random.seed(7)
1978+
text = np.concatenate((X_train, X_test), axis=0)
1979+
text = np.array(text)
1980+
tokenizer = Tokenizer(num_words=MAX_NB_WORDS)
1981+
tokenizer.fit_on_texts(text)
1982+
sequences = tokenizer.texts_to_sequences(text)
1983+
word_index = tokenizer.word_index
1984+
text = pad_sequences(sequences, maxlen=MAX_SEQUENCE_LENGTH)
1985+
print('Found %s unique tokens.' % len(word_index))
1986+
indices = np.arange(text.shape[0])
1987+
# np.random.shuffle(indices)
1988+
text = text[indices]
1989+
print(text.shape)
1990+
X_train = text[0:len(X_train), ]
1991+
X_test = text[len(X_train):, ]
1992+
embeddings_index = {}
1993+
f = open("C:\\Users\\kamran\\Documents\\GitHub\\RMDL\\Examples\\Glove\\glove.6B.50d.txt", encoding="utf8")
1994+
for line in f:
1995+
values = line.split()
1996+
word = values[0]
1997+
try:
1998+
coefs = np.asarray(values[1:], dtype='float32')
1999+
except:
2000+
pass
2001+
embeddings_index[word] = coefs
2002+
f.close()
2003+
print('Total %s word vectors.' % len(embeddings_index))
2004+
return (X_train, X_test, word_index,embeddings_index)
2005+
2006+
2007+
.. code:: python
2008+
2009+
def Build_Model_RCNN_Text(word_index, embeddings_index, nclasses, MAX_SEQUENCE_LENGTH=500, EMBEDDING_DIM=50):
2010+
2011+
kernel_size = 2
2012+
filters = 256
2013+
pool_size = 2
2014+
gru_node = 256
2015+
2016+
embedding_matrix = np.random.random((len(word_index) + 1, EMBEDDING_DIM))
2017+
for word, i in word_index.items():
2018+
embedding_vector = embeddings_index.get(word)
2019+
if embedding_vector is not None:
2020+
# words not found in embedding index will be all-zeros.
2021+
if len(embedding_matrix[i]) !=len(embedding_vector):
2022+
print("could not broadcast input array from shape",str(len(embedding_matrix[i])),
2023+
"into shape",str(len(embedding_vector))," Please make sure your"
2024+
" EMBEDDING_DIM is equal to embedding_vector file ,GloVe,")
2025+
exit(1)
2026+
2027+
embedding_matrix[i] = embedding_vector
2028+
2029+
2030+
2031+
model = Sequential()
2032+
model.add(Embedding(len(word_index) + 1,
2033+
EMBEDDING_DIM,
2034+
weights=[embedding_matrix],
2035+
input_length=MAX_SEQUENCE_LENGTH,
2036+
trainable=True))
2037+
model.add(Dropout(0.25))
2038+
model.add(Conv1D(filters, kernel_size, activation='relu'))
2039+
model.add(MaxPooling1D(pool_size=pool_size))
2040+
model.add(Conv1D(filters, kernel_size, activation='relu'))
2041+
model.add(MaxPooling1D(pool_size=pool_size))
2042+
model.add(Conv1D(filters, kernel_size, activation='relu'))
2043+
model.add(MaxPooling1D(pool_size=pool_size))
2044+
model.add(Conv1D(filters, kernel_size, activation='relu'))
2045+
model.add(MaxPooling1D(pool_size=pool_size))
2046+
model.add(LSTM(gru_node, return_sequences=True, recurrent_dropout=0.2))
2047+
model.add(LSTM(gru_node, return_sequences=True, recurrent_dropout=0.2))
2048+
model.add(LSTM(gru_node, return_sequences=True, recurrent_dropout=0.2))
2049+
model.add(LSTM(gru_node, recurrent_dropout=0.2))
2050+
model.add(Dense(1024,activation='relu'))
2051+
model.add(Dense(nclasses))
2052+
model.add(Activation('softmax'))
2053+
2054+
model.compile(loss='sparse_categorical_crossentropy',
2055+
optimizer='adam',
2056+
metrics=['accuracy'])
2057+
2058+
return model
2059+
2060+
2061+
.. code:: python
2062+
2063+
newsgroups_train = fetch_20newsgroups(subset='train')
2064+
newsgroups_test = fetch_20newsgroups(subset='test')
2065+
X_train = newsgroups_train.data
2066+
X_test = newsgroups_test.data
2067+
y_train = newsgroups_train.target
2068+
y_test = newsgroups_test.target
2069+
2070+
X_train_Glove,X_test_Glove, word_index,embeddings_index = loadData_Tokenizer(X_train,X_test)
2071+
2072+
2073+
Run RCNN :
2074+
2075+
2076+
.. code:: python
2077+
2078+
2079+
model_RCNN = Build_Model_CNN_Text(word_index,embeddings_index, 20)
2080+
2081+
2082+
model_RCNN.summary()
2083+
2084+
model_RCNN.fit(X_train_Glove, y_train,
2085+
validation_data=(X_test_Glove, y_test),
2086+
epochs=15,
2087+
batch_size=128,
2088+
verbose=2)
2089+
2090+
predicted = model_RCNN.predict(X_test_Glove)
2091+
2092+
predicted = np.argmax(predicted, axis=1)
2093+
print(metrics.classification_report(y_test, predicted))
2094+
2095+
2096+
summary of the model:
2097+
2098+
2099+
.. code:: python
2100+
2101+
_________________________________________________________________
2102+
Layer (type) Output Shape Param #
2103+
=================================================================
2104+
embedding_1 (Embedding) (None, 500, 50) 8960500
2105+
_________________________________________________________________
2106+
dropout_1 (Dropout) (None, 500, 50) 0
2107+
_________________________________________________________________
2108+
conv1d_1 (Conv1D) (None, 499, 256) 25856
2109+
_________________________________________________________________
2110+
max_pooling1d_1 (MaxPooling1 (None, 249, 256) 0
2111+
_________________________________________________________________
2112+
conv1d_2 (Conv1D) (None, 248, 256) 131328
2113+
_________________________________________________________________
2114+
max_pooling1d_2 (MaxPooling1 (None, 124, 256) 0
2115+
_________________________________________________________________
2116+
conv1d_3 (Conv1D) (None, 123, 256) 131328
2117+
_________________________________________________________________
2118+
max_pooling1d_3 (MaxPooling1 (None, 61, 256) 0
2119+
_________________________________________________________________
2120+
conv1d_4 (Conv1D) (None, 60, 256) 131328
2121+
_________________________________________________________________
2122+
max_pooling1d_4 (MaxPooling1 (None, 30, 256) 0
2123+
_________________________________________________________________
2124+
lstm_1 (LSTM) (None, 30, 256) 525312
2125+
_________________________________________________________________
2126+
lstm_2 (LSTM) (None, 30, 256) 525312
2127+
_________________________________________________________________
2128+
lstm_3 (LSTM) (None, 30, 256) 525312
2129+
_________________________________________________________________
2130+
lstm_4 (LSTM) (None, 256) 525312
2131+
_________________________________________________________________
2132+
dense_1 (Dense) (None, 1024) 263168
2133+
_________________________________________________________________
2134+
dense_2 (Dense) (None, 20) 20500
2135+
_________________________________________________________________
2136+
activation_1 (Activation) (None, 20) 0
2137+
=================================================================
2138+
Total params: 11,765,256
2139+
Trainable params: 11,765,256
2140+
Non-trainable params: 0
2141+
_________________________________________________________________
2142+
2143+
2144+
2145+
Output:
2146+
2147+
.. code:: python
2148+
2149+
Train on 11314 samples, validate on 7532 samples
2150+
Epoch 1/15
2151+
- 28s - loss: 2.6624 - acc: 0.1081 - val_loss: 2.3012 - val_acc: 0.1753
2152+
Epoch 2/15
2153+
- 22s - loss: 2.1142 - acc: 0.2224 - val_loss: 1.9168 - val_acc: 0.2669
2154+
Epoch 3/15
2155+
- 22s - loss: 1.7465 - acc: 0.3290 - val_loss: 1.8257 - val_acc: 0.3412
2156+
Epoch 4/15
2157+
- 22s - loss: 1.4730 - acc: 0.4356 - val_loss: 1.5433 - val_acc: 0.4436
2158+
Epoch 5/15
2159+
- 22s - loss: 1.1800 - acc: 0.5556 - val_loss: 1.2973 - val_acc: 0.5467
2160+
Epoch 6/15
2161+
- 22s - loss: 0.9910 - acc: 0.6281 - val_loss: 1.2530 - val_acc: 0.5797
2162+
Epoch 7/15
2163+
- 22s - loss: 0.8581 - acc: 0.6854 - val_loss: 1.1522 - val_acc: 0.6281
2164+
Epoch 8/15
2165+
- 22s - loss: 0.7058 - acc: 0.7428 - val_loss: 1.2385 - val_acc: 0.6033
2166+
Epoch 9/15
2167+
- 22s - loss: 0.6792 - acc: 0.7515 - val_loss: 1.0200 - val_acc: 0.6775
2168+
Epoch 10/15
2169+
- 22s - loss: 0.5782 - acc: 0.7948 - val_loss: 1.0961 - val_acc: 0.6577
2170+
Epoch 11/15
2171+
- 23s - loss: 0.4674 - acc: 0.8341 - val_loss: 1.0866 - val_acc: 0.6924
2172+
Epoch 12/15
2173+
- 23s - loss: 0.4284 - acc: 0.8512 - val_loss: 0.9880 - val_acc: 0.7096
2174+
Epoch 13/15
2175+
- 22s - loss: 0.3883 - acc: 0.8670 - val_loss: 1.0190 - val_acc: 0.7151
2176+
Epoch 14/15
2177+
- 22s - loss: 0.3334 - acc: 0.8874 - val_loss: 1.0025 - val_acc: 0.7232
2178+
Epoch 15/15
2179+
- 22s - loss: 0.2857 - acc: 0.9038 - val_loss: 1.0123 - val_acc: 0.7331
2180+
2181+
2182+
precision recall f1-score support
2183+
2184+
0 0.64 0.73 0.68 319
2185+
1 0.45 0.83 0.58 389
2186+
2 0.81 0.64 0.71 394
2187+
3 0.64 0.57 0.61 392
2188+
4 0.55 0.78 0.64 385
2189+
5 0.77 0.52 0.62 395
2190+
6 0.84 0.77 0.80 390
2191+
7 0.87 0.79 0.83 396
2192+
8 0.85 0.90 0.87 398
2193+
9 0.98 0.84 0.90 397
2194+
10 0.93 0.96 0.95 399
2195+
11 0.92 0.79 0.85 396
2196+
12 0.59 0.53 0.56 393
2197+
13 0.82 0.82 0.82 396
2198+
14 0.84 0.84 0.84 394
2199+
15 0.83 0.89 0.86 398
2200+
16 0.68 0.86 0.76 364
2201+
17 0.97 0.86 0.91 376
2202+
18 0.66 0.50 0.57 310
2203+
19 0.53 0.31 0.40 251
2204+
2205+
avg / total 0.77 0.75 0.75 7532
2206+
2207+
19492208
19502209
-----------------------------------------
19512210
Random Multimodel Deep Learning (RMDL)

0 commit comments

Comments
 (0)