{"cells":[{"metadata":{"_uuid":"1269d0481484736312ea010b46218dbc9dd4ac94"},"cell_type":"markdown","source":"### Materials used for this notebook\n- Preprocessing: https://www.kaggle.com/theoviel/improve-your-score-with-some-text-preprocessing\n- Embedding mean: https://www.kaggle.com/shujian/single-rnn-with-4-folds-clr\n- OneCycle:  https://github.com/titu1994/keras-one-cycle\n-  https://www.kaggle.com/strideradu/word2vec-and-gensim-go-go-go\n- Capsule https://www.kaggle.com/gmhost/gru-capsule\n- Text features: https://github.com/wongchunghang/toxic-comment-challenge-lstm/blob/master/toxic_comment_9872_model.ipynb\n"},{"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\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\"))\nprint(os.listdir())\n\nimport time\nstartTime = time.time()\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":"import pandas as pd\nimport numpy as np\nimport operator \nimport re\nimport gc\nimport pickle\n\nfrom keras.preprocessing.text import Tokenizer\nfrom sklearn.preprocessing import StandardScaler","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"38d688e434b5f9da7e111caab6d21229044e8774"},"cell_type":"code","source":"np.random.seed(201942)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a2f22c60a4ae33007fabfc892bd68d472fc51164"},"cell_type":"markdown","source":"# Embeddings - methods"},{"metadata":{"trusted":true,"_uuid":"50a7e53ea6dec85b5a0df42b0411ede4a491f2a9"},"cell_type":"code","source":"def load_embed(file):\n    def get_coefs(word, *arr):\n        return word, np.asarray(arr, dtype='float32')\n\n    if file == '../input/embeddings/wiki-news-300d-1M/wiki-news-300d-1M.vec':\n        embeddings_index = dict(get_coefs(*o.split(\" \")) for o in open(file) if len(o) > 100)\n    else:\n        embeddings_index = dict(get_coefs(*o.split(\" \")) for o in open(file, encoding='latin'))\n        \n    return embeddings_index","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"20333b0ebe78575291fe3baadd0453b4212905b5"},"cell_type":"code","source":"def index_embs(embeddings_index, word_index, NUM_WORDS, fileName):\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#     nb_words = min(NUM_WORDS, len(word_index))\n    embedding_matrix = np.random.normal(emb_mean, emb_std, (NUM_WORDS, embed_size))\n\n    for word, i in word_index.items():\n        if i >= NUM_WORDS: continue\n        embedding_vector = embeddings_index.get(word)\n        if embedding_vector is not None: embedding_matrix[i] = embedding_vector\n    \n    np.save(fileName, embedding_matrix)\n    print(fileName + \" embedding matrix saved!\")\n    \n    del(embeddings_index)\n    del(word_index)\n    del(embedding_matrix)\n    gc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"03fe8724352817c10d7bf65054ca879d4f625430"},"cell_type":"code","source":"def check_coverage(vocab, embeddings_index):\n    known_words = {}\n    unknown_words = {}\n    nb_known_words = 0\n    nb_unknown_words = 0\n    for word in vocab.keys():\n        try:\n            known_words[word] = embeddings_index[word]\n            nb_known_words += vocab[word]\n        except:\n            unknown_words[word] = vocab[word]\n            nb_unknown_words += vocab[word]\n            pass\n\n    print('Found embeddings for {:.2%} of vocab'.format(len(known_words) / len(vocab)))\n    print('Found embeddings for  {:.2%} of all text'.format(nb_known_words / (nb_known_words + nb_unknown_words)))\n    unknown_words = sorted(unknown_words.items(), key=operator.itemgetter(1))[::-1]\n\n    return unknown_words","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d01e30b9e7d6163cab80563f5f7ad8a707d4cebb"},"cell_type":"code","source":"def build_vocab(texts, num_words):\n#     sentences = texts.apply(lambda x: x.split()).values\n#     vocab = {}\n#     for sentence in sentences:\n#         for word in sentence:\n#             try:\n#                 vocab[word] += 1\n#             except KeyError:\n#                 vocab[word] = 1\n    tokenizer = Tokenizer(num_words=NUM_WORDS, filters=\"\")\n    tokenizer.fit_on_texts(texts)\n    \n    # saving\n    with open('tokenizer.pickle', 'wb') as handle:\n        pickle.dump(tokenizer, handle, protocol=pickle.HIGHEST_PROTOCOL)\n    \n    print(\"\\nKeras text tokenizer has been saved as tokenizer.pickle\")\n    \n    return tokenizer.word_index","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"eddcb2d64e893b281bff6d87444867a72b97a1e9"},"cell_type":"code","source":"def add_lower(embedding, vocab):\n    \"\"\"\n    Therer are words that are known with upper letters and unknown without. This method saves both variants of the word\n    \n    \"\"\"\n\n    count = 0\n    for word in vocab:\n        if word in embedding and word.lower() not in embedding:  \n            embedding[word.lower()] = embedding[word]\n            count += 1\n    print(f\"Added {count} words to embedding\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f73a57ca72feb1870bb15f40ef59b400e8eda2f2"},"cell_type":"code","source":"def known_contractions(embed): \n    known = []\n    for contract in contraction_mapping:\n        if contract in embed:\n            known.append(contract)\n    return known","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f93b5fcf9099f78880e8673d0cda4baa700a3801"},"cell_type":"code","source":"def clean_contractions(text, mapping):\n    specials = [\"’\", \"‘\", \"´\", \"`\"]\n    for s in specials:\n        text = text.replace(s, \"'\")\n    text = ' '.join([mapping[t] if t in mapping else t for t in text.split(\" \")])\n    return text","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"443b46203179107bf74c28e54e5f6f6b21b38aae"},"cell_type":"code","source":"def unknown_punct(embed, punct):\n    unknown = ''\n    for p in punct:\n        if p not in embed:\n            unknown += p\n            unknown += ' '\n    return unknown","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"567cd85bacd35fecb2714af834b8fcfa4abde6b1"},"cell_type":"code","source":"def clean_special_chars(text, punct, mapping):\n    for p in mapping:\n        text = text.replace(p, mapping[p])\n    \n    for p in punct:\n        text = text.replace(p, f' {p} ')\n    \n    specials = {'\\u200b': ' ', '…': ' ... ', '\\ufeff': '', 'करना': '', 'है': ''}  # Other special characters that I have to deal with in last\n    for s in specials:\n        text = text.replace(s, specials[s])\n    \n    return text","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2779f1eb74ecdf9ae893f6d5555026a7d486c9e2"},"cell_type":"code","source":"def get_mappings():\n    \"\"\"\n    returns: \n    mispell_dict: mapping from mispelled word to correct word\n    contraction_mapping: mapping from contraction to full word(s)\n    punct_mapping: mapping from punctuation/special char to proper punctuation\n    \"\"\"\n    mispell_dict = {'colour': 'color', 'centre': 'center', 'favourite': 'favorite', 'travelling': 'traveling', 'counselling': 'counseling', 'theatre': 'theater', 'cancelled': 'canceled', 'labour': 'labor', 'organisation': 'organization', 'wwii': 'world war 2', 'citicise': 'criticize', 'youtu ': 'youtube ', 'Qoura': 'Quora', 'sallary': 'salary', 'Whta': 'What', 'narcisist': 'narcissist', 'howdo': 'how do', 'whatare': 'what are', 'howcan': 'how can', 'howmuch': 'how much', 'howmany': 'how many', 'whydo': 'why do', 'doI': 'do I', 'theBest': 'the best', 'howdoes': 'how does', 'mastrubation': 'masturbation', 'mastrubate': 'masturbate', \"mastrubating\": 'masturbating', 'pennis': 'penis', 'Etherium': 'Ethereum', 'narcissit': 'narcissist', 'bigdata': 'big data', '2k17': '2017', '2k18': '2018', 'qouta': 'quota', 'exboyfriend': 'ex boyfriend', 'airhostess': 'air hostess', \"whst\": 'what', 'watsapp': 'whatsapp', 'demonitisation': 'demonetization', 'demonitization': 'demonetization', 'demonetisation': 'demonetization'}\n    punct = \"/-'?!.,#$%\\'()*+-/:;<=>@[\\\\]^_`{|}~\" + '\"\"“”’' + '∞θ÷α•à−β∅³π‘₹´°£€\\×™√²—–&'\n    punct_mapping = {\"‘\": \"'\", \"₹\": \"e\", \"´\": \"'\", \"°\": \"\", \"€\": \"e\", \"™\": \"tm\", \"√\": \" sqrt \", \"×\": \"x\", \"²\": \"2\", \"—\": \"-\", \"–\": \"-\", \"’\": \"'\", \"_\": \"-\", \"`\": \"'\", '“': '\"', '”': '\"', '“': '\"', \"£\": \"e\", '∞': 'infinity', 'θ': 'theta', '÷': '/', 'α': 'alpha', '•': '.', 'à': 'a', '−': '-', 'β': 'beta', '∅': '', '³': '3', 'π': 'pi', }\n    contraction_mapping = {\"ain't\": \"is not\", \"aren't\": \"are not\",\"can't\": \"cannot\", \"'cause\": \"because\", \"could've\": \"could have\", \"couldn't\": \"could not\", \"didn't\": \"did not\",  \"doesn't\": \"does not\", \"don't\": \"do not\", \"hadn't\": \"had not\", \"hasn't\": \"has not\", \"haven't\": \"have not\", \"he'd\": \"he would\",\"he'll\": \"he will\", \"he's\": \"he is\", \"how'd\": \"how did\", \"how'd'y\": \"how do you\", \"how'll\": \"how will\", \"how's\": \"how is\",  \"I'd\": \"I would\", \"I'd've\": \"I would have\", \"I'll\": \"I will\", \"I'll've\": \"I will have\",\"I'm\": \"I am\", \"I've\": \"I have\", \"i'd\": \"i would\", \"i'd've\": \"i would have\", \"i'll\": \"i will\",  \"i'll've\": \"i will have\",\"i'm\": \"i am\", \"i've\": \"i have\", \"isn't\": \"is not\", \"it'd\": \"it would\", \"it'd've\": \"it would have\", \"it'll\": \"it will\", \"it'll've\": \"it will have\",\"it's\": \"it is\", \"let's\": \"let us\", \"ma'am\": \"madam\", \"mayn't\": \"may not\", \"might've\": \"might have\",\"mightn't\": \"might not\",\"mightn't've\": \"might not have\", \"must've\": \"must have\", \"mustn't\": \"must not\", \"mustn't've\": \"must not have\", \"needn't\": \"need not\", \"needn't've\": \"need not have\",\"o'clock\": \"of the clock\", \"oughtn't\": \"ought not\", \"oughtn't've\": \"ought not have\", \"shan't\": \"shall not\", \"sha'n't\": \"shall not\", \"shan't've\": \"shall not have\", \"she'd\": \"she would\", \"she'd've\": \"she would have\", \"she'll\": \"she will\", \"she'll've\": \"she will have\", \"she's\": \"she is\", \"should've\": \"should have\", \"shouldn't\": \"should not\", \"shouldn't've\": \"should not have\", \"so've\": \"so have\",\"so's\": \"so as\", \"this's\": \"this is\",\"that'd\": \"that would\", \"that'd've\": \"that would have\", \"that's\": \"that is\", \"there'd\": \"there would\", \"there'd've\": \"there would have\", \"there's\": \"there is\", \"here's\": \"here is\",\"they'd\": \"they would\", \"they'd've\": \"they would have\", \"they'll\": \"they will\", \"they'll've\": \"they will have\", \"they're\": \"they are\", \"they've\": \"they have\", \"to've\": \"to have\", \"wasn't\": \"was not\", \"we'd\": \"we would\", \"we'd've\": \"we would have\", \"we'll\": \"we will\", \"we'll've\": \"we will have\", \"we're\": \"we are\", \"we've\": \"we have\", \"weren't\": \"were not\", \"what'll\": \"what will\", \"what'll've\": \"what will have\", \"what're\": \"what are\",  \"what's\": \"what is\", \"what've\": \"what have\", \"when's\": \"when is\", \"when've\": \"when have\", \"where'd\": \"where did\", \"where's\": \"where is\", \"where've\": \"where have\", \"who'll\": \"who will\", \"who'll've\": \"who will have\", \"who's\": \"who is\", \"who've\": \"who have\", \"why's\": \"why is\", \"why've\": \"why have\", \"will've\": \"will have\", \"won't\": \"will not\", \"won't've\": \"will not have\", \"would've\": \"would have\", \"wouldn't\": \"would not\", \"wouldn't've\": \"would not have\", \"y'all\": \"you all\", \"y'all'd\": \"you all would\",\"y'all'd've\": \"you all would have\",\"y'all're\": \"you all are\",\"y'all've\": \"you all have\",\"you'd\": \"you would\", \"you'd've\": \"you would have\", \"you'll\": \"you will\", \"you'll've\": \"you will have\", \"you're\": \"you are\", \"you've\": \"you have\" }\n    return mispell_dict, contraction_mapping, punct_mapping, punct","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b1e899f9789eb3660ad0586eb266a9f5dde8556e"},"cell_type":"code","source":"def correct_spelling(x, dic):\n    for word in dic.keys():\n        x = x.replace(word, dic[word])\n    return x","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"33f1311fa725e99326e28f1855e85001d587111d"},"cell_type":"code","source":"def 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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"433af4965dbcb9308ed0a34532abb0a0972a38a0"},"cell_type":"code","source":"def add_features(df, textCol, outputFileName):\n    # Create new DF for features\n    df_features = pd.DataFrame()\n    \n    df_features['total_length'] = df[textCol].apply(len)\n    df_features['capitals'] = df[textCol].apply(lambda comment: sum(1 for c in comment if c.isupper()))\n    df_features['caps_vs_length'] = df_features[\"capitals\"] / df_features['total_length']\n    df_features['num_words'] = df[textCol].str.count('\\S+')\n    df_features['num_unique_words'] = df[textCol].apply(lambda comment: len(set(w for w in comment.split())))\n    df_features['words_vs_unique'] = df_features['num_unique_words'] / df_features['num_words']\n    df_features = df_features.fillna(0)\n\n    # Scale the features\n    ss = StandardScaler()\n    df_features = ss.fit_transform(df_features)\n    \n    print(\"\\nFEATURES DF SAVED TO:\")\n    print(\"{0}_features.npy\".format(outputFileName))\n    np.save(\"{0}_features\".format(outputFileName), df_features)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2559a80fdce5bc5fb5dbf1eb8bc888bb7f33cf79"},"cell_type":"markdown","source":"# Embeddings - cleaning"},{"metadata":{"trusted":true,"_uuid":"b7aacecda6b4170ca5422675a4f7612d13aba89d"},"cell_type":"code","source":"def run_text_preprocessing(df, textCol, outputFileName):\n    mispell_map, contraction_map, punct_map, punct = get_mappings()\n    \n#     print(\"\\nWorking on: \" + outputFileName)\n#     print(\"\\n****** WORD COVERAGE BEFORE PREPROCESSING ******\")\n#     vocab = build_vocab(df[textCol])\n#     print(\"Glove : \")\n#     oov_glove = check_coverage(vocab, embed_glove)\n#     print(\"Paragram : \")\n#     oov_paragram = check_coverage(vocab, embed_paragram)\n#     print(\"FastText : \")\n#     oov_fasttext = check_coverage(vocab, embed_fasttext)\n      \n    # To lower char\n    df['lowered_question'] = df[textCol].apply(lambda x: x.lower())\n    # Contractions\n    df['treated_question'] = df['lowered_question'].apply(lambda x: clean_contractions(x, contraction_map))\n    # Punct + Special chars\n    df['treated_question'] = df['treated_question'].apply(lambda x: clean_special_chars(x, punct, punct_map))\n    # Mispelling\n    #df['treated_question'] = df['treated_question'].apply(lambda x: correct_spelling(x, mispell_map))\n    # Numbers\n    df['treated_question'] = df['treated_question'].apply(lambda x: clean_numbers(x))\n    \n#     print(\"\\n****** FINAL WORD COVERAGE AFTER PREPROCESSING ******\")\n#     vocab = build_vocab(df[\"treated_question\"])\n#     print(\"Glove : \")\n#     oov_glove = check_coverage(vocab, embed_glove)\n#     print(\"Paragram : \")\n#     oov_paragram = check_coverage(vocab, embed_paragram)\n#     print(\"FastText : \")\n#     oov_fasttext = check_coverage(vocab, embed_fasttext)\n    \n    print(\"\\nDATAFRAME SAVED TO:\")\n    print(\"{0}_processed_text.csv\".format(outputFileName))\n    df.to_csv(\"{0}_processed_text.csv\".format(outputFileName))\n    \n    del(mispell_map)\n    del(contraction_map) \n    del(punct_map)\n    del(punct)\n    gc.collect()\n    \n    return df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"72e631ea4b49e3589cffc1834eb5093601e85974"},"cell_type":"code","source":"train = pd.read_csv(\"../input/train.csv\")\ntest = pd.read_csv(\"../input/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8573b0d93bcfcb6c4fdeb37df9e26dde7fb6943f"},"cell_type":"code","source":"add_features(train, \"question_text\", \"train\")\nadd_features(test, \"question_text\", \"test\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6a938d1b5da352c1346dcc5873b28206afd42fb4","scrolled":true},"cell_type":"code","source":"train = run_text_preprocessing(train, \"question_text\", \"train\")\ntest = run_text_preprocessing(test, \"question_text\", \"test\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ac16f2f29a06042a571f88557c7425b16f4db776"},"cell_type":"code","source":"df_texts = pd.concat([train.drop('target', axis=1),test])\ndf_texts.to_csv(\"texts_processed.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7bbc8e943962ebd7a1be389f96b7c908570706da"},"cell_type":"code","source":"del(train)\ndel(test)\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0285e1dd96bae050fbbbd8d3bd2aef8f3306ba69","scrolled":true},"cell_type":"code","source":"glove = '../input/embeddings/glove.840B.300d/glove.840B.300d.txt'\nparagram =  '../input/embeddings/paragram_300_sl999/paragram_300_sl999.txt'\nwiki_news = '../input/embeddings/wiki-news-300d-1M/wiki-news-300d-1M.vec'\n\nNUM_WORDS = None\nword_idx = build_vocab(df_texts[\"treated_question\"], NUM_WORDS)\nNUM_WORDS = len(word_idx) + 1\nprint(NUM_WORDS)\n\nprint(\"\\n\\n****** LOAD EMBEDDINGS ******\")\n\nprint(\"\\n---Extracting GloVe embedding---\")\nembed_glove = load_embed(glove)\nindex_embs(embed_glove, word_idx, NUM_WORDS, fileName=\"glove\")\ndel(embed_glove)\ngc.collect()\n\nprint(\"\\n---Extracting Paragram embedding---\")\nembed_paragram = load_embed(paragram)\nindex_embs(embed_paragram, word_idx, NUM_WORDS, fileName=\"paragram\")\ndel(embed_paragram)\ngc.collect()\n\n# print(\"\\n---Extracting FastText embedding---\")\n# embed_fasttext = load_embed(wiki_news)\n# index_embs(embed_fasttext, word_idx, NUM_WORDS, fileName=\"fasttext\")\n# del(embed_fasttext)\n# gc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0f8803a3d595218ddf672c986cab758240f16e2b"},"cell_type":"code","source":"print(os.listdir())\n\n\n(time.time() - startTime)/ 60","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3853b566dbd20dd3aefeeb95c54ac9fe649702c8"},"cell_type":"code","source":"# dump all variables\nimport gc\ngc.collect()\n%reset -f","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9e53a3296a2c70e1058be8ab9e3b2879b5480cc0"},"cell_type":"markdown","source":"# Data preparation"},{"metadata":{"trusted":true,"_uuid":"24084d74b9746fe5ab26da610bade25eacb83067"},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport gc\nimport time\nimport pickle\nimport copy\nimport warnings\nimport math\n\nfrom tensorflow import set_random_seed\nimport tensorflow as tf\n\nfrom sklearn.model_selection import StratifiedShuffleSplit, StratifiedKFold\nfrom sklearn.metrics import classification_report, f1_score\n\nfrom keras.engine.topology import Layer\nfrom keras.utils.generic_utils import serialize_keras_object\nfrom keras.utils.generic_utils import deserialize_keras_object\nfrom keras.legacy import interfaces\nfrom keras.preprocessing.text import Tokenizer, text_to_word_sequence\nfrom keras.preprocessing.sequence import pad_sequences\nfrom keras.models import Model\nfrom keras import backend as K\nfrom keras.layers import Input, Embedding, GRU, TimeDistributed, Dense, CuDNNGRU, Bidirectional, Dropout, SpatialDropout1D\nfrom keras.layers import concatenate, GlobalMaxPooling1D, GlobalAveragePooling1D, BatchNormalization, CuDNNLSTM, Flatten\nfrom keras import initializers, regularizers, constraints, optimizers, layers\nfrom keras.callbacks import Callback, ReduceLROnPlateau, LearningRateScheduler\nfrom keras.optimizers import Optimizer\nfrom keras import initializers\nfrom keras import regularizers\n\nimport matplotlib.pyplot as plt\n\nimport time\nstartTimePt2 = time.time()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7637da938524f4e4c30c23ebd768b97135678dcc","scrolled":true},"cell_type":"code","source":"NUM_WORDS = 192111 #200549 \nEMBEDDING_DIM = 300\nNUM_FILTERS = 100\nMAX_LEN = 72 #\nBATCH_SIZE = 512\nRANDOM_STATE = 201942\nNUM_EPOCH = 6\nLR = 0.003 # 3e-4\nLR_MAX = LR * 6 # 7e-2\nWD = 0.011 * (BATCH_SIZE / 979591 / NUM_EPOCH)**0.5 ## 979591  1044897\nSTEP_SIZE_CLR = 2 * (int(979591 * 2) / BATCH_SIZE)\n\n# Suggested weight decay factor from the paper: w = w_norm * (b/B/T)**0.5\n# b: batch size\n# B: total number of training points per epoch\n# T: total number of epochs\n# w_norm: designed weight decay factor (w is the normalized one).","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cfdb9afb74afa59183105997dcfe2fb4fb271f22"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a5b8e8af76d61c1431605f53e62945453778d1a2"},"cell_type":"code","source":"np.random.seed(RANDOM_STATE)\nset_random_seed(RANDOM_STATE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"995197d120e6d142155703178f876d98a9bb2edb"},"cell_type":"code","source":"df_train = pd.read_csv(\"train_processed_text.csv\")\ndf_test = pd.read_csv(\"test_processed_text.csv\")\nprint(\"Train shape : \", df_train.shape)\nprint(\"Test shape : \", df_test.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9c4938db1d527de5737246e1a3316a8206e8910d"},"cell_type":"code","source":"## fill up the missing values\nX_train = df_train[\"treated_question\"].fillna(\"_na_\").values\nX_test = df_test[\"treated_question\"].fillna(\"_na_\").values\n\ny_train = df_train['target'].values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a8d6303322d5c99fba3a28efe1a8edf356828631"},"cell_type":"code","source":"# Text Features\nX_features_train = np.load(\"train_features.npy\")\nX_features_test = np.load(\"test_features.npy\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"152a5692b8a99a42f5b1e53919825230a2756698"},"cell_type":"code","source":"# loading\nwith open('tokenizer.pickle', 'rb') as handle:\n    tokenizer = pickle.load(handle)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"aae990e4a4cff8ad73d936b2498aa6f6d1628ec8"},"cell_type":"code","source":"NUM_WORDS = len(tokenizer.word_index) + 1\nNUM_WORDS","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f964197834fcdefe52507cbd05c4102b39096f06"},"cell_type":"code","source":"X_train = tokenizer.texts_to_sequences(X_train)\nX_test = tokenizer.texts_to_sequences(X_test)\nprint(\"hello word\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5faa252e4d2de4b3162059ace24b193e0aefa720"},"cell_type":"code","source":"## Pad the sentences \nX_train = pad_sequences(X_train, maxlen=MAX_LEN)\nX_test = pad_sequences(X_test, maxlen=MAX_LEN)\nprint(\"hello word\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"daab9bee7701865ac5cfb1e14b06010d4bce1f0c"},"cell_type":"code","source":"X_train[0].shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0cd53d3b907b7178f26b0870895c84a2d052c616"},"cell_type":"code","source":"del(df_train)\ndel(df_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1eff697fe5c2aed29fa210edf5811d26f42b12a7"},"cell_type":"code","source":"embedding_matrix_1 = np.load(\"glove.npy\")\n# embedding_matrix_2 = np.load(\"fasttext.npy\")\nembedding_matrix_3 = np.load(\"paragram.npy\")\nprint(\"Done...\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"88573e2ac6b4c78766d521694b1fae075b4c6d84"},"cell_type":"code","source":"embedding_matrix = np.mean([embedding_matrix_1, embedding_matrix_3], axis = 0)\nnp.shape(embedding_matrix)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0ab636ca8b9ab7c27f01e4e7c96f458cf285484a"},"cell_type":"code","source":"del(embedding_matrix_1)\n# del(embedding_matrix_2)\ndel(embedding_matrix_3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"711ddca533b84d0559d9de1bf6942950df2bfa23"},"cell_type":"code","source":"gc.collect()\nprint(\"done\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"63cee59b85e720c1a50a87471f65d3b8aaaa9561"},"cell_type":"markdown","source":"[](http://)\n# Model"},{"metadata":{"trusted":true,"_uuid":"a8e942029c240a8da749cd573ce62240c83f48c2"},"cell_type":"code","source":"class CyclicLR(Callback):\n    \"\"\"This callback implements a cyclical learning rate policy (CLR).\n    The method cycles the learning rate between two boundaries with\n    some constant frequency, as detailed in this paper (https://arxiv.org/abs/1506.01186).\n    The amplitude of the cycle can be scaled on a per-iteration or \n    per-cycle basis.\n    This class has three built-in policies, as put forth in the paper.\n    \"triangular\":\n        A basic triangular cycle w/ no amplitude scaling.\n    \"triangular2\":\n        A basic triangular cycle that scales initial amplitude by half each cycle.\n    \"exp_range\":\n        A cycle that scales initial amplitude by gamma**(cycle iterations) at each \n        cycle iteration.\n    For more detail, please see paper.\n    \n    # Example\n        ```python\n            clr = CyclicLR(base_lr=0.001, max_lr=0.006,\n                                step_size=2000., mode='triangular')\n            model.fit(X_train, Y_train, callbacks=[clr])\n        ```\n    \n    Class also supports custom scaling functions:\n        ```python\n            clr_fn = lambda x: 0.5*(1+np.sin(x*np.pi/2.))\n            clr = CyclicLR(base_lr=0.001, max_lr=0.006,\n                                step_size=2000., scale_fn=clr_fn,\n                                scale_mode='cycle')\n            model.fit(X_train, Y_train, callbacks=[clr])\n        ```    \n    # Arguments\n        base_lr: initial learning rate which is the\n            lower boundary in the cycle.\n        max_lr: upper boundary in the cycle. Functionally,\n            it defines the cycle amplitude (max_lr - base_lr).\n            The lr at any cycle is the sum of base_lr\n            and some scaling of the amplitude; therefore \n            max_lr may not actually be reached depending on\n            scaling function.\n        step_size: number of training iterations per\n            half cycle. Authors suggest setting step_size\n            2-8 x training iterations in epoch.\n        mode: one of {triangular, triangular2, exp_range}.\n            Default 'triangular'.\n            Values correspond to policies detailed above.\n            If scale_fn is not None, this argument is ignored.\n        gamma: constant in 'exp_range' scaling function:\n            gamma**(cycle iterations)\n        scale_fn: Custom scaling policy defined by a single\n            argument lambda function, where \n            0 <= scale_fn(x) <= 1 for all x >= 0.\n            mode paramater is ignored \n        scale_mode: {'cycle', 'iterations'}.\n            Defines whether scale_fn is evaluated on \n            cycle number or cycle iterations (training\n            iterations since start of cycle). Default is 'cycle'.\n    \"\"\"\n\n    def __init__(self, base_lr=0.001, max_lr=0.006, step_size=2000., mode='triangular',\n                 gamma=1., scale_fn=None, scale_mode='cycle'):\n        super(CyclicLR, self).__init__()\n\n        self.base_lr = base_lr\n        self.max_lr = max_lr\n        self.step_size = step_size\n        self.mode = mode\n        self.gamma = gamma\n        if scale_fn == None:\n            if self.mode == 'triangular':\n                self.scale_fn = lambda x: 1.\n                self.scale_mode = 'cycle'\n            elif self.mode == 'triangular2':\n                self.scale_fn = lambda x: 1/(2.**(x-1))\n                self.scale_mode = 'cycle'\n            elif self.mode == 'exp_range':\n                self.scale_fn = lambda x: gamma**(x)\n                self.scale_mode = 'iterations'\n        else:\n            self.scale_fn = scale_fn\n            self.scale_mode = scale_mode\n        self.clr_iterations = 0.\n        self.trn_iterations = 0.\n        self.history = {}\n\n        self._reset()\n\n    def _reset(self, new_base_lr=None, new_max_lr=None,\n               new_step_size=None):\n        \"\"\"Resets cycle iterations.\n        Optional boundary/step size adjustment.\n        \"\"\"\n        if new_base_lr != None:\n            self.base_lr = new_base_lr\n        if new_max_lr != None:\n            self.max_lr = new_max_lr\n        if new_step_size != None:\n            self.step_size = new_step_size\n        self.clr_iterations = 0.\n        \n    def clr(self):\n        cycle = np.floor(1+self.clr_iterations/(2*self.step_size))\n        x = np.abs(self.clr_iterations/self.step_size - 2*cycle + 1)\n        if self.scale_mode == 'cycle':\n            return self.base_lr + (self.max_lr-self.base_lr)*np.maximum(0, (1-x))*self.scale_fn(cycle)\n        else:\n            return self.base_lr + (self.max_lr-self.base_lr)*np.maximum(0, (1-x))*self.scale_fn(self.clr_iterations)\n        \n    def on_train_begin(self, logs={}):\n        logs = logs or {}\n\n        if self.clr_iterations == 0:\n            K.set_value(self.model.optimizer.lr, self.base_lr)\n        else:\n            K.set_value(self.model.optimizer.lr, self.clr())        \n            \n    def on_batch_end(self, epoch, logs=None):\n        \n        logs = logs or {}\n        self.trn_iterations += 1\n        self.clr_iterations += 1\n\n#         self.history.setdefault('lr', []).append(K.get_value(self.model.optimizer.lr))\n#         self.history.setdefault('iterations', []).append(self.trn_iterations)\n\n#         for k, v in logs.items():\n#             self.history.setdefault(k, []).append(v)\n        \n        K.set_value(self.model.optimizer.lr, self.clr())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e2035acd9e8370fdce617d4d840ae2b2ba8ba8ce"},"cell_type":"code","source":"def f1(y_true, y_pred):\n    '''\n    metric from here \n    https://stackoverflow.com/questions/43547402/how-to-calculate-f1-macro-in-keras\n    '''\n    def recall(y_true, y_pred):\n        \"\"\"Recall metric.\n\n        Only computes a batch-wise average of recall.\n\n        Computes the recall, a metric for multi-label classification of\n        how many relevant items are selected.\n        \"\"\"\n        true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n        possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))\n        recall = true_positives / (possible_positives + K.epsilon())\n        \n        return recall\n    \n    def precision(y_true, y_pred):\n        \"\"\"Precision metric.\n\n        Only computes a batch-wise average of precision.\n\n        Computes the precision, a metric for multi-label classification of\n        how many selected items are relevant.\n        \"\"\"\n        true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n        predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))\n        precision = true_positives / (predicted_positives + K.epsilon())\n        return precision\n    \n    precision = precision(y_true, y_pred)\n    recall = recall(y_true, y_pred)\n\n    return 2*((precision*recall)/(precision+recall+K.epsilon()))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"af1718a594eed5083f89f04c35233aa501fc92b3"},"cell_type":"code","source":"class AdamW(Optimizer):\n    \"\"\"Adam optimizer.\n    Default parameters follow those provided in the original paper.\n    # Arguments\n        lr: float >= 0. Learning rate.\n        beta_1: float, 0 < beta < 1. Generally close to 1.\n        beta_2: float, 0 < beta < 1. Generally close to 1.\n        epsilon: float >= 0. Fuzz factor.\n        decay: float >= 0. Learning rate decay over each update.\n        weight_decay: float >= 0. Decoupled weight decay over each update.\n    # References\n        - [Adam - A Method for Stochastic Optimization](http://arxiv.org/abs/1412.6980v8)\n        - [Optimization for Deep Learning Highlights in 2017](http://ruder.io/deep-learning-optimization-2017/index.html)\n        - [Fixing Weight Decay Regularization in Adam](https://arxiv.org/abs/1711.05101)\n    \"\"\"\n\n    def __init__(self, lr=0.001, beta_1=0.9, beta_2=0.999, weight_decay=1e-4,  # decoupled weight decay (1/6)\n                 epsilon=1e-8, decay=0., **kwargs):\n        super(AdamW, self).__init__(**kwargs)\n        with K.name_scope(self.__class__.__name__):\n            self.iterations = K.variable(0, dtype='int64', name='iterations')\n            self.lr = K.variable(lr, name='lr')\n            self.init_lr = lr # decoupled weight decay (2/6)\n            self.beta_1 = K.variable(beta_1, name='beta_1')\n            self.beta_2 = K.variable(beta_2, name='beta_2')\n            self.decay = K.variable(decay, name='decay')\n            self.wd = K.variable(weight_decay, name='weight_decay') # decoupled weight decay (3/6)\n        self.epsilon = epsilon\n        self.initial_decay = decay\n\n    @interfaces.legacy_get_updates_support\n    def get_updates(self, loss, params):\n        grads = self.get_gradients(loss, params)\n        self.updates = [K.update_add(self.iterations, 1)]\n        wd = self.wd # decoupled weight decay (4/6)\n\n        lr = self.lr\n        if self.initial_decay > 0:\n            lr *= (1. / (1. + self.decay * K.cast(self.iterations,\n                                                  K.dtype(self.decay))))\n        eta_t = lr / self.init_lr # decoupled weight decay (5/6)\n\n        t = K.cast(self.iterations, K.floatx()) + 1\n        lr_t = lr * (K.sqrt(1. - K.pow(self.beta_2, t)) /\n                     (1. - K.pow(self.beta_1, t)))\n\n        ms = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params]\n        vs = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params]\n        self.weights = [self.iterations] + ms + vs\n\n        for p, g, m, v in zip(params, grads, ms, vs):\n            m_t = (self.beta_1 * m) + (1. - self.beta_1) * g\n            v_t = (self.beta_2 * v) + (1. - self.beta_2) * K.square(g)\n            p_t = p - lr_t * m_t / (K.sqrt(v_t) + self.epsilon) - eta_t * wd * p # decoupled weight decay (6/6)\n\n            self.updates.append(K.update(m, m_t))\n            self.updates.append(K.update(v, v_t))\n            new_p = p_t\n\n            # Apply constraints.\n            if getattr(p, 'constraint', None) is not None:\n                new_p = p.constraint(new_p)\n\n            self.updates.append(K.update(p, new_p))\n        return self.updates\n\n    def get_config(self):\n        config = {'lr': float(K.get_value(self.lr)),\n                  'beta_1': float(K.get_value(self.beta_1)),\n                  'beta_2': float(K.get_value(self.beta_2)),\n                  'decay': float(K.get_value(self.decay)),\n                  'weight_decay': float(K.get_value(self.wd)),\n                  'epsilon': self.epsilon}\n        base_config = super(AdamW, self).get_config()\n        return dict(list(base_config.items()) + list(config.items()))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c58b0e050cc1001a2c9ed2a54dc0fff87025e5f5"},"cell_type":"code","source":"# Kernel\nglorotInit = initializers.glorot_uniform(seed=RANDOM_STATE)\n# Recurrent\northoInit = initializers.Orthogonal(seed=RANDOM_STATE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"954f8c43d0bbdd31f6db5b15af4d34aaa73e690e"},"cell_type":"code","source":"def squash(x, axis=-1):\n    # s_squared_norm is really small\n    # s_squared_norm = K.sum(K.square(x), axis, keepdims=True) + K.epsilon()\n    # scale = K.sqrt(s_squared_norm)/ (0.5 + s_squared_norm)\n    # return scale * x\n    s_squared_norm = K.sum(K.square(x), axis, keepdims=True)\n    scale = K.sqrt(s_squared_norm + K.epsilon())\n    return x / scale\n\n# A Capsule Implement with Pure Keras\nclass Capsule(Layer):\n    def __init__(self, num_capsule, dim_capsule, routings=3, kernel_size=(9, 1), share_weights=True,\n                 activation='default', **kwargs):\n        super(Capsule, self).__init__(**kwargs)\n        self.num_capsule = num_capsule\n        self.dim_capsule = dim_capsule\n        self.routings = routings\n        self.kernel_size = kernel_size\n        self.share_weights = share_weights\n        if activation == 'default':\n            self.activation = squash\n        else:\n            self.activation = Activation(activation)\n            \n    def build(self, input_shape):\n        super(Capsule, self).build(input_shape)\n        input_dim_capsule = input_shape[-1]\n        if self.share_weights:\n            self.W = self.add_weight(name='capsule_kernel',\n                                     shape=(1, input_dim_capsule,\n                                            self.num_capsule * self.dim_capsule),\n                                     # shape=self.kernel_size,\n                                     initializer=glorotInit,\n                                     trainable=True)\n        else:\n            input_num_capsule = input_shape[-2]\n            self.W = self.add_weight(name='capsule_kernel',\n                                     shape=(input_num_capsule,\n                                            input_dim_capsule,\n                                            self.num_capsule * self.dim_capsule),\n                                     initializer=glorotInit,\n                                     trainable=True)\n\n    def call(self, u_vecs):\n        if self.share_weights:\n            u_hat_vecs = K.conv1d(u_vecs, self.W)\n        else:\n            u_hat_vecs = K.local_conv1d(u_vecs, self.W, [1], [1])\n\n        batch_size = K.shape(u_vecs)[0]\n        input_num_capsule = K.shape(u_vecs)[1]\n        u_hat_vecs = K.reshape(u_hat_vecs, (batch_size, input_num_capsule,\n                                            self.num_capsule, self.dim_capsule))\n        u_hat_vecs = K.permute_dimensions(u_hat_vecs, (0, 2, 1, 3))\n        # final u_hat_vecs.shape = [None, num_capsule, input_num_capsule, dim_capsule]\n        \n        b = K.zeros_like(u_hat_vecs[:, :, :, 0])  # shape = [None, num_capsule, input_num_capsule]\n        for i in range(self.routings):\n            b = K.permute_dimensions(b, (0, 2, 1))  # shape = [None, input_num_capsule, num_capsule]\n            c = K.softmax(b)\n            c = K.permute_dimensions(c, (0, 2, 1))\n            b = K.permute_dimensions(b, (0, 2, 1))\n            outputs = self.activation(tf.keras.backend.batch_dot(c, u_hat_vecs, [2, 2]))\n            if i < self.routings - 1:\n                b = tf.keras.backend.batch_dot(outputs, u_hat_vecs, [2, 3])\n\n        return outputs\n\n    def compute_output_shape(self, input_shape):\n        return (None, self.num_capsule, self.dim_capsule)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ac2647efdbb24eb36848f8860e8171bf1a80c782"},"cell_type":"code","source":"NUM_WORDS","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"86020e12d9b92e145051b5eb947c3dd543163765"},"cell_type":"code","source":"def capsule_model():\n    K.clear_session() \n    \n    inp_features = Input(shape=(6,)) \n    \n    inp = Input(shape=(MAX_LEN,))    \n    emb = Embedding(NUM_WORDS, EMBEDDING_DIM, weights=[embedding_matrix], trainable=False)(inp)\n    emb_drop = SpatialDropout1D(rate=0.2)(emb)\n    rnn = Bidirectional(CuDNNLSTM(75, return_sequences=True, \n                                kernel_initializer=glorotInit, recurrent_initializer=orthoInit))(emb_drop)\n\n    caps = Capsule(num_capsule=4, dim_capsule=10, routings=8, share_weights=True)(rnn)\n    flat = Flatten()(caps)\n\n    dense_1 = Dense(100, activation=\"elu\", kernel_initializer=glorotInit)(flat)\n    \n    concat = concatenate([dense_1, inp_features])\n    \n    out_drop = Dropout(0.12)(concat)\n    out_bn = BatchNormalization()(out_drop)\n    out = Dense(1, activation=\"sigmoid\")(out_bn)\n    \n    model = Model(inputs=[inp_features, inp], outputs=out)\n    model.compile(loss='binary_crossentropy', optimizer=AdamW(lr=LR, weight_decay=WD), metrics=[\"acc\", f1])\n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8b7a4afb027d6436580b690ade61bb8f98c10de1"},"cell_type":"code","source":"def threshold_search(y_true, y_proba):\n    best_threshold = 0\n    best_score = 0\n    \n    for threshold in [i * 0.01 for i in range(100)]:\n        score = f1_score(y_true=y_true, y_pred=y_proba > threshold)\n        if score > best_score:\n            best_threshold = threshold\n            best_score = score\n    search_result = {'threshold': best_threshold, 'f1': best_score}\n    return search_result","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a4b483f0d77ff50f672048019002dace836f17cc"},"cell_type":"code","source":"def step_decay(epoch):\n    initial_lrate = LR\n    drop = 0.6\n    epochs_drop = 5.0\n    lrate = initial_lrate * math.pow(drop, math.floor((1+epoch)/epochs_drop))\n    return lrate","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"690eebda00f0d0a852c1565aa4ad40695de50b84"},"cell_type":"code","source":"# clr = CyclicLR(base_lr=LR, max_lr=LR_MAX,\n#                step_size=STEP_SIZE_CLR, mode='exp_range',\n#                gamma=0.99994)\nclr = CyclicLR(base_lr=0.001, max_lr=0.003,\n               step_size=300., mode='exp_range',\n               gamma=0.99994)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f04076c4eacb49aa36820e2ebebb251a8cf79109"},"cell_type":"code","source":"# https://www.kaggle.com/strideradu/word2vec-and-gensim-go-go-go\ndef train_pred(model, train_X,train_features_X, train_y, val_X, val_features_X, val_y, test_data, callbacks=None):\n    st = time.time()      \n    model.fit([train_features_X, np.array(train_X)], train_y, batch_size=BATCH_SIZE, epochs=NUM_EPOCH, \n              validation_data=([val_features_X, np.array(val_X)], val_y), callbacks = callbacks, verbose=2)\n    \n    pred_val_y = model.predict([val_features_X, val_X], batch_size=1024, verbose=0)\n\n    best_thresh = threshold_search(val_y, pred_val_y)\n    \n    print(\"\\tVal F1 Score: {:.4f}\\tThresh: {:.2f}\".format(best_thresh[\"f1\"], best_thresh[\"threshold\"]))\n    best_score = best_thresh[\"f1\"]\n    pred_test_y = model.predict(test_data, batch_size=1024, verbose=0)\n    print(\"Training time was: \" + str(time.time() - st))\n    print('=' * 60)\n    \n    return pred_val_y, pred_test_y, best_score","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7c545f61f44c3d2b8bc3cebe905b94baa72f9292","scrolled":false},"cell_type":"code","source":"train_meta = np.zeros(y_train.shape)\ntest_meta = np.zeros(len(X_test))\n\nsplits = list(StratifiedKFold(n_splits=5, shuffle=True, random_state=RANDOM_STATE).split(X_train, y_train))\n\nfor idx, (train_idx, valid_idx) in enumerate(splits):\n    X_train_tmp = X_train[train_idx]\n    X_features_train_tmp = X_features_train[train_idx]\n    y_train_tmp = y_train[train_idx]\n    \n    X_val_tmp =  X_train[valid_idx]\n    X_features_val_tmp = X_features_train[valid_idx]\n    y_val_tmp = y_train[valid_idx]\n    \n    model = capsule_model()   \n    #reduceLR = ReduceLROnPlateau(monitor='val_loss', factor=0.6, patience=1, min_lr=0.0001, verbose=2)\n    #scheduleLR = LearningRateScheduler(schedule=step_decay, verbose=2)\n    pred_val_y, pred_test_y, best_score = train_pred(model, X_train_tmp, X_features_train_tmp, y_train_tmp, X_val_tmp, \n                                                     X_features_val_tmp, y_val_tmp, [X_features_test ,X_test], \n                                                     callbacks=[clr])\n    \n    train_meta[valid_idx] = pred_val_y.reshape(-1)\n    test_meta += pred_test_y.reshape(-1) / len(splits)\n\n    del(X_train_tmp)\n    del(y_train_tmp)\n    del(X_val_tmp)\n    del(y_val_tmp)\n    gc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"aa2153de9208455ad9c883ac21f93f04b435e922"},"cell_type":"markdown","source":"using CLR"},{"metadata":{"_uuid":"46727204154d58fd1e0fbe4006718aa3947df8c0"},"cell_type":"markdown","source":" # Prediction"},{"metadata":{"trusted":true,"_uuid":"524d4ef5518c4969f26ac15ccc39dbf2fc9d8f65"},"cell_type":"code","source":"search_result = threshold_search(y_train, train_meta)\nprint(search_result)\n\nsub = pd.read_csv('../input/sample_submission.csv')\nsub.prediction = test_meta > search_result['threshold']\nsub.to_csv(\"submission.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"830d167b93a76e24832d727511c845a1c265e315"},"cell_type":"code","source":"(time.time() - startTimePt2) / 60","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dc1d4f2cc024231963ab551da2b8b1fb50719a8a","scrolled":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4e68fbd01fd6f79703cfb6f0a924ec61f3d4a886"},"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}