Kaggle competition: Quora Insincere Questions Classification
<SOS> metric: FScore_1 Kaggle data source: quora-insincere-questions-classification<train_model>
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
import os
import numpy as np
import pandas as pd
from tqdm import tqdm
import math
from sklearn.model_selection import train_test_split
t = prediction.reshape(28000, 28, 28)
 plt.imshow(t[2222])
 print(" The label predicted is ", submission["Label"][2222] )<save_to_csv>
SEQ_LEN = 100
submission.to_csv('./submission.csv',index=False )<set_options>
train_df = pd.read_csv(".. /input/train.csv")
 train_df, val_df = train_test_split(train_df, test_size=0.07 )
%matplotlib inline 
<load_from_csv>
def f1(y_true, y_pred):
def recall(y_true, y_pred):

 true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
 possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
 recall = true_positives /(possible_positives + K.epsilon())
 return recall
def precision(y_true, y_pred):

 true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
 predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
 precision = true_positives /(predicted_positives + K.epsilon())
 return precision
precision = precision(y_true, y_pred)
 recall = recall(y_true, y_pred)
 return 2*(( precision*recall)/(precision+recall+K.epsilon()))
train, test = pd.read_csv(".. /input/train.csv"), pd.read_csv(".. /input/test.csv" )<count_values>
embeddings_index = {}
f = open('.. /input/embeddings/glove.840B.300d/glove.840B.300d.txt')
 for line in tqdm(f):
values = line.split(" ")
 word = values[0]
coefs = np.asarray(values[1:], dtype='float32')
 embeddings_index[word] = coefs
f.close()
print('Found %s word vectors.' % len(embeddings_index))
train['label'].value_counts()<data_type_conversions>
_WORD_SPLIT = re.compile("([.,!?"':; )(])")
 _DIGIT_RE = re.compile(br"\d")
 STOP_WORDS = "" ' [ ]., ! : ; ?".split(" ")
 def basic_tokenizer(sentence):

 words = []
for space_separated_fragment in sentence.strip().split() :
words.extend(_WORD_SPLIT.split(space_separated_fragment))
return [w.lower() for w in words if w != '' and w != ' ']
train_pixels, test_pixels = train.iloc[:,1:].values.astype('float32'), test.values.astype('float32')
 train_labels = train.iloc[:,0].values.astype('int32')
 train_labels = train_labels.reshape(-1, 1)
 print(f'train_pixels shape = {train_pixels.shape}')
 print(f'test_pixels shape = {test_pixels.shape}')
 print(f'train_labels shape = {train_labels.shape}' )<train_model>
def text_to_array(text):
empyt_emb = np.zeros(300)
 text = basic_tokenizer(text[:-1])[:SEQ_LEN]
embeds = [embeddings_index.get(x, empyt_emb)for x in text]
embeds+= [empyt_emb] *(SEQ_LEN - len(embeds))
return np.array(embeds)
 val_vects = np.array([text_to_array(X_text)for X_text in tqdm(val_df["question_text"][:3000])])
 val_y = np.array(val_df["target"][:3000] )
train_pixels, test_pixels = train_pixels.reshape(-1, 28, 28, 1), test_pixels.reshape(-1, 28, 28, 1)
 print(f'train_pixels shape = {train_pixels.shape}')
 print(f'test_pixels shape = {test_pixels.shape}' )<train_model>
batch_size = 256
def batch_gen(train_df):
n_batches = math.ceil(len(train_df)/ batch_size)
 while True:
train_df = train_df.sample(frac=1.)
for i in range(n_batches):
texts = train_df.iloc[i*batch_size:(i+1)*batch_size, 1]
text_arr = np.array([text_to_array(text)for text in texts])
 yield text_arr, np.array(train_df["target"][i*batch_size:(i+1)*batch_size] )
train_pixels, test_pixels = train_pixels / 255.0, test_pixels / 255.0<categorify>
from keras.models import Sequential,Model
from keras.layers import CuDNNLSTM, Dense, Bidirectional, Input,Dropout
from keras import backend as K
from keras.engine.topology import Layer
from keras import initializers, regularizers, constraints
train_labels = to_categorical(train_labels, num_classes = 10)
 print(f'train_labels shape = {train_labels.shape}')
 train_labels<split>
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
train_pixels, val_pixels, train_labels, val_labels = train_test_split(train_pixels, train_labels, test_size = 0.1, random_state=None)
 train_pixels.shape, train_labels.shape, val_pixels.shape, val_labels.shape, test_pixels.shape<prepare_x_and_y>
inp = Input(shape=(SEQ_LEN,300))
x = Bidirectional(CuDNNLSTM(64, return_sequences=True))(inp)
 x = Bidirectional(CuDNNLSTM(64,return_sequences=True))(x)
 x = Attention(SEQ_LEN )(x)
 x = Dense(256, activation="relu" )(x)
 x = Dense(1, activation="sigmoid" )(x)
 model = Model(inputs=inp, outputs=x)
 model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=[f1] )
m_train = train_pixels.shape[0]
m_val = val_pixels.shape[0]
m_test = test_pixels.shape[0]
n_x = test.shape[1]
n_y = train_labels.shape[1]
print(f" m_train = {m_train} / m_val = {m_val} / m_test = {m_test} / n_x = {n_x} / n_y = {n_y}" )<init_hyperparams>
mg = batch_gen(train_df)
 model.fit_generator(mg, epochs=5,
steps_per_epoch=1000,
validation_data=(val_vects, val_y),
verbose=True )
datagen = ImageDataGenerator(
 rotation_range = 10,
zoom_range = 0.1,
shear_range = 0.1,
width_shift_range = 0.1,
height_shift_range = 0.1 )<choose_model_class>
batch_size = 256
def batch_gen(test_df):
n_batches = math.ceil(len(test_df)/ batch_size)
 for i in range(n_batches):
texts = test_df.iloc[i*batch_size:(i+1)*batch_size, 1]
text_arr = np.array([text_to_array(text)for text in texts])
 yield text_arr
test_df = pd.read_csv(".. /input/test.csv")
 all_preds = []
for x in tqdm(batch_gen(test_df)) :
all_preds.extend(model.predict(x ).flatten() )
<save_to_csv><EOS> 
y_te =(np.array(all_preds)> 0.35 ).astype(np.int)
 submit_df = pd.DataFrame({"qid": test_df["qid"], "prediction": y_te})
 submit_df.to_csv("submission.csv", index=False )