{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# Fix seeds\n#from numpy.random import seed\n#seed(1)\n#from tensorflow import set_random_seed\n#set_random_seed(2)\n\nimport numpy as np \nimport pandas as pd\nimport os\nfrom tqdm import tqdm\nimport time\nfrom IPython import display\nimport matplotlib.pyplot as plt\nimport matplotlib\n%matplotlib inline\n\nimport tensorflow as tf\nimport keras\nfrom keras import layers, models, activations\n","execution_count":1,"outputs":[{"output_type":"stream","text":"Using TensorFlow backend.\n","name":"stderr"}]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"# Model\n\ndef build_neural_network(data_size_in, n_classes):\n        \n    inputs = layers.Input(shape=data_size_in)\n    \n    x = layers.Conv1D(kernel_size=5000, filters=8, activation='relu')(inputs)\n    \n    #x = layers.Conv1D(kernel_size=5000, filters=8, activation='relu')(x)\n    \n    x = layers.normalization.BatchNormalization()(x)\n    \n    x = layers.AveragePooling1D(pool_size=3)(x)\n    \n    x = layers.Conv1D(kernel_size=1000, filters=8, activation='relu')(x)\n    \n    #x = layers.Conv1D(kernel_size=1000, filters=8, activation='relu')(x)\n    \n    x = layers.normalization.BatchNormalization()(x)\n    \n    x = layers.AveragePooling1D(pool_size=3)(x)\n    \n    x = layers.Conv1D(kernel_size=500, filters=16, activation='relu')(x)\n    \n    #x = layers.Conv1D(kernel_size=500, filters=16, activation='relu')(x)\n    \n    x = layers.normalization.BatchNormalization()(x)\n    \n    x = layers.AveragePooling1D(pool_size=3)(x)\n    \n    x = layers.Conv1D(kernel_size=500, filters=16, activation='relu')(x)\n    \n    #x = layers.Conv1D(kernel_size=500, filters=16, activation='relu')(x)\n    \n    x = layers.normalization.BatchNormalization()(x)\n    \n    x = layers.AveragePooling1D(pool_size=3)(x)\n    \n    x = layers.Conv1D(kernel_size=150, filters=32, activation='relu')(x)\n    \n    #x = layers.Conv1D(kernel_size=150, filters=32, activation='relu')(x)\n    \n    x = layers.normalization.BatchNormalization()(x)\n    \n    x = layers.AveragePooling1D(pool_size=3)(x)\n    \n    x = layers.Conv1D(kernel_size=150, filters=64, activation='relu')(x)\n    \n    #x = layers.Conv1D(kernel_size=150, filters=64, activation='relu')(x)\n    \n    x = layers.normalization.BatchNormalization()(x)\n    \n    x = layers.MaxPool1D(pool_size=3)(x)\n    \n    x = layers.Flatten()(x)\n        \n    x = layers.Dense(units=500, activation='sigmoid')(x)\n    \n    x = layers.Dropout(rate=0.5)(x)\n    \n    x = layers.Dense(units=25, activation='relu')(x)\n    \n    predictions = layers.Dense(units=n_classes, activation='linear')(x)\n    \n    \n    model = keras.models.Model(inputs=inputs, outputs=predictions)\n    \n    \n    print(model.summary())\n    return model","execution_count":2,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# load pretrained network\n\nnetwork_filepath = \"../input/best-model/best_model.h5\"\nbest_network=keras.models.load_model(network_filepath)","execution_count":4,"outputs":[{"output_type":"stream","text":"WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.\nInstructions for updating:\nColocations handled automatically by placer.\nWARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:3445: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.\nInstructions for updating:\nPlease use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.\nWARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\nInstructions for updating:\nUse tf.cast instead.\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Load submission file\nsubmission = pd.read_csv('../input/LANL-Earthquake-Prediction/sample_submission.csv', index_col='seg_id', dtype={\"time_to_failure\": np.float32})\n\n# Load each test data, create the feature matrix, get numeric prediction\nfor i, seg_id in enumerate(tqdm(submission.index)):\n  #  print(i)\n    seg = pd.read_csv('../input/LANL-Earthquake-Prediction/test/' + seg_id + '.csv')\n    x = seg['acoustic_data'].values\n    temp = np.expand_dims(x,axis=0)\n    submission.time_to_failure[i] = best_network.predict(np.expand_dims(temp, axis=2))\n\nsubmission.head()\n\n# Save\nsubmission.to_csv('submission.csv')","execution_count":5,"outputs":[{"output_type":"stream","text":"100%|██████████| 2624/2624 [01:49<00:00, 23.99it/s]\n","name":"stderr"}]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}