text_plain_output_1.png
from keras.callbacks import EarlyStopping, ModelCheckpoint

from keras.layers import Conv1D, MaxPooling1D, GlobalMaxPool1D, Dropout, concatenate

from keras.layers import Dense, Embedding, Input,GRU

from keras.models import Model

from sklearn.model_selection import train_test_split

import numpy as np

import numpy as np # linear algebra

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

max_features = 20000
maxlen = 100

train = pd.read_csv('../input/train.csv')
test = pd.read_csv('../input/test.csv')
list_sentences_train = train['comment_text'].fillna('unknown').values
list_classes = ['toxic', 'severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate']
y = train[list_classes].values
list_sentences_test = test['comment_text'].fillna('unknown').values

def cnn_rnn():
    embed_size = 256
    inp = Input(shape=(maxlen,))
    main = Embedding(max_features, embed_size)(inp)
    main = Dropout(0.2)(main)
    main = Conv1D(filters=32, kernel_size=2, padding='same', activation='relu')(main)
    main = MaxPooling1D(pool_size=2)(main)
    main = Conv1D(filters=32, kernel_size=2, padding='same', activation='relu')(main)
    main = MaxPooling1D(pool_size=2)(main)
    main = GRU(32)(main)
    main = Dense(16, activation='relu')(main)
    main = Dense(6, activation='sigmoid')(main)
    model = Model(inputs=inp, outputs=main)
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
    model.summary()
    return model

model = cnn_rnn()
model.summary()

from sklearn.model_selection import train_test_split
any_category_positive = np.sum(y, 1)
X_t_train, X_t_test, y_train, y_test = train_test_split(X_t, y, test_size=0.1)

batch_size = 128
epochs = 3
file_path = 'model_best.h5'
checkpoint = ModelCheckpoint(file_path, monitor='val_loss', verbose=1, save_best_only=True, mode='min')
early = EarlyStopping(monitor='val_loss', mode='min', patience=20)
callbacks_list = [checkpoint, early]
model.fit(X_t_train, y_train, validation_data=(X_t_test, y_test), batch_size=batch_size, epochs=epochs, shuffle=True, callbacks=callbacks_list)
model.save('Whole_model.h5')