{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input/quora-insincere-questions-classification/\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn import metrics\nimport re\nimport gc\n\nfrom keras.preprocessing.text import Tokenizer\nfrom keras.preprocessing.sequence import pad_sequences\nfrom keras.layers import Dense, Input, CuDNNLSTM, Embedding, Dropout, Activation, CuDNNGRU, Conv1D, GRU\nfrom keras.layers import Bidirectional, GlobalMaxPool1D, GlobalAvgPool1D, BatchNormalization\nfrom keras.optimizers import Adam\nfrom keras.models import Model\nfrom keras import backend as K\nfrom keras.engine.topology import Layer\nfrom keras import initializers, regularizers, constraints, optimizers, layers\nfrom keras.callbacks import ModelCheckpoint\nimport keras.utils as ku","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c62ac216a8adf0b552b4917667027c09a1507c21"},"cell_type":"code","source":"embed_size = 300 # how big is each word vector\nmax_features = 100000 # how many unique words to use (i.e num rows in embedding vector)\nmaxlen = 72 # max number of words in a question to use\nSEED = 2019\nfrom tqdm import tqdm\ntqdm.pandas()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c1882859e8ba362d4077919dfac0e4efd9599132"},"cell_type":"code","source":"puncts = [',', '.', '\"', ':', ')', '(', '-', '!', '?', '|', ';', \"'\", '$', '&', '/', '[', ']', '>', '%', '=', '#', '*', '+', '\\\\', '•',  '~', '@', '£', \n '·', '_', '{', '}', '©', '^', '®', '`',  '<', '→', '°', '€', '™', '›',  '♥', '←', '×', '§', '″', '′', 'Â', '█', '½', 'à', '…', \n '“', '★', '”', '–', '●', 'â', '►', '−', '¢', '²', '¬', '░', '¶', '↑', '±', '¿', '▾', '═', '¦', '║', '―', '¥', '▓', '—', '‹', '─', \n '▒', '：', '¼', '⊕', '▼', '▪', '†', '■', '’', '▀', '¨', '▄', '♫', '☆', 'é', '¯', '♦', '¤', '▲', 'è', '¸', '¾', 'Ã', '⋅', '‘', '∞', \n '∙', '）', '↓', '、', '│', '（', '»', '，', '♪', '╩', '╚', '³', '・', '╦', '╣', '╔', '╗', '▬', '❤', 'ï', 'Ø', '¹', '≤', '‡', '√', ]\n\ndef clean_text(x):\n    x = str(x)\n    for punct in puncts:\n        x = x.replace(punct, f' {punct} ')\n    return x\n\ndef clean_numbers(x):\n    x = re.sub('[0-9]{5,}', '#####', x)\n    x = re.sub('[0-9]{4}', '####', x)\n    x = re.sub('[0-9]{3}', '###', x)\n    x = re.sub('[0-9]{2}', '##', x)\n    return x\n\nmispell_dict = {\"aren't\" : \"are not\",\n\"can't\" : \"cannot\",\n\"couldn't\" : \"could not\",\n\"didn't\" : \"did not\",\n\"doesn't\" : \"does not\",\n\"don't\" : \"do not\",\n\"hadn't\" : \"had not\",\n\"hasn't\" : \"has not\",\n\"haven't\" : \"have not\",\n\"he'd\" : \"he would\",\n\"he'll\" : \"he will\",\n\"he's\" : \"he is\",\n\"i'd\" : \"I would\",\n\"i'd\" : \"I had\",\n\"i'll\" : \"I will\",\n\"i'm\" : \"I am\",\n\"isn't\" : \"is not\",\n\"it's\" : \"it is\",\n\"it'll\":\"it will\",\n\"i've\" : \"I have\",\n\"let's\" : \"let us\",\n\"mightn't\" : \"might not\",\n\"mustn't\" : \"must not\",\n\"shan't\" : \"shall not\",\n\"she'd\" : \"she would\",\n\"she'll\" : \"she will\",\n\"she's\" : \"she is\",\n\"shouldn't\" : \"should not\",\n\"that's\" : \"that is\",\n\"there's\" : \"there is\",\n\"they'd\" : \"they would\",\n\"they'll\" : \"they will\",\n\"they're\" : \"they are\",\n\"they've\" : \"they have\",\n\"we'd\" : \"we would\",\n\"we're\" : \"we are\",\n\"weren't\" : \"were not\",\n\"we've\" : \"we have\",\n\"what'll\" : \"what will\",\n\"what're\" : \"what are\",\n\"what's\" : \"what is\",\n\"what've\" : \"what have\",\n\"where's\" : \"where is\",\n\"who'd\" : \"who would\",\n\"who'll\" : \"who will\",\n\"who're\" : \"who are\",\n\"who's\" : \"who is\",\n\"who've\" : \"who have\",\n\"won't\" : \"will not\",\n\"wouldn't\" : \"would not\",\n\"you'd\" : \"you would\",\n\"you'll\" : \"you will\",\n\"you're\" : \"you are\",\n\"you've\" : \"you have\",\n\"'re\": \" are\",\n\"wasn't\": \"was not\",\n\"we'll\":\" will\",\n\"didn't\": \"did not\",\n\"tryin'\":\"trying\"}\n\ndef _get_mispell(mispell_dict):\n    mispell_re = re.compile('(%s)' % '|'.join(mispell_dict.keys()))\n    return mispell_dict, mispell_re\n\nmispellings, mispellings_re = _get_mispell(mispell_dict)\ndef replace_typical_misspell(text):\n    def replace(match):\n        return mispellings[match.group(0)]\n    return mispellings_re.sub(replace, text)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c0eadda2b828b9954e50817e26a59b48a273abc5"},"cell_type":"code","source":"def data_preparation():\n    train_df = pd.read_csv(\"../input/quora-insincere-questions-classification/train.csv\")\n    test_df = pd.read_csv(\"../input/quora-insincere-questions-classification/test.csv\")\n    \n    print(\"Train shape : \",train_df.shape)\n    print(\"Test shape : \",test_df.shape)\n    \n    # lower\n    train_df[\"question_text\"] = train_df[\"question_text\"].progress_apply(lambda x: x.lower())\n    test_df[\"question_text\"] = test_df[\"question_text\"].progress_apply(lambda x: x.lower())\n    \n    # Clean the text\n    train_df[\"question_text\"] = train_df[\"question_text\"].progress_apply(lambda x: clean_text(x))\n    test_df[\"question_text\"] = test_df[\"question_text\"].progress_apply(lambda x: clean_text(x))\n    \n    # Clean numbers\n    train_df[\"question_text\"] = train_df[\"question_text\"].progress_apply(lambda x: clean_numbers(x))\n    test_df[\"question_text\"] = test_df[\"question_text\"].progress_apply(lambda x: clean_numbers(x))\n    \n    # Clean speelings\n    train_df[\"question_text\"] = train_df[\"question_text\"].progress_apply(lambda x: replace_typical_misspell(x))\n    test_df[\"question_text\"] = test_df[\"question_text\"].progress_apply(lambda x: replace_typical_misspell(x))\n    \n    ## fill up the missing values\n    data_df = pd.concat([train_df[\"question_text\"], test_df[\"question_text\"]])\n    data = data_df.fillna(\"_##_\").values\n    train_df, val_df = train_test_split(train_df, test_size=0.1, random_state=2019)\n    train_X = train_df[\"question_text\"].fillna(\"_##_\").values\n    val_X = val_df[\"question_text\"].fillna(\"_##_\").values\n    test_X = test_df[\"question_text\"].fillna(\"_##_\").values\n    print(data.shape)\n\n    ## Tokenize the sentences\n    print(\"Tokenization...\")\n    tokenizer = Tokenizer(num_words=max_features)\n    tokenizer.fit_on_texts(list(data))\n    total_words = len(tokenizer.word_index) + 1\n    \n    data = tokenizer.texts_to_sequences(data)\n    train_X = tokenizer.texts_to_sequences(train_X)\n    val_X = tokenizer.texts_to_sequences(val_X)\n    test_X = tokenizer.texts_to_sequences(test_X)\n    \n    max_seq_len = max([len(x) for x in data])\n\n    ## Pad the sentences \n    data = pad_sequences(data, maxlen=max_seq_len)\n    train_X = pad_sequences(train_X, maxlen=max_seq_len)\n    val_X = pad_sequences(val_X, maxlen=max_seq_len)\n    test_X = pad_sequences(test_X, maxlen=max_seq_len)\n    \n    train_y = train_df['target'].values\n    val_y = val_df['target'].values\n    #Generate predictors and labels\n    predictors, labels = data[:,:-1], data[:,-1]\n    \n    return predictors, labels, max_seq_len, total_words, tokenizer.word_index, train_X, train_y, val_X, val_y, test_X","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"851119eb64145cd2f9ee6a6fa4995ff9b9fd7f31"},"cell_type":"code","source":"def load_glove(word_index):\n    EMBEDDING_FILE = '../input/quora-insincere-questions-classification/embeddings/glove.840B.300d/glove.840B.300d.txt'\n    def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')\n    embeddings_index = dict(get_coefs(*o.split(\" \")) for o in open(EMBEDDING_FILE))\n\n    all_embs = np.stack(embeddings_index.values())\n    emb_mean,emb_std = all_embs.mean(), all_embs.std()\n    embed_size = all_embs.shape[1]\n\n    # word_index = tokenizer.word_index\n    nb_words = min(max_features, len(word_index))\n    embedding_matrix = np.random.normal(emb_mean, emb_std, (nb_words, embed_size))\n    for word, i in word_index.items():\n        if i >= max_features: continue\n        embedding_vector = embeddings_index.get(word)\n        if embedding_vector is not None: embedding_matrix[i] = embedding_vector\n            \n    return embedding_matrix \n\ndef load_para(word_index):\n    EMBEDDING_FILE = '../input/quora-insincere-questions-classification/embeddings/paragram_300_sl999/paragram_300_sl999.txt'\n    def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')\n    embeddings_index = dict(get_coefs(*o.split(\" \")) for o in open(EMBEDDING_FILE, encoding=\"utf8\", errors='ignore') if len(o)>100)\n\n    all_embs = np.stack(embeddings_index.values())\n    emb_mean,emb_std = all_embs.mean(), all_embs.std()\n    embed_size = all_embs.shape[1]\n\n    # word_index = tokenizer.word_index\n    nb_words = min(max_features, len(word_index))\n    embedding_matrix = np.random.normal(emb_mean, emb_std, (nb_words, embed_size))\n    for word, i in word_index.items():\n        if i >= max_features: continue\n        embedding_vector = embeddings_index.get(word)\n        if embedding_vector is not None: embedding_matrix[i] = embedding_vector\n    \n    return embedding_matrix\n\ndef load_fasttext(word_index):\n    EMBEDDING_FILE = '../input/quora-insincere-questions-classification/embeddings/wiki-news-300d-1M/wiki-news-300d-1M.vec'\n    def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')\n    embeddings_index = dict(get_coefs(*o.split(\" \")) for o in open(EMBEDDING_FILE) if len(o)>100)\n\n    all_embs = np.stack(embeddings_index.values())\n    emb_mean,emb_std = all_embs.mean(), all_embs.std()\n    embed_size = all_embs.shape[1]\n\n\n    nb_words = min(max_features, len(word_index))\n    embedding_matrix = np.random.normal(emb_mean, emb_std, (nb_words, embed_size))\n    for word, i in word_index.items():\n        if i >= max_features: continue\n        embedding_vector = embeddings_index.get(word)\n        if embedding_vector is not None: embedding_matrix[i] = embedding_vector\n    \n    return embedding_matrix","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a794a07a4e8c736fdbbff612d3c3c9ee01209b63"},"cell_type":"code","source":"class Attention(Layer):\n    def __init__(self, step_dim,\n                 W_regularizer=None, b_regularizer=None,\n                 W_constraint=None, b_constraint=None,\n                 bias=True, **kwargs):\n        self.supports_masking = True\n        self.init = initializers.get('glorot_uniform')\n\n        self.W_regularizer = regularizers.get(W_regularizer)\n        self.b_regularizer = regularizers.get(b_regularizer)\n\n        self.W_constraint = constraints.get(W_constraint)\n        self.b_constraint = constraints.get(b_constraint)\n\n        self.bias = bias\n        self.step_dim = step_dim\n        self.features_dim = 0\n        super(Attention, self).__init__(**kwargs)\n\n    def build(self, input_shape):\n        assert len(input_shape) == 3\n\n        self.W = self.add_weight((input_shape[-1],),\n                                 initializer=self.init,\n                                 name='{}_W'.format(self.name),\n                                 regularizer=self.W_regularizer,\n                                 constraint=self.W_constraint)\n        self.features_dim = input_shape[-1]\n\n        if self.bias:\n            self.b = self.add_weight((input_shape[1],),\n                                     initializer='zero',\n                                     name='{}_b'.format(self.name),\n                                     regularizer=self.b_regularizer,\n                                     constraint=self.b_constraint)\n        else:\n            self.b = None\n\n        self.built = True\n\n    def compute_mask(self, input, input_mask=None):\n        return None\n\n    def call(self, x, mask=None):\n        features_dim = self.features_dim\n        step_dim = self.step_dim\n\n        eij = K.reshape(K.dot(K.reshape(x, (-1, features_dim)),\n                        K.reshape(self.W, (features_dim, 1))), (-1, step_dim))\n\n        if self.bias:\n            eij += self.b\n\n        eij = K.tanh(eij)\n\n        a = K.exp(eij)\n\n        if mask is not None:\n            a *= K.cast(mask, K.floatx())\n\n        a /= K.cast(K.sum(a, axis=1, keepdims=True) + K.epsilon(), K.floatx())\n\n        a = K.expand_dims(a)\n        weighted_input = x * a\n        return K.sum(weighted_input, axis=1)\n\n    def compute_output_shape(self, input_shape):\n        return input_shape[0],  self.features_dim","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f23b1b5fee8280a790cc714785cd29e958358d5a"},"cell_type":"code","source":"predictors, label, max_sequence_len, total_words, word_index, train_X, train_y, val_X, val_y, test_X = data_preparation()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d7d871908c517982e8345fef4687bbbc66df6474"},"cell_type":"code","source":"print(len(predictors[0]), max_sequence_len, total_words, len(word_index), train_X.shape, val_X.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a235c7d1a4b95efb22ab245229c7ccb7b5f4149b"},"cell_type":"code","source":"#embedding_matrix = load_glove(word_index)\nembedding_matrix = np.zeros((max_features, embed_size))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b2d95a1dffd00cf43a060589f83f9f349c6a83b8"},"cell_type":"code","source":"'''\ninp = Input(shape=(max_sequence_len-1,), dtype='float32')\nx = Embedding(max_features, embed_size, weights=[embedding_matrix], trainable=True)(inp)\nx = Bidirectional(CuDNNGRU(128, return_sequences=True))(x)\nx21 = Attention(step_dim=max_sequence_len-1)(x)\nx1 = Bidirectional(CuDNNGRU(64, return_sequences=True))(x)\nx2 = Attention(step_dim=max_sequence_len-1)(x1)\nconc = layers.Concatenate()([x21, x2])\nconc = Dropout(0.1)(conc)\nout = Dense(total_words, activation='softmax')(conc)\n'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"968e2b88e8b75e1e166675ff58eaa7a168fdfb7c"},"cell_type":"code","source":"#model = Model(inputs=inp, outputs=out)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"68558f8ae21cf4024b8e3e55fce0ed9e3cb753a1"},"cell_type":"code","source":"#model.compile(loss=\"sparse_categorical_crossentropy\", optimizer='adam', metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f109cce2061ae3c26f824b51b7ace36ab263a46d"},"cell_type":"code","source":"#model.load_weights(\"../input/10epoch-lm/pre_trained_weights_5_to_10.h5\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cdd294fcb726fece17cfbf3ab9ead4f4f7ccd650"},"cell_type":"code","source":"#callback=ModelCheckpoint(\"pre_trained_weights_10_to_15.h5\", monitor='val_loss', verbose=1, save_best_only=False, save_weights_only=True, mode='auto', period=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a8acdc928dc8cff3ee5921e30c0da0af2acf8ba9"},"cell_type":"code","source":"#model.fit(x=predictors, y=label, epochs=5, batch_size=512, shuffle=True, callbacks=[callback])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f18cdae3901ad37212d2d068aa32d1f16238de6b"},"cell_type":"code","source":"inp = Input(shape=(max_sequence_len-1,), dtype='float32')\nx = Embedding(max_features, embed_size, weights=[embedding_matrix], trainable=True)(inp)\nx = Bidirectional(CuDNNGRU(128, return_sequences=True))(x)\nx21 = Attention(step_dim=max_sequence_len-1)(x)\nx1 = Bidirectional(CuDNNGRU(64, return_sequences=True))(x)\nx2 = Attention(step_dim=max_sequence_len-1)(x1)\nconc = layers.Concatenate()([x21, x2])\nconc = Dropout(0.1)(conc)\n#out = Dense(total_words, activation='softmax')(conc)\no = Dense(32, activation='relu', name='newd')(conc)\no = Dropout(0.1, name=\"new_drop\")(o)\n#o = BatchNormalization()(o)\noutput = Dense(1, activation='sigmoid', name='out')(o)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6de8325f729bad0bf162405aab94f35f4dd09c72"},"cell_type":"code","source":"model1 = Model(inputs=inp, outputs=output)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"90904a7d43fa2d76e7f20778197d36fe97eb9c54"},"cell_type":"code","source":"optimizer = Adam(lr=0.0001)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7ce7a52a542ee23c0d4e14732b139ac8ed49cd13"},"cell_type":"code","source":"model1.compile(loss=\"binary_crossentropy\", optimizer=optimizer, metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e7d8e9964b33c1aa974280cb6b5f794528b96db6"},"cell_type":"code","source":"model1.load_weights(\"../input/10epoch-lm/pre_trained_weights_5_to_10.h5\", by_name=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8ff82f57627d9bbc0afafb685f4ecaba01a16760"},"cell_type":"code","source":"train_X = train_X[:,1:]\nval_X = val_X[:, 1:]\ntest_X = test_X[:, 1:]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ad92de58bd5f4bfeddd909538749c8ab25fcafb1"},"cell_type":"code","source":"train_X[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2e9da172bff57df8916cb9be48143613df44c2b0"},"cell_type":"code","source":"model1.fit(train_X, train_y, batch_size=512, epochs=2, validation_data=(val_X, val_y))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"49b3e4e7c4fced3ed7d7fca7a1ff1ac2cfca14bf"},"cell_type":"code","source":"pred_noemb_val_y = model1.predict([val_X], batch_size=1024, verbose=1)\nfor thresh in np.arange(0.1, 0.501, 0.01):\n    thresh = np.round(thresh, 2)\n    print(\"F1 score at threshold {0} is {1}\".format(thresh, metrics.f1_score(val_y, (pred_noemb_val_y>thresh).astype(int))))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1437c10e6b201aea08d44002e1e9fab4a8bd87cf"},"cell_type":"code","source":"pred_noemb_test_y = model1.predict([test_X], batch_size=1024, verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c71dc4ed5a6ed2fcd11278da103a1f9d44ccad87"},"cell_type":"code","source":"test_df = pd.read_csv(\"../input/quora-insincere-questions-classification/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"01437d6d7cea4709c887e08a3db2d51052e302ae"},"cell_type":"code","source":"pred_test_y = (pred_noemb_test_y>0.34).astype(int)\nout_df = pd.DataFrame({\"qid\":test_df[\"qid\"].values})\nout_df['prediction'] = pred_test_y\nout_df.to_csv(\"submission.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1d68c203ac18a92b1f2a90e7726daf70cd2ca95f"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}