{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport tensorflow as tf\nimport gc","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-17T12:44:48.909853Z","iopub.execute_input":"2023-04-17T12:44:48.910382Z","iopub.status.idle":"2023-04-17T12:45:28.868536Z","shell.execute_reply.started":"2023-04-17T12:44:48.910353Z","shell.execute_reply":"2023-04-17T12:45:28.867542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pyarrow","metadata":{"execution":{"iopub.status.busy":"2023-04-17T12:45:28.870203Z","iopub.execute_input":"2023-04-17T12:45:28.870658Z","iopub.status.idle":"2023-04-17T12:45:36.839840Z","shell.execute_reply.started":"2023-04-17T12:45:28.870631Z","shell.execute_reply":"2023-04-17T12:45:36.838690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y=pd.read_parquet(\"/kaggle/input/icecube-neutrinos-in-deep-ice/train_meta.parquet\")","metadata":{"execution":{"iopub.status.busy":"2023-04-17T12:45:36.844815Z","iopub.execute_input":"2023-04-17T12:45:36.845080Z","iopub.status.idle":"2023-04-17T12:46:11.743794Z","shell.execute_reply.started":"2023-04-17T12:45:36.845051Z","shell.execute_reply":"2023-04-17T12:46:11.742774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y=Y[Y['batch_id']>=520]\nY=Y[Y['batch_id']<=525]\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-04-17T12:46:11.744980Z","iopub.execute_input":"2023-04-17T12:46:11.745508Z","iopub.status.idle":"2023-04-17T12:46:13.548907Z","shell.execute_reply.started":"2023-04-17T12:46:11.745475Z","shell.execute_reply":"2023-04-17T12:46:13.548040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def y_to_xyz(batch_y):\n    angle_code=batch_y\n    angle_code=np.transpose(angle_code)\n    angle_code=np.transpose(np.array([np.cos(angle_code[0])*np.sin(angle_code[1]),\n                                      np.sin(angle_code[0])*np.sin(angle_code[1]),\n                                      np.cos(angle_code[1])]))\n    \n    return angle_code\n\n","metadata":{"execution":{"iopub.status.busy":"2023-04-17T12:46:13.550103Z","iopub.execute_input":"2023-04-17T12:46:13.550403Z","iopub.status.idle":"2023-04-17T12:46:13.557481Z","shell.execute_reply.started":"2023-04-17T12:46:13.550376Z","shell.execute_reply":"2023-04-17T12:46:13.556640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def loadY(bid):\n    y=Y[Y['batch_id']==bid]\n    y = y[['azimuth', 'zenith']].to_numpy()\n    y=y_to_xyz(y)\n    return y\ndef loadX(bid):\n    resGraph=pd.read_csv(\"/kaggle/input/graphnetresults1/results_\"+str(bid)+\".csv\")\n    resLSTM=np.load(\"/kaggle/input/lstmresults1/lstm_batch_\"+str(bid)+\".npz\")['arr_0']\n    #print(resGraph)\n    resGraph= resGraph[['direction_x','direction_y','direction_z','direction_kappa']].to_numpy()\n    y1=Y[Y['batch_id']==bid]\n    pulses=y1[['last_pulse_index']].to_numpy()-y1[['first_pulse_index']].to_numpy()\n    \n    e1=np.expand_dims(np.sum(np.log(resLSTM[:,0:575]+1e-6)*resLSTM[:,0:575],axis=1),axis=-1)\n    e2=np.expand_dims(np.sum(np.log(resLSTM[:,576:1151]+1e-6)*resLSTM[:,576:1151],axis=1),axis=-1)\n    e3=np.expand_dims(np.sum(np.log(resLSTM[:,1152:1727]+1e-6)*resLSTM[:,1152:1727],axis=1),axis=-1)\n    \n    x=np.concatenate((resLSTM,resGraph,pulses,e1,e2,e3),axis=1)\n    gc.collect()\n    return x","metadata":{"execution":{"iopub.status.busy":"2023-04-17T12:49:47.014522Z","iopub.execute_input":"2023-04-17T12:49:47.015284Z","iopub.status.idle":"2023-04-17T12:49:47.026947Z","shell.execute_reply.started":"2023-04-17T12:49:47.015248Z","shell.execute_reply":"2023-04-17T12:49:47.026100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#y=loadY(520)\n#y","metadata":{"execution":{"iopub.status.busy":"2023-04-17T12:46:13.579142Z","iopub.execute_input":"2023-04-17T12:46:13.579769Z","iopub.status.idle":"2023-04-17T12:46:13.588832Z","shell.execute_reply.started":"2023-04-17T12:46:13.579726Z","shell.execute_reply":"2023-04-17T12:46:13.587949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#x=loadX(520)\n#x","metadata":{"execution":{"iopub.status.busy":"2023-04-17T12:46:13.589940Z","iopub.execute_input":"2023-04-17T12:46:13.590297Z","iopub.status.idle":"2023-04-17T12:46:13.600279Z","shell.execute_reply.started":"2023-04-17T12:46:13.590271Z","shell.execute_reply":"2023-04-17T12:46:13.599398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#x.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-17T12:46:13.603424Z","iopub.execute_input":"2023-04-17T12:46:13.603806Z","iopub.status.idle":"2023-04-17T12:46:13.610921Z","shell.execute_reply.started":"2023-04-17T12:46:13.603778Z","shell.execute_reply":"2023-04-17T12:46:13.610071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def MeanAngErr(y_true, y_pred):\n    fcos=tf.math.scalar_mul(-0.99999,tf.keras.losses.cosine_similarity(y_true, y_pred))\n    return tf.reduce_mean(tf.math.acos(fcos), axis=-1)  # Note the `axis=-1`\ndef create_model():\n    \n    inputs = tf.keras.Input(shape=(1733,))\n    \n    intermediate = tf.keras.layers.Reshape((1733,1), input_shape=(1733,))(inputs)\n\n    inputs1 = tf.keras.layers.Cropping1D(cropping=(2,0))(intermediate)\n    inputs1 = tf.keras.layers.Reshape((1731,), input_shape=(1731,1))(inputs1)\n    \n    tensor_1 = tf.keras.layers.Dense(20, activation='tanh')(inputs)\n    tensor_1 = tf.keras.layers.Dense(1731, activation='tanh')(tensor_1)\n    #tensor_2 = tf.keras.layers.BatchNormalization()(tensor_2)\n    \n    #tensor_2 = tf.keras.layers.Dropout(0.5)(tensor_2)\n    \n    product = tf.keras.layers.Multiply()([tensor_1, inputs1])\n    \n    outputs = tf.keras.layers.Dense(3, activation = 'linear')(product)\n        \n        # Finalize Model\n    model = tf.keras.models.Model(inputs = inputs, outputs = outputs)\n\n        # Compile model\n    model.compile(loss = MeanAngErr,\n                      optimizer= tf.keras.optimizers.Adam())\n        \n        # Show Model Summary\n    model.summary()\n\n    return model","metadata":{"execution":{"iopub.status.busy":"2023-04-17T12:46:13.611960Z","iopub.execute_input":"2023-04-17T12:46:13.612250Z","iopub.status.idle":"2023-04-17T12:46:13.671665Z","shell.execute_reply.started":"2023-04-17T12:46:13.612224Z","shell.execute_reply":"2023-04-17T12:46:13.670783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.backend.clear_session()","metadata":{"execution":{"iopub.status.busy":"2023-04-17T13:01:18.029963Z","iopub.execute_input":"2023-04-17T13:01:18.030377Z","iopub.status.idle":"2023-04-17T13:01:18.046238Z","shell.execute_reply.started":"2023-04-17T13:01:18.030345Z","shell.execute_reply":"2023-04-17T13:01:18.045146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=create_model()","metadata":{"execution":{"iopub.status.busy":"2023-04-17T13:01:19.806686Z","iopub.execute_input":"2023-04-17T13:01:19.807077Z","iopub.status.idle":"2023-04-17T13:01:19.910784Z","shell.execute_reply.started":"2023-04-17T13:01:19.807048Z","shell.execute_reply":"2023-04-17T13:01:19.909910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#yval=loadY(525)\n#xval=loadX(525)\n\n#y=np.concatenate((loadY(520),loadY(521),loadY(522),loadY(523),loadY(524)),axis=0)\n#x=np.concatenate((loadX(520),loadX(521),loadX(522),loadX(523),loadX(524)),axis=0)\ny=np.concatenate((loadY(520),loadY(521),loadY(522),loadY(523),loadY(524),loadY(525)),axis=0)\nx=np.concatenate((loadX(520),loadX(521),loadX(522),loadX(523),loadX(524),loadX(525)),axis=0)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T12:50:03.503591Z","iopub.execute_input":"2023-04-17T12:50:03.503998Z","iopub.status.idle":"2023-04-17T12:53:00.833038Z","shell.execute_reply.started":"2023-04-17T12:50:03.503967Z","shell.execute_reply":"2023-04-17T12:53:00.832072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(x,y,epochs=10,batch_size=200)\nmodel.fit(x,y,epochs=5,batch_size=2000)\nmodel.fit(x,y,epochs=5,batch_size=20000)\nmodel.fit(x,y,epochs=5,batch_size=200000)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T13:01:25.472279Z","iopub.execute_input":"2023-04-17T13:01:25.472709Z","iopub.status.idle":"2023-04-17T13:07:07.956743Z","shell.execute_reply.started":"2023-04-17T13:01:25.472675Z","shell.execute_reply":"2023-04-17T13:07:07.955559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save_weights(\"model3.h5\")","metadata":{"execution":{"iopub.status.busy":"2023-04-17T13:07:18.355255Z","iopub.execute_input":"2023-04-17T13:07:18.359822Z","iopub.status.idle":"2023-04-17T13:07:18.378317Z","shell.execute_reply.started":"2023-04-17T13:07:18.359767Z","shell.execute_reply":"2023-04-17T13:07:18.376982Z"},"trusted":true},"execution_count":null,"outputs":[]}]}