{"cells":[{"metadata":{"_uuid":"acf1ac4a667cefe906734059468c01c60b97ad63"},"cell_type":"markdown","source":"# Character level only model - LB 0.630\n\nAn attempt at seeing how good a character only model could be: TL; DR not great but it does actually work\n\nMany of Quora's rules about asking questions deal with capitalisation and if the question is in fact, a question at all. Many of the models made public do things like strip `?` ([the default of Keras' tokenizer, which you must specifcally tell it not to do](https://www.kaggle.com/hamishdickson/using-keras-oov-tokens)) and lower the case of all text - that removes this information. My intuition here is a model just focusing on those aspects might do well.\n\nI wanted to use this as part of an ensemble model, but it's very slow - with the architecture below you need about 20 epochs to get anything useful - so that might be a no-goer\n\nThe model and clr used below is all based on [one of shujain's kernels](https://www.kaggle.com/shujian/single-rnn-with-4-folds-clr). I've make a quick tokenizer, but if you're careful with the filters there's no reason you couldn't use keras' one instead. The other change made is the embeddings are trainable, given the embedding space is 15 dimensions and it overly covers about 200 features you can get away with that\n\n**update:** it turns out using batch norm helps you get to a bad answer quicker, in this case about 10 epochs rather than the 20 before"},{"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\"))\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":"import os\nimport time\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom tqdm import tqdm\nimport math\nimport random\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn import metrics\n\nfrom keras.preprocessing.text import Tokenizer\nfrom keras.preprocessing.sequence import pad_sequences\nfrom keras.layers import *\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\n\nimport matplotlib.pyplot as plt\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"06b6fb35b9b74684b1e2da126bc11aadc5eb13d0"},"cell_type":"code","source":"train_df = pd.read_csv(\"../input/train.csv\")\ntest_df = pd.read_csv(\"../input/test.csv\")\n\ntrain_df, val_df = train_test_split(train_df, test_size=0.03)\n\ntrain_df['l'] = train_df['question_text'].apply(lambda t: len(str(t)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fbe55c44be2c213370cc6b58e92f114f60f130b8"},"cell_type":"code","source":"train_df.l.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"75e0d13f48d7e08126d2d24ae46a28b4d687dd7f"},"cell_type":"code","source":"maxlen = int(train_df.quantile(0.99)[1])\nmaxlen","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7110dff685d7a40ff6a453219d333cbbb894acea"},"cell_type":"code","source":"chars = set([])\n\nfor a in train_df.question_text.values:\n    for c in str(a):\n        chars.add(c)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"96c428c32ad3e306d119153e56a32c577a648a33"},"cell_type":"code","source":"count = 2\nchars_to_token = {}\ntoken_to_char = {1: \"OOV\"}\n\nfor c in chars:\n    chars_to_token[c] = count\n    token_to_char[count] = c\n    count += 1\n\n\ndef texts_to_sequences(texts):\n    out = []\n    for text in texts:\n        out1 = []\n        for t in text:\n            if t in chars_to_token:\n                out1.append(chars_to_token[t])\n            else:\n                out1.append(1)\n        out.append(out1)\n        \n    return np.array(out)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b4f0b545a8fdb777c3c601bb095ff20c31e7dbce"},"cell_type":"code","source":"val_df = val_df.sample(frac=1)\n\ntrain_X = texts_to_sequences(train_df.question_text.values)\nval_X = texts_to_sequences(val_df.question_text.values)\ntest_X = texts_to_sequences(test_df.question_text.values)\n\ntrain_X = pad_sequences(train_X, maxlen=maxlen)\nval_X = pad_sequences(val_X, maxlen=maxlen)\ntest_X = pad_sequences(test_X, maxlen=maxlen)\n\n## Get the target values\ntrain_y = train_df['target'].values\nval_y = val_df['target'].values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9117fc067376f36ccf9aff3771bd45545d4c0053"},"cell_type":"code","source":"# https://www.kaggle.com/suicaokhoailang/lstm-attention-baseline-0-652-lb\n\nclass 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":"085bbf6ccbbb144b2806fb9fd44b45b6c870c736"},"cell_type":"code","source":"# https://www.kaggle.com/hireme/fun-api-keras-f1-metric-cyclical-learning-rate/code\nfrom keras.callbacks import Callback\nclass 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())\n    \n\ndef 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        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    precision = precision(y_true, y_pred)\n    recall = recall(y_true, y_pred)\n    return 2*((precision*recall)/(precision+recall+K.epsilon()))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"58353a6e0defb36c96ab6cc7a8165eb3c4a45248"},"cell_type":"code","source":"max_features = len(chars) + 1\n\ndef build_model(embed_dim=15, trainable=True, lstm_dim=20, gru_dim=20, dense_dim=4, learning_rate=0.001):\n\n    inp = Input(shape=(maxlen,))\n    x = Embedding(max_features, embed_dim, input_length=train_X.shape[1], trainable=trainable)(inp)\n    x = SpatialDropout1D(0.1)(x)\n    \n    x = Bidirectional(CuDNNLSTM(lstm_dim, return_sequences=True))(x)\n    y = Bidirectional(CuDNNGRU(gru_dim, return_sequences=True))(x)\n    \n    atten_1 = Attention(maxlen)(x)\n    atten_2 = Attention(maxlen)(y)\n    avg_pool = GlobalAveragePooling1D()(y)\n    max_pool = GlobalMaxPooling1D()(y)\n    \n    conc = concatenate([atten_1, atten_2, avg_pool, max_pool])\n    conc = Dense(dense_dim, activation=\"relu\")(conc)\n    conc = BatchNormalization()(conc)\n    outp = Dense(1, activation=\"sigmoid\")(conc) \n    \n    model = Model(inputs=inp, outputs=outp)\n    \n    adam = Adam(lr=learning_rate, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.00, amsgrad=False)\n    \n    model.compile(loss='binary_crossentropy', optimizer=adam, metrics=[f1])\n    \n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"93f937f3f902c4abbacc7bba040525e42b6fb3d0"},"cell_type":"code","source":"def print_it(history):\n    plt.plot(history.history['loss'])\n    plt.plot(history.history['val_loss'])\n    plt.title('model loss')\n    plt.ylabel('loss')\n    plt.xlabel('epoch')\n    plt.legend(['train', 'validation'], loc='upper left')\n    plt.show()\n\n    plt.plot(history.history['f1'])\n    plt.plot(history.history['val_f1'])\n    plt.title('model f1')\n    plt.ylabel('f1')\n    plt.xlabel('epoch')\n    plt.legend(['train', 'validation'], loc='upper left')\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"707ebdd9055535ebda1ee2e856de9b7265a4ea8b"},"cell_type":"code","source":"from sklearn import metrics\n\ndef find_threshold(y_hat, y):\n    max_threshold = 0.\n    max_value = 0.\n    \n    for thresh in np.arange(0.01, 0.99, 0.01):\n        thresh = np.round(thresh, 2)\n        v = metrics.f1_score(y, (y_hat > thresh).astype(int))\n        if v > max_value:\n            max_value = v\n            max_threshold = thresh\n\n    print(\"best f1 score: \" + str(max_value) + \" at: \" + str(max_threshold))\n    \n    return max_threshold, max_value","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8d61af2fd88a84c4d20b079fcc7f2a7f5561a407"},"cell_type":"code","source":"model = build_model()\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0ad20f8936e807ae025f7977d6de51ff649c0101"},"cell_type":"code","source":"batch_size = 1024\n\nstep_size = float(4 * len(train_X)) / float(batch_size)\n\nclr = CyclicLR(base_lr=0.001, max_lr=0.01,\n               step_size=step_size, mode='exp_range',\n               gamma=0.99994)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"46fd905827511a1414efa19c2a59fc1b7f0eda5b"},"cell_type":"code","source":"history = model.fit(train_X, train_y, batch_size=batch_size, epochs=10, validation_data=(val_X, val_y), callbacks=[clr])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8c1c31ad56532c8550138f3d6b4f0013bce6d5c4"},"cell_type":"code","source":"print_it(history)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3f9df90f7d77f41ea2e9b06eaf45347427d673c2"},"cell_type":"code","source":"y_val_pred = model.predict([val_X])\nf1_threshold, f1_value = find_threshold(y_val_pred, val_y)\nprint(f1_threshold, f1_value)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f74daadb1ad906c05c3f54528fa9033e02fe8815"},"cell_type":"code","source":"print(\"precision\", metrics.precision_score(val_y, (y_val_pred > f1_threshold).astype(int)))\nprint(\"recall\", metrics.recall_score(val_y, (y_val_pred > f1_threshold).astype(int)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6bb081830fb4ce9a7f368f4a46f43600d009ff45"},"cell_type":"code","source":"pd.set_option('display.max_colwidth', -1)\n\nval_df['y_hat'] = (y_val_pred > f1_threshold).astype(int)\nval_df[val_df['y_hat'] != val_df['target']].sample(20)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"936a48ec1adfb2593d60c3d053b0024d09f0639f"},"cell_type":"code","source":"val_df[val_df['y_hat'] == 1].sample(20)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7e89ccc48579107e99e41607f3e66a1827cbcf66"},"cell_type":"code","source":"pred_test_y = model.predict([test_X])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"735f1f92f4a34b44c99de45827446ad000e0efa8"},"cell_type":"code","source":"pred_test_y = (pred_test_y > f1_threshold).astype(int)\ntest_df = pd.read_csv(\"../input/test.csv\", usecols=[\"qid\"])\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":"84ea250436a02d62a388daecdf8e158cfa014ea2"},"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}