{"cells":[{"metadata":{"trusted":true,"_uuid":"480e74ee9b1fc0557ffd9fad749ea781bdb5d4a2"},"cell_type":"code","source":"from __future__ import division\nimport os\nos.environ['PYTHONHASHSEED'] = '10000'\n#os.environ['CUDA_VISIBLE_DEVICES']=\"1\"\n\nimport random\nimport numpy as np\nnp.random.seed(10001)\nrandom.seed(10002)\n#os.environ['OMP_NUM_THREADS'] = '4'\n\n#import pyximport\n#pyximport.install()\n\n\nimport tensorflow as tf\ntf.set_random_seed(10003)\n\n#session_conf = tf.ConfigProto(intra_op_parallelism_threads=5, inter_op_parallelism_threads=1)\nfrom keras import backend as K\n\n\n#K.set_session(tf.Session(graph=tf.get_default_graph(), config=session_conf))\n\nimport pandas as pd\n\nfrom keras.preprocessing.sequence import pad_sequences\nfrom keras.layers import Input, Dropout, Dense, concatenate,  Embedding, Flatten, Activation, PReLU, CuDNNLSTM, CuDNNGRU, Lambda, add, multiply\nfrom keras.layers import Conv1D, Bidirectional, SpatialDropout1D, BatchNormalization\nfrom keras.layers import GlobalMaxPooling1D, GlobalAveragePooling1D, MaxPool1D, AveragePooling1D\nfrom keras.optimizers import Adam, SGD, Nadam, RMSprop\nfrom keras.models import Model\nfrom keras.regularizers import l2\nfrom keras.callbacks import Callback\n\nimport pandas as pd \nfrom sklearn.preprocessing import LabelEncoder\nfrom collections import defaultdict\nfrom sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer\nfrom scipy.sparse import csr_matrix, hstack\nfrom itertools import combinations\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.linear_model import RidgeClassifier\nfrom sklearn.kernel_ridge import KernelRidge\nfrom sklearn.ensemble import AdaBoostClassifier, RandomForestClassifier, ExtraTreesClassifier\nfrom sklearn.metrics import roc_auc_score, f1_score, log_loss\nfrom sklearn.model_selection import StratifiedKFold\n\n\nimport re\nfrom sklearn.metrics import mean_squared_error\nfrom sklearn.linear_model import Ridge\nfrom multiprocessing import Pool\nimport gc\nimport time\nimport nltk\nfrom nltk import ngrams\nfrom nltk.stem import WordNetLemmatizer\nwordnet_lemmatizer = WordNetLemmatizer()\nfrom nltk.corpus import stopwords\nstop_words = set(stopwords.words('english'))\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.ensemble import ExtraTreesClassifier, RandomForestClassifier\n###################################################################\n\nfrom IPython.display import display","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e7651eb5e04b64eb4cf587cccb374796e1ac964c"},"cell_type":"code","source":"###################################################################\n#GLOBAL VARIABLES\npath = '../input/'\nembedding_path = '../input/embeddings/'\ncores = 4\nmax_text_length=50###################\ndo_submission = False # use -1 for submission, otherwise tha value of split is the number of instances in train\n\nmin_df_one=1\nkeep_only_words_in_embedding = True\nkeep_unknown_words_in_keras_sequence_as_zeros = True\n\ncontraction_mapping = {u\"ain't\": u\"is not\", u\"aren't\": u\"are not\",u\"can't\": u\"cannot\", u\"'cause\": u\"because\",\n                       u\"could've\": u\"could have\", u\"couldn't\": u\"could not\", u\"didn't\": u\"did not\",\n                       u\"doesn't\": u\"does not\", u\"don't\": u\"do not\", u\"hadn't\": u\"had not\",\n                       u\"hasn't\": u\"has not\", u\"haven't\": u\"have not\", u\"he'd\": u\"he would\",\n                       u\"he'll\": u\"he will\", u\"he's\": u\"he is\", u\"how'd\": u\"how did\", u\"how'd'y\": u\"how do you\",\n                       u\"how'll\": u\"how will\", u\"how's\": u\"how is\",  u\"I'd\": u\"I would\",\n                       u\"I'd've\": u\"I would have\", u\"I'll\": u\"I will\", u\"I'll've\": u\"I will have\",\n                       u\"I'm\": u\"I am\", u\"I've\": u\"I have\", u\"i'd\": u\"i would\", u\"i'd've\": u\"i would have\",\n                       u\"i'll\": u\"i will\",  u\"i'll've\": u\"i will have\",u\"i'm\": u\"i am\", u\"i've\": u\"i have\",\n                       u\"isn't\": u\"is not\", u\"it'd\": u\"it would\", u\"it'd've\": u\"it would have\",\n                       u\"it'll\": u\"it will\", u\"it'll've\": u\"it will have\",u\"it's\": u\"it is\",\n                       u\"let's\": u\"let us\", u\"ma'am\": u\"madam\", u\"mayn't\": u\"may not\",\n                       u\"might've\": u\"might have\",u\"mightn't\": u\"might not\",u\"mightn't've\": u\"might not have\",\n                       u\"must've\": u\"must have\", u\"mustn't\": u\"must not\", u\"mustn't've\": u\"must not have\",\n                       u\"needn't\": u\"need not\", u\"needn't've\": u\"need not have\",u\"o'clock\": u\"of the clock\",\n                       u\"oughtn't\": u\"ought not\", u\"oughtn't've\": u\"ought not have\", u\"shan't\": u\"shall not\", \n                       u\"sha'n't\": u\"shall not\", u\"shan't've\": u\"shall not have\", u\"she'd\": u\"she would\",\n                       u\"she'd've\": u\"she would have\", u\"she'll\": u\"she will\", u\"she'll've\": u\"she will have\",\n                       u\"she's\": u\"she is\", u\"should've\": u\"should have\", u\"shouldn't\": u\"should not\",\n                       u\"shouldn't've\": u\"should not have\", u\"so've\": u\"so have\",u\"so's\": u\"so as\",\n                       u\"this's\": u\"this is\",u\"that'd\": u\"that would\", u\"that'd've\": u\"that would have\",\n                       u\"that's\": u\"that is\", u\"there'd\": u\"there would\", u\"there'd've\": u\"there would have\",\n                       u\"there's\": u\"there is\", u\"here's\": u\"here is\",u\"they'd\": u\"they would\", \n                       u\"they'd've\": u\"they would have\", u\"they'll\": u\"they will\", \n                       u\"they'll've\": u\"they will have\", u\"they're\": u\"they are\", u\"they've\": u\"they have\", \n                       u\"to've\": u\"to have\", u\"wasn't\": u\"was not\", u\"we'd\": u\"we would\",\n                       u\"we'd've\": u\"we would have\", u\"we'll\": u\"we will\", u\"we'll've\": u\"we will have\", \n                       u\"we're\": u\"we are\", u\"we've\": u\"we have\", u\"weren't\": u\"were not\",\n                       u\"what'll\": u\"what will\", u\"what'll've\": u\"what will have\", u\"what're\": u\"what are\",\n                       u\"what's\": u\"what is\", u\"what've\": u\"what have\", u\"when's\": u\"when is\",\n                       u\"when've\": u\"when have\", u\"where'd\": u\"where did\", u\"where's\": u\"where is\",\n                       u\"where've\": u\"where have\", u\"who'll\": u\"who will\", u\"who'll've\": u\"who will have\",\n                       u\"who's\": u\"who is\", u\"who've\": u\"who have\", u\"why's\": u\"why is\", u\"why've\": u\"why have\",\n                       u\"will've\": u\"will have\", u\"won't\": u\"will not\", u\"won't've\": u\"will not have\",\n                       u\"would've\": u\"would have\", u\"wouldn't\": u\"would not\", u\"wouldn't've\": u\"would not have\",\n                       u\"y'all\": u\"you all\", u\"y'all'd\": u\"you all would\",u\"y'all'd've\": u\"you all would have\",\n                       u\"y'all're\": u\"you all are\",u\"y'all've\": u\"you all have\",u\"you'd\": u\"you would\",\n                       u\"you'd've\": u\"you would have\", u\"you'll\": u\"you will\", u\"you'll've\": u\"you will have\",\n                       u\"you're\": u\"you are\", u\"you've\": u\"you have\" }","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f08b5ebfd8cddc166b2ed89be910ff9f4218d503"},"cell_type":"code","source":"from gensim.models import KeyedVectors, fasttext\n\ndef load_glove_words():\n    EMBEDDING_FILE = embedding_path+'glove.840B.300d/glove.840B.300d.txt'\n    def get_coefs(word,*arr): return word, 1\n    embeddings_index1 = dict(get_coefs(*o.split(\" \")) for o in open(EMBEDDING_FILE))\n    \n    EMBEDDING_FILE = embedding_path+'paragram_300_sl999/paragram_300_sl999.txt'\n    embeddings_index2  = dict(get_coefs(*o.split(\" \")) for o in open(EMBEDDING_FILE, encoding='utf-8', errors='ignore'))\n    #embeddings_index2  = dict(get_coefs(*o.split(\" \")) for o in open(EMBEDDING_FILE))\n    \n    #EMBEDDING_FILE = embedding_path+'wiki-news-300d-1M/wiki-news-300d-1M.vec'\n    #embeddings_index3  = dict(get_coefs(*o.split(\" \")) for o in open(EMBEDDING_FILE) if len(o)>100)\n    \n    return ( set(list(embeddings_index1.keys())) ).union( set(list(embeddings_index2.keys())) )#.union( set(list(embeddings_index3.keys())) )\n    \n    #EMBEDDING_FILE = embedding_path+'GoogleNews-vectors-negative300/GoogleNews-vectors-negative300.bin'\n    #embeddings_index2 = KeyedVectors.load_word2vec_format(EMBEDDING_FILE, binary=True)\n    \n    #return set(list(embeddings_index1.keys()))\n\n\nembeddings_index = load_glove_words()\nprint( len(embeddings_index) )\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"50ab701f48adab9ab64f28ed7f433a81fc3b0fde"},"cell_type":"code","source":"def clean_str(text):\n    \n    text = re.sub( u\"\\[math\\].*\\[\\/math\\]\", u\" math \", text) \n    text = re.sub( u\"\\S*@\\S*\\.\\S*\", u\" email \", text) \n        \n    text = u\" \".join( re.sub(u\"^\\d+(?:[.,]\\d*)?$\", u\"number\", w)  for w in text.split(\" \"))\n    \n    specials = [u\"’\", u\"‘\", u\"´\", u\"`\", u\"\\u2019\"]\n    for s in specials:\n        text = u\" \".join( w.replace(s, u\"'\")  for w in text.split(\" \"))\n        \n        \n    #text = u\" \".join( [w.strip() if w in embeddings_index else clean(w).strip() for w in text.split(\" \")] ) \n    text = u\" \".join( [w.strip() if w in embeddings_index else clean(w).strip() for w in text.split(\" \")] ) \n    text = re.sub( u\"\\s+\", u\" \", text ).strip()\n    text = u\" \".join( [w.strip()  for w in text.split(\" \")  if w in embeddings_index ] ) \n    \n    #text = u\" \".join( [w for w in text.split()] )#max_text_length\n    \n    return text\n\n    \ndef clean(text):\n    try:\n        if text in contraction_mapping:\n            return contraction_mapping[text]\n        \n        for i, j in [ (u\"é\", u\"e\"), (u\"ē\", u\"e\"), (u\"è\", u\"e\"), (u\"ê\", u\"e\"), (u\"à\", u\"a\"),\n                    (u\"â\", u\"a\"), (u\"ô\", u\"o\"), (u\"ō\", u\"o\"), (u\"ü\", u\"u\"), (u\"ï\", u\"i\"),\n                    (u\"ç\", u\"c\"), (u\"\\xed\", u\"i\")]:\n            text = re.sub(i, j, text)\n        if text in embeddings_index:\n            return text\n        \n        \n        text = text.lower()\n        if text in embeddings_index:\n            return text\n \n        text = re.sub(u\"[^a-z\\s0-9]\", u\" \", text)\n        text = u\" \".join( re.sub(u\"^\\d+(?:[.,]\\d*)?$\", u\"number\", w)  for w in text.split(\" \"))\n        text = re.sub(u\"[^a-z\\s]\", u\" \", text)\n        text = re.sub( u\"\\s+\", u\" \", text ).strip()\n       \n        text = u\" \".join( [ wordnet_lemmatizer.lemmatize(w) if w not in embeddings_index else w for w in text.split() ] )\n         \n    except:\n        print ('ERROR')\n        text = ''\n    return text\n\n###################################################################\n\ndef parallelize_dataframe(df, func):\n    df_split = np.array_split(df, cores)\n    pool = Pool(cores)\n    df = pd.concat(pool.map(func, df_split))\n    pool.close()\n    pool.join()\n    return df\ndef clean_str_df(df):\n    return df.apply( lambda s : clean_str(s))\n\ndef prepare_data(df_data, train=True):\n    print ('Prepare data....')\n    df_data['question_text'] = parallelize_dataframe(df_data['question_text'], clean_str_df)  \n    \n    \n\n    return df_data\n###############################################################################################\n\ndef create_vocabulary( df ):\n    ###################################################################\n    #STORE ALL WORDS  FREQUENCY and Filter\n    start_time = time.time()\n    word_frequency_dc=defaultdict(np.uint32)\n    def word_count(text):\n        text = set( text.split(' ') ) \n        if keep_only_words_in_embedding:\n            for w in text:\n                if w in embeddings_index:\n                    word_frequency_dc[w]+=1\n        else:\n            for w in text:\n                word_frequency_dc[w]+=1\n\n    \n    df['question_text'].apply( lambda x : word_count(x) )\n    print('[{}] Finished COUNTING WORDS FOR question_text...'.format(time.time() - start_time))\n\n    #LABEL ENCODING\n    start_time = time.time()\n    vocabulary_dc = word_frequency_dc.copy()\n    #Keep 0 value for unknown words or low frequency words\n    cpt=1\n    for key in vocabulary_dc:\n        vocabulary_dc[key]=cpt\n        cpt+=1\n    print('[{}] Finished CREATING VOCABULARY ...'.format(time.time() - start_time))\n    return word_frequency_dc, vocabulary_dc\n\ndef tokenize(text):\n    return [w for w in text.split()]\n\ndef load_glove(word_index):\n    def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')\n    \n    EMBEDDING_FILE = embedding_path+'glove.840B.300d/glove.840B.300d.txt'\n    #embeddings_index = dict(get_coefs(*o.split(\" \")) for o in open(EMBEDDING_FILE)  if o.split(\" \")[0] in word_index )\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_mean1,emb_std1 = all_embs.mean(), all_embs.std()\n    \n    EMBEDDING_FILE = embedding_path+'paragram_300_sl999/paragram_300_sl999.txt'\n    #embeddings_index2  = dict(get_coefs(*o.split(\" \")) for o in open(EMBEDDING_FILE, encoding='utf-8', errors='ignore') if o.split(\" \")[0] in word_index )\n    embeddings_index2  = dict(get_coefs(*o.split(\" \")) for o in open(EMBEDDING_FILE, encoding='utf-8', errors='ignore')  )\n   \n    all_embs = np.stack(embeddings_index2.values())\n    emb_mean2,emb_std2 = all_embs.mean(), all_embs.std()\n    \n    embedding_matrix = np.zeros( (len(word_index)+2,  300 ) , dtype=np.float32)\n    for word, i in word_index.items():\n        embedding_vector = None\n        if (word in embeddings_index) & (word in embeddings_index2) :\n            embedding_vector = ( embeddings_index.get(word) + (embeddings_index2.get(word)-emb_mean2+emb_mean1) )/2.\n        else:            \n            if (word in embeddings_index):\n                embedding_vector = embeddings_index.get(word)\n            else:\n                if (word in embeddings_index2):\n                    embedding_vector = embeddings_index2.get(word)-emb_mean2+emb_mean1\n        #        else:\n        #            embedding_vector = embeddings_index3.get(word)       \n        if embedding_vector is not None: embedding_matrix[i] = embedding_vector.astype(np.float32)\n            \n    return embedding_matrix\n\ndef preprocess_keras(text):\n    if keep_unknown_words_in_keras_sequence_as_zeros:\n        return [ vocabulary_dc[w] if ((w in embeddings_index)&(word_frequency_dc[w]>=min_df_one)) else 0 for w in (text.split()) ]\n    else:\n        return [ vocabulary_dc[w]  for w in (text.split()) if w in embeddings_index ]\ndef preprocess_keras_df(df):\n    return df.apply( preprocess_keras )\n\n\ndef get_keras_data(df):\n    X = {\n        'num_data' : num_FE(df),\n        'question_text_in_glove': pad_sequences(df['seq_question_text_in_glove'], maxlen=max_text_length, value=0, padding='post' ),   \n        'question_text_mask_in_glove': (pad_sequences(df['seq_question_text_in_glove'], maxlen=max_text_length, value=0, padding='post' )>0).astype(int).reshape(-1,max_text_length,1),   \n        'starting_words': pad_sequences(df['seq_question_text_in_glove'].apply(lambda x : x[:10]), maxlen=10, value=0, padding='post' ),   \n        'ending_words': pad_sequences(df['seq_question_text_in_glove'].apply(lambda x : x[-10:]), maxlen=10, value=0, padding='post' )\n        }\n    return X\n\ndef num_FE(temp_df):\n    df = pd.DataFrame()\n    df['total_length'] = temp_df['question_text_original'].apply(len)\n    df['capitals'] = temp_df['question_text_original'].apply(lambda comment: sum(1 for c in comment if c.isupper()))\n    df['caps_vs_length'] = df.apply(lambda row: float(row['capitals'])/float(row['total_length']),\n                                    axis=1)\n    df['num_exclamation_marks'] = temp_df['question_text_original'].apply(lambda comment: comment.count('!'))\n    df['num_question_marks'] = temp_df['question_text_original'].apply(lambda comment: comment.count('?'))\n    df['num_punctuation'] = temp_df['question_text_original'].apply(\n        lambda comment: sum(comment.count(w) for w in '.,;:'))\n    df['num_symbols'] = temp_df['question_text_original'].apply(\n        lambda comment: sum(comment.count(w) for w in '*#$'))\n    df['num_words'] = temp_df['question_text_original'].apply(lambda comment: len(comment.split()))\n    df['num_unique_words'] = temp_df['question_text_original'].apply(\n        lambda comment: len(set(w for w in comment.split())))\n    df['words_vs_unique'] = df['num_unique_words'] / df['num_words']\n   \n    return df.values.astype(np.float32)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"93ab042be3f7034a9331444a18105fd6855d19d7"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1ed1b4bd272aceba33ae42a78f54b0fdffc7108d"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0b94f56d26bdb8e82694ea37861894653c69d2d8"},"cell_type":"code","source":"def threshold_search(y_true, y_proba):\n    best_threshold = 0\n    best_score = 0\n    for threshold in [i * 0.01 for i in range(10,70)]:\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\n\nclass My_Callback(Callback):\n    def __init__(self, save_path, factor=0.5, patience_reduce=10, patience_stop=30, min_lr=1e-4):\n        self.scores_list = []\n        self.scores_list_stop = []\n        self.thresh_list = []\n        self.all_preds = None\n        self.best_score = 0.\n        self.best_threshold = 0.5\n        self.best_epoch = 0\n        self.best_lr = 0\n        \n        self.save_path = save_path\n        self.factor = factor\n        self.min_lr = min_lr\n        self.patience_reduce = patience_reduce\n        self.patience_stop = patience_stop\n\n\n    def on_epoch_end(self, epoch, logs={}):\n        all_preds = self.all_preds\n        ep = epoch\n        best_score = self.best_score\n\n        \n        preds_valid = self.model.predict(test_keras , batch_size=2048)    \n        thresholds = threshold_search(dtest_y, preds_valid)\n        best_thres = thresholds['threshold']\n        score_all = thresholds['f1']\n        print (ep+1, '   F1    : ', \"{0:.4f}\".format(score_all), \"{0:.4f}\".format(best_thres), \n               ' AUC  : ', \"{0:.4f}\".format(roc_auc_score( dtest_y,  preds_valid>=best_thres )) )\n           \n        \n\n        #####################################\n        \n        self.scores_list.append(score_all)\n        self.thresh_list.append(best_thres)\n        \n        self.scores_list_stop.append(score_all)\n        \n        if score_all > best_score:\n            self.best_score = score_all\n            self.best_threshold = best_thres\n            self.best_epoch = ep+1\n            self.best_lr = np.float32(K.get_value(self.model.optimizer.lr))\n            self.model.save_weights(self.save_path, overwrite=True)\n            print( \"Score improved from \", \"{0:.4f}\".format(best_score), ' to ', \"{0:.4f}\".format(score_all),\\\n            'threshold : ', \"{0:.4f}\".format(self.best_threshold), ' lr : ', self.best_lr, ' epoch : ', self.best_epoch)\n        else:\n            print (\"Score didnt improve, current best score is : \", \"{0:.4f}\".format(best_score) ,\\\n            'with threshold : ', \"{0:.4f}\".format(self.best_threshold), ' lr : ', self.best_lr, ' epoch : ', self.best_epoch)\n        \n        \n        #Reduce LR\n        pos = np.argmax(self.scores_list)\n        if len(self.scores_list) - pos>self.patience_reduce:\n            old_lr = np.float32(K.get_value(self.model.optimizer.lr))\n            if old_lr > self.min_lr:\n                new_lr = old_lr * self.factor\n                new_lr = max(new_lr, self.min_lr)\n                \n                self.model.load_weights(self.save_path)\n                K.set_value(self.model.optimizer.lr, new_lr)\n                self.scores_list = self.scores_list[:pos+1]\n                self.thresh_list = self.thresh_list[:pos+1]\n                print ('*'*70)\n                print( 'Reducing LR from ', old_lr, ' to ', new_lr)\n                print( 'Loading Last best score : ', self.scores_list[-1], 'with threshold : ',self.thresh_list[-1])\n                print ('*'*70)\n                \n        #early STop\n        pos = np.argmax(self.scores_list_stop)\n        if len(self.scores_list_stop) - pos>self.patience_stop:\n            print( 'TRAINING STOP, NO MORE IMPROVEMENT')\n            print( 'LOADING BEST WEIGHTS ....')\n            self.model.load_weights(self.save_path)\n            self.stopped_epoch = ep\n            self.model.stop_training = True\n            \n        print ('')\n        return\n    \nimport keras\nclass DataGenerator(keras.utils.Sequence):\n    'Generates data for Keras'\n    def __init__(self, data, labels, shuffle=True, seed=None):\n        \n        self.epoch = 0\n        self.seed = seed\n        if self.seed is None:\n            self.seed = self.epoch\n        np.random.seed(self.seed)\n        \n        self.list_IDs = np.random.permutation(len(labels))\n   \n        self.data_num   = data['num_data']\n        self.data_start = data['starting_words']\n        self.data_end   = data['ending_words']\n        self.data_glove = data['question_text_in_glove']\n        self.data_mask  = data['question_text_mask_in_glove']\n        self.labels = labels\n        self.shuffle = shuffle\n        self.on_epoch_end()\n\n    def __len__(self):\n        'Denotes the number of batches per epoch'\n        return int(np.floor(len(self.list_IDs) / batch_size))\n\n    def __getitem__(self, index):\n        'Generate one batch of data'\n        # Generate indexes of the batch\n        indexes = self.indexes[index*batch_size:(index+1)*batch_size]\n\n        # Find list of IDs\n        list_IDs_temp = [self.list_IDs[k] for k in indexes]\n\n        # Generate data\n        X, y = self.__data_generation(list_IDs_temp)\n\n        return (X, y)\n\n    def on_epoch_end(self):\n        'Updates indexes after each epoch'\n        #self.list_IDs = self.list_IDs_ori+ list( np.random.choice( no_boats.ImageId.values, len(self.list_IDs_ori) )  )\n        self.epoch += 1\n        self.seed += 1\n        np.random.seed(self.seed)\n        self.list_IDs = np.random.permutation(len(self.labels))\n        \n        self.indexes = np.arange(len(self.list_IDs))\n        if self.shuffle == True:\n            np.random.shuffle(self.indexes)\n\n    def __data_generation(self, list_IDs_temp):\n     \n        X = {\n        'num_data'  : self.data_num[list_IDs_temp],\n        'starting_words': self.data_start[list_IDs_temp],   \n        'ending_words': self.data_end[list_IDs_temp],   \n        'question_text_in_glove': self.data_glove[list_IDs_temp],   \n        'question_text_mask_in_glove': self.data_mask[list_IDs_temp].reshape(-1,max_text_length,1),                 \n    }\n    \n\n        return X , self.labels[list_IDs_temp]\n    \n\n    \n\ndef new_rnn_model():\n    sequence_in_glove = Input(shape=[train_keras[\"question_text_in_glove\"].shape[1]], name=\"question_text_in_glove\")\n    mask_in_glove     = Input(shape=[train_keras[\"question_text_mask_in_glove\"].shape[1],1], name=\"question_text_mask_in_glove\")\n    starting_words    = Input(shape=[train_keras[\"starting_words\"].shape[1]], name=\"starting_words\")\n    ending_words      = Input(shape=[train_keras[\"ending_words\"].shape[1]], name=\"ending_words\")\n    num_data          = Input(shape=[train_keras[\"num_data\"].shape[1]], name=\"num_data\")\n    num_data_normed   = BatchNormalization()(num_data)\n    \n    shared_embedding = Embedding(glove_weights.shape[0], glove_weights.shape[1], weights=[glove_weights], trainable=False, name='emb') \n    emb_sequence_in_glove = shared_embedding (sequence_in_glove)\n    emb_sequence_in_glove = multiply( [emb_sequence_in_glove, mask_in_glove] )\n    emb_sequence_in_glove = SpatialDropout1D(0.1)(emb_sequence_in_glove)\n    \n    #############################\n    shared_embedding2 = Embedding(glove_weights.shape[0], 20,  trainable=True, name='emb2') \n    emb_sequence_in_glove2 = shared_embedding (sequence_in_glove)\n    emb_sequence_in_glove2 = multiply( [emb_sequence_in_glove2, mask_in_glove] )\n    emb_sequence_in_glove2 = SpatialDropout1D(0.2)(emb_sequence_in_glove2)\n    lstm2 = CuDNNLSTM(64, return_sequences=True)(emb_sequence_in_glove2)\n    lstm2 = SpatialDropout1D(0.2)(lstm2)\n    max_lstm2 = GlobalMaxPooling1D( )(lstm2)\n    avg_lstm2 = Lambda(lambda x : K.sum(x, axis=1))(lstm2)\n    avg_lstm2 = BatchNormalization()(avg_lstm2)\n    ################################\n   \n    \n    \n    \n    starting_emb = shared_embedding(starting_words)\n    ending_emb   = shared_embedding(ending_words)\n    avg_emb_start = Lambda(lambda x : K.sum(x, axis=1))(starting_emb)\n    avg_emb_start = BatchNormalization()(avg_emb_start)\n    avg_emb_end = Lambda(lambda x : K.sum(x, axis=1))(ending_emb)\n    avg_emb_end = BatchNormalization()(avg_emb_end)\n    \n    avg_emb = Lambda(lambda x : K.sum(x, axis=1))(emb_sequence_in_glove)\n    avg_emb = BatchNormalization()(avg_emb)\n    \n    lstm = CuDNNLSTM(128, return_sequences=True)(emb_sequence_in_glove)\n    lstm = SpatialDropout1D(0.1)(lstm)\n \n\n    max_lstm = GlobalMaxPooling1D( )(lstm)\n    avg_lstm = Lambda(lambda x : K.sum(x, axis=1))(lstm)\n    avg_lstm = BatchNormalization()(avg_lstm)\n        \n    # main layers\n    main_l = concatenate([ max_lstm, avg_lstm, num_data_normed, avg_emb])#, max_lstm2, avg_lstm2\n\n    #main_l = Activation('relu')(main_l)\n    #main_l = BatchNormalization()(main_l)\n    \n\n    main_l = Dense(196)(main_l)\n    main_l = Activation('relu')(main_l)\n    main_l = Dense(64)(main_l)\n    main_l = Activation('relu')(main_l)\n\n\n    output = Dense(1, activation=\"sigmoid\") (main_l)\n    model = Model([sequence_in_glove, mask_in_glove, starting_words, ending_words, num_data],  output)\n    \n\n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5979b92ad99388eb84f7ce40e6f9f0e24feaa664"},"cell_type":"code","source":"df_sub = pd.read_csv(path+'test.csv',  encoding='utf-8', engine='python')\ndf_sub['target'] = -99\ndf_sub['question_text'].fillna(u'unknownstring', inplace=True)\n\ndf_train_all = pd.read_csv(path+'train.csv',  encoding='utf-8', engine='python')\ndf_train_all['question_text'].fillna(u'unknownstring', inplace=True)\n\ndf_sub['question_text_original']     = df_sub['question_text'].copy()\ndf_sub       = prepare_data(df_sub)\ndf_train_all['question_text_original']     = df_train_all['question_text'].copy()\ndf_train_all       = prepare_data(df_train_all)\n\nword_frequency_dc, vocabulary_dc = create_vocabulary( df_train_all[['question_text']].append( df_sub[['question_text']] ) )\nprint (len(vocabulary_dc))\nglove_weights = load_glove(vocabulary_dc)\nprint(glove_weights.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d84bc22540fb256df70d54170de8dda29fa47466"},"cell_type":"code","source":"df_train_all['seq_question_text_in_glove']   = parallelize_dataframe(df_train_all['question_text'],  preprocess_keras_df)\ntrain_keras        = get_keras_data(df_train_all)\n\ndf_sub['seq_question_text_in_glove']   = parallelize_dataframe(df_sub['question_text'],  preprocess_keras_df)\nsub_keras        = get_keras_data(df_sub)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"63667badd79100f5411a61b32d9d2df9de6619bf"},"cell_type":"code","source":"import gc\n\n\nscale = 1.\nBATCH_SIZE = int(scale*1024)\nlr1 = scale*2e-3\nlr2 = scale*1e-3\n\nbatch_size = BATCH_SIZE\nepochs = 5\nsave_model_name='./model32.h5'\n\nall_preds_sub      = [  ]\n\nprint(\"Fitting RNN model ...\")\n\nfor bag in range(7):\n\n    train_generator = DataGenerator( train_keras,  df_train_all['target'].values, shuffle=True, seed=bag )\n    rnn_model = new_rnn_model()\n    rnn_model.get_layer('emb').trainable=False\n    optimizer = Adam(lr=lr1)\n    rnn_model.compile(loss=\"binary_crossentropy\", optimizer=optimizer)\n    rnn_model.fit_generator( generator=train_generator,   workers=3, verbose=2,    epochs=epochs)\n\n    finetune=True\n    if finetune:\n        rnn_model.get_layer('emb').trainable=True\n        optimizer = Adam(lr=lr2)\n        rnn_model.compile(loss=\"binary_crossentropy\", optimizer=optimizer)\n        rnn_model.fit_generator( generator=train_generator, workers=3, verbose=2,epochs=1)\n \n    preds_sub = rnn_model.predict( sub_keras , batch_size=2048 ).squeeze()\n    all_preds_sub.append( preds_sub )\n \n\n    del rnn_model\n    gc.collect()\n    print('*******************************************************')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"56eecfed34e48001cb53a9d7a83f28e3e5fb1a9b"},"cell_type":"code","source":"subThreshold = 0.33\nmean_preds = np.array(all_preds_sub).transpose()    \nmean_preds = mean_preds.mean(axis=1)\n\nsub_df = pd.DataFrame()\nsub_df['qid'] = df_sub.qid.values\nsub_df['prediction'] = (mean_preds>subThreshold).astype(int)\nsub_df.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"c8dd2d3da2dbc7ada74ff33189ba126813acedba"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"6d6eee4bb76b7e4ed552e5c90d4f3dceb373b2e6"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"3a93a19700c089c4c32527e9d7fca9cf7b396dae"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"76ec1950c0718306dcacfdfcbe28d60dc994caa7"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"6555fae797a489d5480914f68f825ca35720919c"},"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}