text_plain_output_1.png image_output_1.png
from keras.callbacks import EarlyStopping

from keras.layers import GRU, Input, Dense, Activation, RepeatVector, Bidirectional, LSTM, Dropout, Embedding

from keras.layers.embeddings import Embedding

from keras.preprocessing import sequence

from keras.preprocessing.text import Tokenizer

from keras.preprocessing.text import Tokenizer

from sklearn.metrics import confusion_matrix, classification_report, f1_score

from sklearn.model_selection import train_test_split,StratifiedKFold

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)

import tensorflow as tf

import pandas as pd
import numpy as np
import matplotlib as plt
import seaborn as sns
import tensorflow as tf
import keras
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.models import Model, Sequential
from keras.layers import GRU, Input, Dense, Activation, RepeatVector, Bidirectional, LSTM, Dropout, Embedding
from keras.layers.embeddings import Embedding
from sklearn.model_selection import train_test_split
from keras.losses import sparse_categorical_crossentropy
from keras.preprocessing.text import Tokenizer
from keras.preprocessing import sequence
from keras.callbacks import EarlyStopping
from sklearn.model_selection import train_test_split, StratifiedKFold
from sklearn.metrics import confusion_matrix, classification_report, f1_score
import collections
from tensorflow.python.client import device_lib
import matplotlib.pyplot as plt
import seaborn as sns
import re
import string
import emoji
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
SEED = 10

df = pd.read_csv('../input/nlp-getting-started/train.csv')
df_test = pd.read_csv('../input/nlp-getting-started/test.csv')
df.dropna(subset=['text'], inplace=True)
X = df['text']
y = df['target']
X_test = df_test['text']

def get_model():
    model = tf.keras.Sequential([Input(name='inputs', shape=[MAX_LEN]), Embedding(len(tok.word_index), 128), Bidirectional(tf.keras.layers.LSTM(128, return_sequences=True)), Bidirectional(tf.keras.layers.LSTM(64)), Dense(64, activation='relu'), Dropout(0.5), Dense(1, activation='sigmoid')])
    model.compile(loss=tf.keras.losses.BinaryCrossentropy(), optimizer=tf.keras.optimizers.Adam(0.0001), metrics=['accuracy'])
    return model

skf = StratifiedKFold(n_splits=5, random_state=SEED, shuffle=True)
CV_score_array = []
y_test_list = []
for fold, (train_idx, test_idx) in enumerate(skf.split(X, y)):
    print(f'Fold: {fold + 1},', end=' ')
    X_train, X_valid = (X[train_idx], X[test_idx])
    y_train, y_valid = (y[train_idx], y[test_idx])
    MAX_LEN = 50
    tok = Tokenizer()
    tok.fit_on_texts(X_train)
    sequences = tok.texts_to_sequences(X_train)
    valid_sequences = tok.texts_to_sequences(X_valid)
    test_sequences = tok.texts_to_sequences(X_test)
    X_train_seq = sequence.pad_sequences(sequences, maxlen=MAX_LEN)
    X_valid_seq = sequence.pad_sequences(valid_sequences, maxlen=MAX_LEN)
    X_test_seq = sequence.pad_sequences(test_sequences, maxlen=MAX_LEN)
    model = get_model()
    history = model.fit(X_train_seq, y_train, epochs=10, validation_data=(X_valid_seq, y_valid), batch_size=32, callbacks=[EarlyStopping(monitor='val_accuracy', mode='max', patience=3, verbose=False, restore_best_weights=True)])
    yhat_valid = np.where(model.predict(X_valid_seq) >= 0.5, 1, 0)
    f_score = f1_score(y_valid, yhat_valid)
    print('F1 Score: ' + str(f_score))
    print(classification_report(y_valid, yhat_valid))
    y_test_list.append(model.predict(X_test_seq))
    CV_score_array.append(f_score)
print('Average F1 Score 5 Folds: ' + str(np.array(CV_score_array).mean()))