text_html_output_1.png
from keras.layers import Dropout, Activation, Flatten, \

from keras.layers import LSTM, GRU, SimpleRNN

from keras.models import Sequential,Model,load_model

from sklearn.model_selection import KFold

from sklearn.model_selection import train_test_split,cross_val_score, cross_val_predict, KFold, GridSearchCV

from transformers import XLMRobertaTokenizer, TFAutoModel, TFAutoModelForSequenceClassification, TFXLMRobertaModel, XLMRobertaModel

import contractions

import matplotlib.pyplot as plt

import matplotlib.pyplot as plt

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 re

train_df = pd.read_csv('/kaggle/input/nlp-getting-started/train.csv')
test_df = pd.read_csv('/kaggle/input/nlp-getting-started/test.csv')

train_df.shape

nan_count = train_df.isna().sum()
nan_count

train_df.drop(['location'], axis=1)

train_df.duplicated(['text', 'target']).sum()

train_df = train_df.drop_duplicates(['text', 'target'])

train_df.shape

def remove_mentions(data_df):
    mentions_removed = re.sub('@[A-Za-z0-9_]+', '', data_df)
    return mentions_removed

def remove_hashtags(data_df):
    hashtags_removed = re.sub('#[A-Za-z0-9_]+', '', data_df)
    return hashtags_removed

def remove_urls(data_df):
    hashtags_removed = re.sub('http[s]?://(?:[a-zA-Z]|[0-9]|[\n]|[$-_@.&+\\]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', '', data_df)
    return hashtags_removed

def convert_contractions(data_df):
    contractions_converted = contractions.fix(data_df)
    return contractions_converted

train_df['text'] = train_df['text'].astype(str)
train_df['mentions_removed'] = train_df['text'].apply(remove_mentions).tolist()
train_df['hashtags_removed'] = train_df['mentions_removed'].apply(remove_hashtags).tolist()
train_df['url_removed'] = train_df['hashtags_removed'].apply(remove_urls).tolist()
train_df['lower_cased'] = train_df['url_removed'].apply(lambda x: x.lower())
train_df['contractions_converted'] = train_df['lower_cased'].apply(convert_contractions).tolist()

MODEL_TYPE = 'xlm-roberta-large'
xlm_tokenizer = XLMRobertaTokenizer.from_pretrained(MODEL_TYPE)
xlm_model = TFAutoModel.from_pretrained(MODEL_TYPE)

embedding_matrix = xlm_model.get_input_embeddings().weight

tokenized_feature_raw = xlm_tokenizer.batch_encode_plus(train_df['contractions_converted'], add_special_tokens=True)
token_sentence_length = [len(x) for x in tokenized_feature_raw['input_ids']]
avg_length = sum(token_sentence_length) / train_df.shape[0]
MAX_LEN = max(token_sentence_length)
import matplotlib.pyplot as plt
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)

max_len = round(max(token_sentence_length))
max_len

tokenized_feature = xlm_tokenizer.batch_encode_plus(train_df['contractions_converted'], add_special_tokens=True, padding='max_length', truncation=True, max_length=max_len, return_attention_mask=True, return_tensors='tf')

padded_inputs = tokenized_feature['input_ids']

train_padded_docs = np.array(padded_inputs)
labels = np.array(train_df['target'])

def create_rnn_model(input_shape):
    model_xlm = Sequential()
    model_xlm.add(Embedding(250002, 1024, trainable=False, weights=[embedding_matrix.numpy()]))
    model_xlm.add(GRU(512, return_sequences=True))
    model_xlm.add(GRU(512, return_sequences=True))
    model_xlm.add(GlobalMaxPool1D())
    model_xlm.add(Dense(30, activation='relu'))
    model_xlm.add(Dropout(0.4))
    model_xlm.add(Dense(1, activation='sigmoid'))
    return model_xlm

kfold = KFold(n_splits=10, shuffle=True, random_state=42)

model = create_rnn_model(X_train_xlm.shape[1:])
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
batch_size = 64
epochs = 10
history_model_xlm = model.fit(X_train_xlm, y_train_xlm, validation_split=0.2, batch_size=batch_size, epochs=epochs)

y_pred = (model.predict(X_test_xlm) > 0.5).astype(int)

for train_index, test_index in kfold.split(train_padded_docs):
    X_train, X_test = (train_padded_docs[train_index], train_padded_docs[test_index])
    y_train, y_test = (labels[train_index], labels[test_index])
    model = create_rnn_model(X_train.shape[1:])
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
    model.fit(X_train, y_train, epochs=10, batch_size=64, verbose=0)
    loss, accuracy = model.evaluate(X_test, y_test, verbose=0)

tokenized_feature_test_data = xlm_tokenizer.batch_encode_plus(test_df['contractions_converted'], add_special_tokens=True, padding='max_length', truncation=True, max_length=max_len, return_attention_mask=True, return_tensors='tf')

padded_inputs_test = tokenized_feature_test_data['input_ids']

predictions = (model.predict(padded_inputs_test) > 0.5).astype(int)

sample_submission = pd.read_csv('/kaggle/input/nlp-getting-started/sample_submission.csv')
sample_submission['target'] = predictions

sample_submission