Kaggle competition: Quora Insincere Questions Classification
import numpy as np
import pandas as pd
from pathlib import Path
from sklearn import preprocessing
import os
from timeit import default_timer
import tensorflow as tf
import tensorflow.keras as keras
import regex as re
import lightgbm as lgbm<load_from_csv>
np.random.seed(2018)
DATA_ROOT = Path(".. ")/ "/kaggle/input/sentiment-analysis-on-movie-reviews"
train = pd.read_csv(DATA_ROOT / 'train.tsv.zip', sep="\t")
 test = pd.read_csv(DATA_ROOT / 'test.tsv.zip', sep="\t")
 print(train.shape,test.shape)
 train.head()<feature_engineering>
embed_size = 300
max_features = 95000
maxlen = 70
def add_features(df):
df['nwords'] = df.Phrase.apply(lambda text: len(re.findall(r'\w+', text)))
 df['nchars'] = df.Phrase.apply(lambda text: len(text))<feature_engineering>
def show_timer() :
time_steps.append(time.time())
 last_step = time_steps[-1] - time_steps[-2]
total = time_steps[-1] - time_steps[0]
print('Last step time: {:.1f}(s)- Total time: {:.1f}(s)'.format(last_step, total))
add_features(test)
 add_features(train)
 test.head()<categorify>
def clean_text(x):
x = str(x)
 for punct in "/-'":
x = x.replace(punct, ' ')
 for punct in '&':
x = x.replace(punct, f' {punct} ')
 for punct in '?!.,"
x = x.replace(punct, '')
 return x
def clean_numbers(x):
x = re.sub('[0-9]{5,}', '
x = re.sub('[0-9]{4}', '
x = re.sub('[0-9]{3}', '
x = re.sub('[0-9]{2}', '
return x
def _get_mispell(mispell_dict):
mispell_re = re.compile('(%s)' % '|'.join(mispell_dict.keys()))
return mispell_dict, mispell_re
mispell_dict = {'colour':'color',
'centre':'center',
'didnt':'did not',
'doesnt':'does not',
'isnt':'is not',
'shouldnt':'should not',
'favourite':'favorite',
'travelling':'traveling',
'counselling':'counseling',
'theatre':'theater',
'cancelled':'canceled',
'labour':'labor',
'organisation':'organization',
'wwii':'world war 2',
'citicise':'criticize',
'instagram': 'social medium',
'whatsapp': 'social medium',
'snapchat': 'social medium'
}
mispellings, mispellings_re = _get_mispell(mispell_dict)
 def replace_typical_misspell(text):
def replace(match):
return mispellings[match.group(0)]
return mispellings_re.sub(replace, text )
TRANSFORMER_BATCH=128
def count_embedd(df):
idx_chunk=list(df.columns ).index('Phrase')
 embedd_lst = []
for index in range(0, df.shape[0], TRANSFORMER_BATCH):
embedds = model.encode(df.iloc[index:index+TRANSFORMER_BATCH, idx_chunk].values, show_progress_bar=False)
 embedd_lst.append(embedds)
 return np.concatenate(embedd_lst)
<train_model>
def pre_process(x):
x = clean_text(x)
 x = clean_numbers(x)
 x = replace_typical_misspell(x)
 return x
start_time = default_timer()
train_embedd = count_embedd(train)
 print("Train embeddings: {}: in: {:5.2f}s".format(train_embedd.shape, default_timer() - start_time))<train_model>
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/test.csv")
 print("Train shape : ",train_df.shape)
 print("Test shape : ",test_df.shape)
 train_df["question_text"] = train_df["question_text"].apply(lambda x: pre_process(x))
test_df["question_text"] = test_df["question_text"].apply(lambda x: pre_process(x))
train_X_all = train_df["question_text"].fillna("_
test_X = test_df["question_text"].fillna("_
tokenizer = Tokenizer(num_words=max_features)
 tokenizer.fit_on_texts(list(train_X_all))
train_X_all = tokenizer.texts_to_sequences(train_X_all)
 test_X = tokenizer.texts_to_sequences(test_X)
 word_index = tokenizer.word_index
train_X_all = pad_sequences(train_X_all, maxlen=maxlen)
 test_X = pad_sequences(test_X, maxlen=maxlen)
 train_y_all = train_df['target'].values
show_timer()
start_time = default_timer()
test_embedd = count_embedd(test)
 print("Test embeddings: {}: in: {:5.2f}s".format(test_embedd.shape, default_timer() - start_time)) 
<concatenate>
def load_and_prec(trn_idx, val_idx):
trn_idx = np.random.permutation(trn_idx)
 val_idx = np.random.permutation(val_idx)
 train_X = train_X_all[trn_idx]
val_X = train_X_all[val_idx]
train_y = train_y_all[trn_idx]
val_y = train_y_all[val_idx]
return train_X, val_X, test_X, train_y, val_y
X_train = np.array(train_embedd)
 X_test = np.array(test_embedd)
 X_train = np.concatenate(( X_train, train[['nchars','nwords']].values), axis=1)
 X_test = np.concatenate(( X_test, test[['nchars','nwords']].values), axis=1)
 X_train.shape, X_test.shape<categorify>
def load_glove(word_index):
EMBEDDING_FILE = '.. /input/embeddings/glove.840B.300d/glove.840B.300d.txt'
def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embeddings_index = dict(get_coefs(*o.split(" ")) for o in open(EMBEDDING_FILE))
all_embs = np.stack(embeddings_index.values())
 emb_mean,emb_std = all_embs.mean() , all_embs.std()
embed_size = all_embs.shape[1]
nb_words = min(max_features, len(word_index))
embedding_matrix = np.random.normal(emb_mean, emb_std,(nb_words, embed_size))
for word, i in word_index.items() :
if i >= max_features: continue
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None: embedding_matrix[i] = embedding_vector
return embedding_matrix
def load_fasttext(word_index):
EMBEDDING_FILE = '.. /input/embeddings/wiki-news-300d-1M/wiki-news-300d-1M.vec'
def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embeddings_index = dict(get_coefs(*o.split(" ")) for o in open(EMBEDDING_FILE)if len(o)>100)
 all_embs = np.stack(embeddings_index.values())
 emb_mean,emb_std = all_embs.mean() , all_embs.std()
embed_size = all_embs.shape[1]
nb_words = min(max_features, len(word_index))
embedding_matrix = np.random.normal(emb_mean, emb_std,(nb_words, embed_size))
for word, i in word_index.items() :
if i >= max_features: continue
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None: embedding_matrix[i] = embedding_vector
return embedding_matrix
def load_para(word_index):
EMBEDDING_FILE = '.. /input/embeddings/paragram_300_sl999/paragram_300_sl999.txt'
def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embeddings_index = dict(get_coefs(*o.split(" ")) for o in open(EMBEDDING_FILE, encoding="utf8", errors='ignore')if len(o)>100)
 all_embs = np.stack(embeddings_index.values())
 emb_mean,emb_std = all_embs.mean() , all_embs.std()
embed_size = all_embs.shape[1]
nb_words = min(max_features, len(word_index))
embedding_matrix = np.random.normal(emb_mean, emb_std,(nb_words, embed_size))
for word, i in word_index.items() :
if i >= max_features: continue
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None: embedding_matrix[i] = embedding_vector
return embedding_matrix
enc = preprocessing.OneHotEncoder()
label = train['Sentiment'].values.reshape(( -1,1))
enc.fit(label)
 y_train = enc.transform(label ).toarray()
y_train.shape<choose_model_class>
class Attention(Layer):
def __init__(self, step_dim,
W_regularizer=None, b_regularizer=None,
W_constraint=None, b_constraint=None,
bias=True, **kwargs):
self.supports_masking = True
self.init = initializers.get('glorot_uniform')
 self.W_regularizer = regularizers.get(W_regularizer)
 self.b_regularizer = regularizers.get(b_regularizer)
 self.W_constraint = constraints.get(W_constraint)
 self.b_constraint = constraints.get(b_constraint)
 self.bias = bias
self.step_dim = step_dim
self.features_dim = 0
super(Attention, self ).__init__(**kwargs)
 def build(self, input_shape):
assert len(input_shape)== 3
self.W = self.add_weight(( input_shape[-1],),
initializer=self.init,
name='{}_W'.format(self.name),
regularizer=self.W_regularizer,
constraint=self.W_constraint)
 self.features_dim = input_shape[-1]
if self.bias:
self.b = self.add_weight(( input_shape[1],),
initializer='zero',
name='{}_b'.format(self.name),
regularizer=self.b_regularizer,
constraint=self.b_constraint)
 else:
self.b = None
self.built = True
def compute_mask(self, input, input_mask=None):
return None
def call(self, x, mask=None):
features_dim = self.features_dim
step_dim = self.step_dim
eij = K.reshape(K.dot(K.reshape(x,(-1, features_dim)) ,
K.reshape(self.W,(features_dim, 1))),(-1, step_dim))
if self.bias:
eij += self.b
eij = K.tanh(eij)
 a = K.exp(eij)
 if mask is not None:
a *= K.cast(mask, K.floatx())
 a /= K.cast(K.sum(a, axis=1, keepdims=True)+ K.epsilon() , K.floatx())
 a = K.expand_dims(a)
 weighted_input = x * a
return K.sum(weighted_input, axis=1)
 def compute_output_shape(self, input_shape):
return input_shape[0], self.features_dim
KERAS_VALIDATION_SPLIT=0.05
KERAS_EPOCHS=10
KERAS_BATCH_SIZE=128
n_features=X_train.shape[1]
n_labels = y_train.shape[1]
start_time=default_timer()
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(2048, input_dim=n_features, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(n_labels, activation='softmax')
 ])
 LR=0.0001
adam = keras.optimizers.Adam(learning_rate=LR, beta_1=0.9, beta_2=0.999, amsgrad=False)
 model.compile(optimizer=adam,
loss='categorical_crossentropy',
metrics=['accuracy'])
 model.fit(X_train, y_train, epochs=KERAS_EPOCHS, batch_size=KERAS_BATCH_SIZE, validation_split=KERAS_VALIDATION_SPLIT)
 print("Training.Dataset size: {} {:5.2f}s".format(X_train.shape, default_timer() - start_time))<predict_on_test>
def model_lstm_atten(embedding_matrix):
inp = Input(shape=(maxlen,))
x = Embedding(max_features, embed_size, weights=[embedding_matrix], trainable=False )(inp)
 x = Bidirectional(CuDNNLSTM(128, return_sequences=True))(x)
 x = Bidirectional(CuDNNLSTM(64, return_sequences=True))(x)
 x = Attention(maxlen )(x)
 x = Dense(64, activation="relu" )(x)
 x = Dense(1, activation="sigmoid" )(x)
 model = Model(inputs=inp, outputs=x)
 model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
 return model
y_preds = model.predict(X_test)
 y_preds.shape<save_to_csv>
def train_pred(model, epochs=2):
for e in range(epochs):
model.fit(train_X, train_y, batch_size=512, epochs=1, validation_data=(val_X, val_y))
pred_val_y = model.predict([val_X], batch_size=1024, verbose=0)
 best_thresh = 0.5
best_score = 0.0
for thresh in np.arange(0.1, 0.501, 0.01):
thresh = np.round(thresh, 2)
 score = metrics.f1_score(val_y,(pred_val_y > thresh ).astype(int))
if score > best_score:
best_thresh = thresh
best_score = score
print("Val F1 Score: {:.4f}".format(best_score))
pred_test_y = model.predict([test_X], batch_size=1024, verbose=0)
 return pred_val_y, pred_test_y, best_score
sample_submission = pd.read_csv(DATA_ROOT / 'sampleSubmission.csv')
 sample_submission['Sentiment'] = np.argmax(y_preds,axis=1)
 sample_submission.to_csv("predictions.csv", index=False )<save_to_csv>
embedding_matrix_1 = load_glove(word_index)
 embedding_matrix_3 = load_para(word_index)
 embedding_matrix = np.mean([embedding_matrix_1, embedding_matrix_3], axis = 0)
 np.shape(embedding_matrix)
 show_timer()
params = {
'objective': 'multiclass',
'num_class':y_train.shape[1]
}
lgbm_model = lgbm.LGBMClassifier(objective='multiclass')
 lgbm_model.fit(X_train, label)
 y_preds = lgbm_model.predict_proba(X_test)
 sample_submission = pd.read_csv(DATA_ROOT / 'sampleSubmission.csv')
 sample_submission['Sentiment'] = np.argmax(y_preds,axis=1)
 sample_submission.to_csv("predictions_lgbm.csv", index=False )<save_to_csv>
outputs = []
def create_download_link(title = "Download CSV file", filename = "data.csv"):
html = '<a href={filename}>{title}</a>'
html = html.format(title=title,filename=filename)
 return HTML(html)
 create_download_link(filename='predictions_lgbm.csv')
<install_modules>
nsplits = 5
nfiles = np.arange(len(train_df))
kfold = StratifiedKFold(nsplits, shuffle=True ).split(train_X_all, train_y_all )
%%bash
pip install -q transformers<import_modules>
idx = 0
for train_idx, val_idx in kfold:
train_X, val_X, test_X, train_y, val_y = load_and_prec(train_idx, val_idx)
 pred_val_y, pred_test_y, best_score = train_pred(model_lstm_atten(embedding_matrix), epochs = 3)
 outputs.append([pred_val_y, pred_test_y, best_score, f'2 LSTM w/ attention fold id {idx}'])
 idx += 1
import numpy as np
import pandas as pd
from pathlib import Path
import os
import torch
import torch.optim as optim
import random
from fastai import *
from fastai.text import *
from fastai.callbacks import *
from transformers import PreTrainedModel, PreTrainedTokenizer, PretrainedConfig
from transformers import BertForSequenceClassification, BertTokenizer, BertConfig
from transformers import RobertaForSequenceClassification, RobertaTokenizer, RobertaConfig
from transformers import XLNetForSequenceClassification, XLNetTokenizer, XLNetConfig
from transformers import XLMForSequenceClassification, XLMTokenizer, XLMConfig
from transformers import DistilBertForSequenceClassification, DistilBertTokenizer, DistilBertConfig<import_modules>
best_thresh = 0.32
pred_test_y = np.mean([outputs[i][1] for i in range(len(outputs)) ], axis = 0)
 pred_test_y2 =(pred_test_y > best_thresh ).astype(int)
 test_df = pd.read_csv(".. /input/test.csv", usecols=["qid"])
 out_df = pd.DataFrame({"qid":test_df["qid"].values})
 out_df['prediction'] = pred_test_y2
out_df.to_csv("submission.csv", index=False)
 show_timer()