Diff of /MultiscaleNet.ipynb [000000] .. [a8e9b4]

Switch to side-by-side view

--- a
+++ b/MultiscaleNet.ipynb
@@ -0,0 +1,809 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 1,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "%matplotlib inline\n",
+    "import pandas as pd\n",
+    "import numpy as np\n",
+    "from cdtw import pydtw\n",
+    "import seaborn as sns\n",
+    "from tqdm import tqdm\n",
+    "import os\n",
+    "import json\n",
+    "import h5py"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 2,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "name": "stderr",
+     "output_type": "stream",
+     "text": [
+      "Using TensorFlow backend.\n"
+     ]
+    }
+   ],
+   "source": [
+    "from keras import backend as K\n",
+    "from keras.regularizers import l2, activity_l2\n",
+    "\n",
+    "from keras import backend as K\n",
+    "from keras.engine.topology import Layer\n",
+    "from keras.optimizers import RMSprop, SGD, Adam\n",
+    "from keras.layers.core import Dense, Dropout, Activation, Flatten, Lambda, Merge\n",
+    "from keras.layers.recurrent import LSTM, GRU\n",
+    "from keras.models import Sequential, Model, load_model\n",
+    "from keras.layers import Input, Bidirectional, merge\n",
+    "from keras.layers.convolutional import Convolution1D, AtrousConvolution1D\n",
+    "from keras.layers.pooling import MaxPooling1D, AveragePooling1D, GlobalMaxPooling1D"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 3,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "def read_train(fname):\n",
+    "    subjects = {}\n",
+    "    with h5py.File(fname, \"r\") as data_file:\n",
+    "        for subject, subject_data in data_file.items():\n",
+    "            print(subject)\n",
+    "            X = subject_data[\"data\"][:]\n",
+    "            y = subject_data[\"labels\"][:]\n",
+    "            subjects[subject] = (X, y)\n",
+    "    return subjects\n",
+    "\n",
+    "def read_test(fname):\n",
+    "    subjects = {}\n",
+    "    with h5py.File(fname, \"r\") as data_file:\n",
+    "        X = {}\n",
+    "        for subject, subject_data in data_file.items():\n",
+    "            X[subject] = {}\n",
+    "            for chunk_id, chunk in data_file[subject].items():\n",
+    "                X[subject][chunk_id] = chunk[:]\n",
+    "\n",
+    "    return X\n",
+    "\n",
+    "def batch(ts, y, n=1):\n",
+    "    l = len(ts)\n",
+    "    for ndx in range(0, l-n, 1):\n",
+    "        yield (ts[ndx:min(ndx + n, l)], y[ndx:min(ndx + n, l)])\n",
+    "\n",
+    "def label_batch(batch):\n",
+    "    if all([i == 1 for i in batch[1]]):\n",
+    "        return 1\n",
+    "    elif all([i == 0 for i in batch[1]]):\n",
+    "        return 0\n",
+    "    elif all([i == 2 for i in batch[1]]):\n",
+    "        return 2\n",
+    "    return -1\n",
+    "        \n",
+    "def get_data():\n",
+    "    train = read_train(\"train.h5\")\n",
+    "    test = read_test(\"test.h5\")\n",
+    "    \n",
+    "\n",
+    "    subject_datas = {}\n",
+    "    for subject, data in tqdm(train.items()):\n",
+    "        subject_ts = data[0].T\n",
+    "        subject_y = data[1][0]\n",
+    "        batches = [i for i in batch(subject_ts, subject_y, n=1125)]\n",
+    "        batches = [(i[0], label_batch(i)) for i in batches]\n",
+    "        batches = [i for i in batches if i[1] != -1]\n",
+    "        batches = [i for i in batches if len(i[0]) == 1125]\n",
+    "        subject_datas[subject] = batches\n",
+    "    \n",
+    "    X = []\n",
+    "    y = []\n",
+    "    for subj, subj_data in tqdm(subject_datas.items()):\n",
+    "        X.extend([i[0] for i in subj_data])\n",
+    "        y.extend([i[1] for i in subj_data])\n",
+    "    return X, y, test"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 4,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "subject_0\n",
+      "subject_1\n",
+      "subject_2\n",
+      "subject_3\n"
+     ]
+    },
+    {
+     "name": "stderr",
+     "output_type": "stream",
+     "text": [
+      "100%|██████████| 4/4 [01:14<00:00, 19.86s/it]\n",
+      "100%|██████████| 4/4 [00:00<00:00, 145.58it/s]\n"
+     ]
+    }
+   ],
+   "source": [
+    "X, y, test = get_data()"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 5,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [],
+   "source": [
+    "X = np.array(X)\n",
+    "y = np.array(y)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 6,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "def shuffle_in_unison_scary(a, b):\n",
+    "    rng_state = np.random.get_state()\n",
+    "    np.random.shuffle(a)\n",
+    "    np.random.set_state(rng_state)\n",
+    "    np.random.shuffle(b)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 7,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "shuffle_in_unison_scary(X, y)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 8,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [],
+   "source": [
+    "validation_start = len(X) - 30\n",
+    "X_train = X[:validation_start]\n",
+    "y_train = y[:validation_start]\n",
+    "X_val = X[validation_start:]\n",
+    "y_val = y[validation_start:]"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 9,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "from scipy.signal import resample\n",
+    "\n",
+    "\n",
+    "def toarr(label):\n",
+    "    arr = np.zeros(3)\n",
+    "    arr[label] = 1\n",
+    "    return arr\n",
+    "\n",
+    "def data_generator(X, Y, batch_size):\n",
+    "    while True:\n",
+    "        inds = np.random.choice(len(X), batch_size)\n",
+    "        x = X[inds]\n",
+    "        y = Y[inds]\n",
+    "        y = np.vstack([toarr(i) for i in y])\n",
+    "        x_256 = np.array([resample(i, 256) for i in x])\n",
+    "        x_500 = np.array([resample(i, 500) for i in x])\n",
+    "        x = np.array([i for i in x])\n",
+    "        yield ([x_256, x_500, x], y)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 10,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "def multiscale(chunk):\n",
+    "    resampled_256 = resample(chunk, 256)\n",
+    "    resampled_500 = resample(chunk, 500)\n",
+    "    return [resampled_256, resampled_500, chunk]"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "def get_base_model(input_len, fsize):\n",
+    "    '''Base network to be shared (eq. to feature extraction).\n",
+    "    '''\n",
+    "    with K.tf.device('/gpu:1'):\n",
+    "        input_seq = Input(shape=(input_len, 24))\n",
+    "        nb_filters = 150\n",
+    "        convolved = Convolution1D(nb_filters, fsize, border_mode=\"same\", activation=\"tanh\")(input_seq)\n",
+    "        processed = GlobalMaxPooling1D()(convolved)\n",
+    "        compressed = Dense(150, activation=\"tanh\")(processed)\n",
+    "        compressed = Dropout(0.3)(compressed)\n",
+    "        compressed = Dense(150, activation=\"tanh\")(compressed)\n",
+    "        model = Model(input=input_seq, output=compressed)            \n",
+    "        return model"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [],
+   "source": [
+    "with K.tf.device('/gpu:1'):\n",
+    "    \n",
+    "    input256_seq = Input(shape=(256, 24))\n",
+    "    input500_seq = Input(shape=(500, 24))\n",
+    "    input1125_seq = Input(shape=(1125, 24))\n",
+    "    \n",
+    "    base_network256 = get_base_model(256, 4)\n",
+    "    base_network500 = get_base_model(500, 7)\n",
+    "    base_network1125 = get_base_model(1125, 10)\n",
+    "    \n",
+    "    embedding_256 = base_network256(input256_seq)\n",
+    "    embedding_500 = base_network500(input500_seq)\n",
+    "    embedding_1125 = base_network256(input1125_seq)\n",
+    "    \n",
+    "    merged = merge([embedding_256, embedding_500, embedding_1125], mode=\"concat\")\n",
+    "    out = Dense(3, activation='softmax')(merged)\n",
+    "    \n",
+    "    model = Model(input=[input256_seq, input500_seq, input1125_seq], output=out)\n",
+    "    \n",
+    "    #opt = SGD(lr=0.001, momentum=0.9, nesterov=True, clipvalue=0.0001)\n",
+    "    opt = RMSprop(lr=0.005, clipvalue=10**6)\n",
+    "    #opt = Adam(lr=0.001)\n",
+    "    model.compile(loss=\"categorical_crossentropy\", optimizer=opt)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 13,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "with K.tf.device('/gpu:2'):\n",
+    "    model = load_model(\"convnet-multiscale-true-022unk\")"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 21,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "Epoch 1/100000\n",
+      "100000/100000 [==============================] - 135s - loss: 0.1939   \n",
+      "Epoch 2/100000\n",
+      " 99500/100000 [============================>.] - ETA: 0s - loss: 0.1922"
+     ]
+    },
+    {
+     "ename": "KeyboardInterrupt",
+     "evalue": "",
+     "output_type": "error",
+     "traceback": [
+      "\u001b[0;31m\u001b[0m",
+      "\u001b[0;31mKeyboardInterrupt\u001b[0mTraceback (most recent call last)",
+      "\u001b[0;32m<ipython-input-21-09ad3c297605>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      6\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mK\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdevice\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'/gpu:2'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      7\u001b[0m     model.fit_generator(data_generator(X_train, y_train, batch_size=50), samples_per_epoch, nb_epoch, \n\u001b[0;32m----> 8\u001b[0;31m                         callbacks=[earlyStopping], verbose=1)#, nb_val_samples=20000,\n\u001b[0m\u001b[1;32m      9\u001b[0m                         \u001b[0;31m#validation_data=data_generator(X_val, y_val, batch_size=40))\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
+      "\u001b[0;32m/usr/local/lib/python2.7/dist-packages/keras/engine/training.pyc\u001b[0m in \u001b[0;36mfit_generator\u001b[0;34m(self, generator, samples_per_epoch, nb_epoch, verbose, callbacks, validation_data, nb_val_samples, class_weight, max_q_size, nb_worker, pickle_safe)\u001b[0m\n\u001b[1;32m   1451\u001b[0m                     \u001b[0mbatch_logs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0ml\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mo\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1452\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1453\u001b[0;31m                 \u001b[0mcallbacks\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mon_batch_end\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbatch_index\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbatch_logs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1454\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1455\u001b[0m                 \u001b[0;31m# construct epoch logs\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
+      "\u001b[0;32m/usr/local/lib/python2.7/dist-packages/keras/callbacks.pyc\u001b[0m in \u001b[0;36mon_batch_end\u001b[0;34m(self, batch, logs)\u001b[0m\n\u001b[1;32m     59\u001b[0m         \u001b[0mt_before_callbacks\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtime\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     60\u001b[0m         \u001b[0;32mfor\u001b[0m \u001b[0mcallback\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcallbacks\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 61\u001b[0;31m             \u001b[0mcallback\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mon_batch_end\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbatch\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlogs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     62\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_delta_ts_batch_end\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtime\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mt_before_callbacks\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     63\u001b[0m         \u001b[0mdelta_t_median\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmedian\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_delta_ts_batch_end\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
+      "\u001b[0;32m/usr/local/lib/python2.7/dist-packages/keras/callbacks.pyc\u001b[0m in \u001b[0;36mon_batch_end\u001b[0;34m(self, batch, logs)\u001b[0m\n\u001b[1;32m    187\u001b[0m         \u001b[0;31m# will be handled by on_epoch_end\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    188\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mverbose\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mseen\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mparams\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'nb_sample'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 189\u001b[0;31m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprogbar\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mupdate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mseen\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlog_values\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    190\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    191\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0mon_epoch_end\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mepoch\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlogs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m{\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
+      "\u001b[0;32m/usr/local/lib/python2.7/dist-packages/keras/utils/generic_utils.pyc\u001b[0m in \u001b[0;36mupdate\u001b[0;34m(self, current, values, force)\u001b[0m\n\u001b[1;32m    157\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    158\u001b[0m             \u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstdout\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minfo\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 159\u001b[0;31m             \u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstdout\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mflush\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    160\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    161\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0mcurrent\u001b[0m \u001b[0;34m>=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtarget\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
+      "\u001b[0;32m/usr/local/lib/python2.7/dist-packages/ipykernel/iostream.pyc\u001b[0m in \u001b[0;36mflush\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    267\u001b[0m             \u001b[0mevt\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mthreading\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mEvent\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    268\u001b[0m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_io_loop\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0madd_callback\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mevt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mset\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 269\u001b[0;31m             \u001b[0mevt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwait\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    270\u001b[0m         \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    271\u001b[0m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_flush\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
+      "\u001b[0;32m/usr/lib/python2.7/threading.pyc\u001b[0m in \u001b[0;36mwait\u001b[0;34m(self, timeout)\u001b[0m\n\u001b[1;32m    618\u001b[0m         \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    619\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__flag\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 620\u001b[0;31m                 \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__cond\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwait\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    621\u001b[0m             \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__flag\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    622\u001b[0m         \u001b[0;32mfinally\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
+      "\u001b[0;32m/usr/lib/python2.7/threading.pyc\u001b[0m in \u001b[0;36mwait\u001b[0;34m(self, timeout)\u001b[0m\n\u001b[1;32m    337\u001b[0m         \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m    \u001b[0;31m# restore state no matter what (e.g., KeyboardInterrupt)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    338\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0mtimeout\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 339\u001b[0;31m                 \u001b[0mwaiter\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0macquire\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    340\u001b[0m                 \u001b[0;32mif\u001b[0m \u001b[0m__debug__\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    341\u001b[0m                     \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_note\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"%s.wait(): got it\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
+      "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
+     ]
+    }
+   ],
+   "source": [
+    "from keras.callbacks import EarlyStopping\n",
+    "nb_epoch = 100000\n",
+    "earlyStopping = EarlyStopping(monitor='val_loss', patience=10, verbose=0, mode='auto')\n",
+    "samples_per_epoch = 100000\n",
+    "\n",
+    "with K.tf.device('/gpu:2'):\n",
+    "    model.fit_generator(data_generator(X_train, y_train, batch_size=50), samples_per_epoch, nb_epoch, \n",
+    "                        callbacks=[earlyStopping], verbose=1)#, nb_val_samples=20000,\n",
+    "                        #validation_data=data_generator(X_val, y_val, batch_size=40))"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 26,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "Epoch 1/1\n",
+      "30000/30000 [==============================] - 70s - loss: 0.1640    "
+     ]
+    },
+    {
+     "name": "stderr",
+     "output_type": "stream",
+     "text": [
+      " 24%|██▍       | 12/49 [00:00<00:00, 119.99it/s]"
+     ]
+    },
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "\n"
+     ]
+    },
+    {
+     "name": "stderr",
+     "output_type": "stream",
+     "text": [
+      "100%|██████████| 49/49 [00:00<00:00, 135.88it/s]\n",
+      "100%|██████████| 89/89 [00:00<00:00, 215.36it/s]\n",
+      "100%|██████████| 49/49 [00:00<00:00, 248.00it/s]\n",
+      "100%|██████████| 89/89 [00:00<00:00, 219.17it/s]\n"
+     ]
+    },
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "Epoch 1/1\n",
+      "30000/30000 [==============================] - 67s - loss: 0.1592    "
+     ]
+    },
+    {
+     "name": "stderr",
+     "output_type": "stream",
+     "text": [
+      "\r",
+      "  0%|          | 0/49 [00:00<?, ?it/s]"
+     ]
+    },
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "\n"
+     ]
+    },
+    {
+     "name": "stderr",
+     "output_type": "stream",
+     "text": [
+      "100%|██████████| 49/49 [00:00<00:00, 126.00it/s]\n",
+      "100%|██████████| 89/89 [00:00<00:00, 190.92it/s]\n",
+      "100%|██████████| 49/49 [00:00<00:00, 161.84it/s]\n",
+      "100%|██████████| 89/89 [00:00<00:00, 190.56it/s]\n"
+     ]
+    },
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "Epoch 1/1\n",
+      "30000/30000 [==============================] - 60s - loss: 0.1557    "
+     ]
+    },
+    {
+     "name": "stderr",
+     "output_type": "stream",
+     "text": [
+      "\r",
+      "  0%|          | 0/49 [00:00<?, ?it/s]"
+     ]
+    },
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "\n"
+     ]
+    },
+    {
+     "name": "stderr",
+     "output_type": "stream",
+     "text": [
+      "100%|██████████| 49/49 [00:00<00:00, 175.59it/s]\n",
+      "100%|██████████| 89/89 [00:00<00:00, 188.29it/s]\n",
+      "100%|██████████| 49/49 [00:00<00:00, 170.44it/s]\n",
+      "100%|██████████| 89/89 [00:00<00:00, 202.76it/s]\n"
+     ]
+    },
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "Epoch 1/1\n",
+      "30000/30000 [==============================] - 73s - loss: 0.1545    "
+     ]
+    },
+    {
+     "name": "stderr",
+     "output_type": "stream",
+     "text": [
+      " 29%|██▊       | 14/49 [00:00<00:00, 139.19it/s]"
+     ]
+    },
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "\n"
+     ]
+    },
+    {
+     "name": "stderr",
+     "output_type": "stream",
+     "text": [
+      "100%|██████████| 49/49 [00:00<00:00, 190.23it/s]\n",
+      "100%|██████████| 89/89 [00:00<00:00, 235.47it/s]\n",
+      "100%|██████████| 49/49 [00:00<00:00, 221.90it/s]\n",
+      "100%|██████████| 89/89 [00:00<00:00, 171.74it/s]\n"
+     ]
+    },
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "Epoch 1/1\n",
+      "30000/30000 [==============================] - 72s - loss: 0.1556    "
+     ]
+    },
+    {
+     "name": "stderr",
+     "output_type": "stream",
+     "text": [
+      "\r",
+      "  0%|          | 0/49 [00:00<?, ?it/s]"
+     ]
+    },
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "\n"
+     ]
+    },
+    {
+     "name": "stderr",
+     "output_type": "stream",
+     "text": [
+      "100%|██████████| 49/49 [00:00<00:00, 119.83it/s]\n",
+      "100%|██████████| 89/89 [00:00<00:00, 195.06it/s]\n",
+      "100%|██████████| 49/49 [00:00<00:00, 187.22it/s]\n",
+      "100%|██████████| 89/89 [00:00<00:00, 201.47it/s]\n"
+     ]
+    },
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "Epoch 1/1\n",
+      "30000/30000 [==============================] - 68s - loss: 0.1563    "
+     ]
+    },
+    {
+     "name": "stderr",
+     "output_type": "stream",
+     "text": [
+      " 27%|██▋       | 13/49 [00:00<00:00, 119.17it/s]"
+     ]
+    },
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "\n"
+     ]
+    },
+    {
+     "name": "stderr",
+     "output_type": "stream",
+     "text": [
+      "100%|██████████| 49/49 [00:00<00:00, 142.53it/s]\n",
+      "100%|██████████| 89/89 [00:00<00:00, 203.55it/s]\n",
+      "100%|██████████| 49/49 [00:00<00:00, 192.02it/s]\n",
+      "100%|██████████| 89/89 [00:00<00:00, 193.69it/s]\n"
+     ]
+    },
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "Epoch 1/1\n",
+      "30000/30000 [==============================] - 72s - loss: 0.1588    "
+     ]
+    },
+    {
+     "name": "stderr",
+     "output_type": "stream",
+     "text": [
+      " 31%|███       | 15/49 [00:00<00:00, 145.08it/s]"
+     ]
+    },
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "\n"
+     ]
+    },
+    {
+     "name": "stderr",
+     "output_type": "stream",
+     "text": [
+      "100%|██████████| 49/49 [00:00<00:00, 181.02it/s]\n",
+      "100%|██████████| 89/89 [00:00<00:00, 199.11it/s]\n",
+      "100%|██████████| 49/49 [00:00<00:00, 229.50it/s]\n",
+      "100%|██████████| 89/89 [00:00<00:00, 204.20it/s]\n"
+     ]
+    },
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "Epoch 1/1\n",
+      "30000/30000 [==============================] - 67s - loss: 0.1598    "
+     ]
+    },
+    {
+     "name": "stderr",
+     "output_type": "stream",
+     "text": [
+      " 37%|███▋      | 18/49 [00:00<00:00, 174.26it/s]"
+     ]
+    },
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "\n"
+     ]
+    },
+    {
+     "name": "stderr",
+     "output_type": "stream",
+     "text": [
+      "100%|██████████| 49/49 [00:00<00:00, 130.09it/s]\n",
+      "100%|██████████| 89/89 [00:00<00:00, 208.28it/s]\n",
+      "100%|██████████| 49/49 [00:00<00:00, 189.43it/s]\n",
+      "100%|██████████| 89/89 [00:00<00:00, 181.52it/s]\n"
+     ]
+    },
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "Epoch 1/1\n",
+      "30000/30000 [==============================] - 53s - loss: 0.1634    "
+     ]
+    },
+    {
+     "name": "stderr",
+     "output_type": "stream",
+     "text": [
+      " 41%|████      | 20/49 [00:00<00:00, 191.91it/s]"
+     ]
+    },
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "\n"
+     ]
+    },
+    {
+     "name": "stderr",
+     "output_type": "stream",
+     "text": [
+      "100%|██████████| 49/49 [00:00<00:00, 202.34it/s]\n",
+      "100%|██████████| 89/89 [00:00<00:00, 216.69it/s]\n",
+      "100%|██████████| 49/49 [00:00<00:00, 212.14it/s]\n",
+      "100%|██████████| 89/89 [00:00<00:00, 212.55it/s]\n"
+     ]
+    },
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "Epoch 1/1\n",
+      "30000/30000 [==============================] - 70s - loss: 0.1491    "
+     ]
+    },
+    {
+     "name": "stderr",
+     "output_type": "stream",
+     "text": [
+      " 29%|██▊       | 14/49 [00:00<00:00, 135.21it/s]"
+     ]
+    },
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "\n"
+     ]
+    },
+    {
+     "name": "stderr",
+     "output_type": "stream",
+     "text": [
+      "100%|██████████| 49/49 [00:00<00:00, 188.83it/s]\n",
+      "100%|██████████| 89/89 [00:00<00:00, 169.04it/s]\n",
+      "100%|██████████| 49/49 [00:00<00:00, 178.81it/s]\n",
+      "100%|██████████| 89/89 [00:00<00:00, 182.79it/s]\n"
+     ]
+    }
+   ],
+   "source": [
+    "# BLEND NNS\n",
+    "for blend_id in range(25, 35):\n",
+    "    with K.tf.device('/gpu:2'):\n",
+    "        model.fit_generator(data_generator(X_train, y_train, batch_size=100), samples_per_epoch=30000, nb_epoch=1, \n",
+    "                            callbacks=[earlyStopping], verbose=1)\n",
+    "        \n",
+    "        df = []\n",
+    "        for subj in test:\n",
+    "            for chunk in tqdm(test[subj]):\n",
+    "                data = {}\n",
+    "                data[\"subject_id\"] = int(subj.split(\"_\")[-1])\n",
+    "                data[\"chunk_id\"] = int(chunk.split(\"_\")[-1])\n",
+    "                arr = test[subj][chunk].T\n",
+    "                preds = model.predict([np.array([i]) for i in multiscale(arr)])[0]\n",
+    "                data[\"class_0_score\"] = preds[0]\n",
+    "                data[\"class_1_score\"] = preds[1]\n",
+    "                data[\"class_2_score\"] = preds[2]\n",
+    "                for i in range(0, 1125):\n",
+    "                    data[\"tick\"] = i\n",
+    "                    df.append(data.copy())\n",
+    "        df = pd.DataFrame(df)\n",
+    "        df = df[[\"subject_id\", \"chunk_id\", \"tick\", \"class_0_score\",\n",
+    "                 \"class_1_score\",\"class_2_score\"]]\n",
+    "        \n",
+    "        df.to_csv('submit_blended_' + str(blend_id) + '.csv', index=False)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 15,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "model.save(\"convnet-multiscale-deep-021unk\")"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 19,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "name": "stderr",
+     "output_type": "stream",
+     "text": [
+      "100%|██████████| 49/49 [00:00<00:00, 165.60it/s]\n",
+      "100%|██████████| 89/89 [00:00<00:00, 185.89it/s]\n",
+      "100%|██████████| 49/49 [00:00<00:00, 189.62it/s]\n",
+      "100%|██████████| 89/89 [00:00<00:00, 175.60it/s]\n"
+     ]
+    }
+   ],
+   "source": [
+    "df = []\n",
+    "for subj in test:\n",
+    "    for chunk in tqdm(test[subj]):\n",
+    "        data = {}\n",
+    "        data[\"subject_id\"] = int(subj.split(\"_\")[-1])\n",
+    "        data[\"chunk_id\"] = int(chunk.split(\"_\")[-1])\n",
+    "        arr = test[subj][chunk].T\n",
+    "        preds = model.predict([np.array([i]) for i in multiscale(arr)])[0]\n",
+    "        data[\"class_0_score\"] = preds[0]\n",
+    "        data[\"class_1_score\"] = preds[1]\n",
+    "        data[\"class_2_score\"] = preds[2]\n",
+    "        for i in range(0, 1125):\n",
+    "            data[\"tick\"] = i\n",
+    "            df.append(data.copy())\n",
+    "df = pd.DataFrame(df)\n",
+    "df = df[[\"subject_id\", \"chunk_id\", \"tick\", \"class_0_score\",\n",
+    "         \"class_1_score\",\"class_2_score\"]]"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 20,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "df.to_csv('submit_true_multiscale_016_large_batch.csv', index=False)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": []
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "Python 2",
+   "language": "python",
+   "name": "python2"
+  },
+  "language_info": {
+   "codemirror_mode": {
+    "name": "ipython",
+    "version": 2
+   },
+   "file_extension": ".py",
+   "mimetype": "text/x-python",
+   "name": "python",
+   "nbconvert_exporter": "python",
+   "pygments_lexer": "ipython2",
+   "version": "2.7.6"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 1
+}