text_html_output_1.png
from keras.callbacks import EarlyStopping, ModelCheckpoint

from keras.layers import LSTM, Dense, TimeDistributed, Bidirectional, Embedding, Dropout, Flatten, Layer, Input

from keras.models import Sequential, Model

from keras.preprocessing.sequence import pad_sequences

from keras.preprocessing.text import Tokenizer

from sklearn import metrics

from sklearn.metrics import precision_recall_curve, auc, roc_auc_score, roc_curve, recall_score

import seaborn as sns

Ytrain = Ytrain.map({'positive': 1, 'negative': 0})
Ycv = Ycv.map({'positive': 1, 'negative': 0})

tokenizer = Tokenizer(num_words=20000, oov_token='<UNK>')
tokenizer.fit_on_texts(Xtrain)
word2num = tokenizer.word_index
num2word = {k: w for w, k in word2num.items()}
train_sequences = tokenizer.texts_to_sequences(Xtrain)
maxlen = max([len(x) for x in train_sequences])
train_padded = pad_sequences(train_sequences, padding='post', truncating='post', maxlen=100)
test_sequences = tokenizer.texts_to_sequences(Xcv)
test_padded = pad_sequences(test_sequences, padding='post', truncating='post', maxlen=100)

inp = Input(shape=(100,))
x = Embedding(20000, 256, trainable=False)(inp)
x = Bidirectional(LSTM(300, return_sequences=True, dropout=0.25, recurrent_dropout=0.25))(x)
x = Attention()(x)
x = Dense(256, activation='relu')(x)
x = Dropout(0.25)(x)
x = Dense(1, activation='sigmoid')(x)
model = Model(inputs=inp, outputs=x)
model.summary()

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
file_path = 'model.hdf5'
ckpt = ModelCheckpoint(file_path, monitor='val_loss', verbose=1, save_best_only=True, mode='min')
early = EarlyStopping(monitor='val_loss', mode='min', patience=1)

model.fit(train_padded, Ytrain, batch_size=1024, epochs=30, validation_data=(test_padded, Ycv), callbacks=[ckpt])

Ycv_pred0 = model.predict(test_padded)
Ycv_pred = (Ycv_pred0 > 0.5).astype('int64')
print('Accuracy :', metrics.accuracy_score(Ycv, Ycv_pred))
print('f1 score macro :', metrics.f1_score(Ycv, Ycv_pred, average='macro'))
print('f1 scoore micro :', metrics.f1_score(Ycv, Ycv_pred, average='micro'))
print('Hamming loss :', metrics.hamming_loss(Ycv, Ycv_pred))
fpr, tpr, thresh = roc_curve(Ycv, Ycv_pred0)
print('auc: ', auc(fpr, tpr))
print('Classification report: \n', metrics.classification_report(Ycv, Ycv_pred))
fig, ax = plt.subplots(figsize=[10, 7])
ax.set_title('Receiver Operating Characteristic trainning')
ax.plot(fpr, tpr, sns.xkcd_rgb['greenish cyan'])
ax.plot([0, 1], [0, 1], ls='--', c=sns.xkcd_rgb['red pink'])
ax.set_xlim([-0.01, 1.01])
ax.set_ylim([-0.01, 1.01])
ax.set_ylabel('True Positive Rate')
ax.set_xlabel('False Positive Rate')