{"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":"markdown","source":"# Training A Multilayer Perceptron Using GPU P100","metadata":{}},{"cell_type":"code","source":"%%time\n\nimport numpy as np\nimport pandas as pd\n\n\nfrom tensorflow.keras.models import load_model\nfrom tensorflow.test import gpu_device_name","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-06-20T10:39:44.001528Z","iopub.execute_input":"2023-06-20T10:39:44.001927Z","iopub.status.idle":"2023-06-20T10:39:44.0099Z","shell.execute_reply.started":"2023-06-20T10:39:44.001899Z","shell.execute_reply":"2023-06-20T10:39:44.008725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub = pd.read_csv('/kaggle/input/cafa-5-protein-function-prediction/sample_submission.tsv', sep='\\t')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\n# Check if GPU is available\nif gpu_device_name():\n    print('GPU device found: {}'.format(gpu_device_name()))\nelse:\n    print(\"No GPU found. Training on CPU.\")","metadata":{"execution":{"iopub.status.busy":"2023-06-20T10:39:00.286396Z","iopub.execute_input":"2023-06-20T10:39:00.286839Z","iopub.status.idle":"2023-06-20T10:39:00.324631Z","shell.execute_reply.started":"2023-06-20T10:39:00.28681Z","shell.execute_reply":"2023-06-20T10:39:00.323725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %%time\n\n# train_data = np.load('/kaggle/input/t5embeds/train_embeds.npy')\n\n# with h5py.File('/kaggle/input/train-labels-cafa5/train_labels.h5', 'r') as hf:\n#     train_labels = hf['labels'][:]","metadata":{"execution":{"iopub.status.busy":"2023-06-20T09:38:23.087502Z","iopub.execute_input":"2023-06-20T09:38:23.087869Z","iopub.status.idle":"2023-06-20T09:38:23.093744Z","shell.execute_reply.started":"2023-06-20T09:38:23.087841Z","shell.execute_reply":"2023-06-20T09:38:23.090892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\ntest_data = np.load('/kaggle/input/t5embeds/test_embeds.npy')","metadata":{"execution":{"iopub.status.busy":"2023-06-20T09:38:26.073035Z","iopub.execute_input":"2023-06-20T09:38:26.073662Z","iopub.status.idle":"2023-06-20T09:38:36.651249Z","shell.execute_reply.started":"2023-06-20T09:38:26.073628Z","shell.execute_reply":"2023-06-20T09:38:36.650182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %%time\n\n# INPUT_SHAPE = [train_data.shape[1]]\n# BATCH_SIZE = int(train_data.shape[0] / 250)\n\n# model = tf.keras.Sequential([\n#     tf.keras.layers.BatchNormalization(input_shape=INPUT_SHAPE),    \n#     tf.keras.layers.Dense(units=512, activation='relu'),\n#     tf.keras.layers.Dense(units=512, activation='relu'),\n#     tf.keras.layers.Dense(units=512, activation='relu'),\n#     tf.keras.layers.Dense(units=train_labels.shape[1],activation='sigmoid')\n# ])\n\n# model.compile(\n#     optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),\n#     loss='binary_crossentropy',\n#     metrics=['binary_accuracy', tf.keras.metrics.AUC()]\n# )","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %%time\n# model.fit(\n#         train_data, train_labels,\n#         batch_size=BATCH_SIZE,\n#         epochs=10\n#     )","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.save('MultiLayerPerceptron.h5')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nmodel = load_model('/kaggle/input/train-labels-cafa5/MultiaLayerPerceptron.h5')","metadata":{"execution":{"iopub.status.busy":"2023-06-20T09:40:50.508451Z","iopub.execute_input":"2023-06-20T09:40:50.509153Z","iopub.status.idle":"2023-06-20T09:40:51.523817Z","shell.execute_reply.started":"2023-06-20T09:40:50.509122Z","shell.execute_reply":"2023-06-20T09:40:51.52165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\npredictions =  model.predict(test_data)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T09:40:54.856018Z","iopub.execute_input":"2023-06-20T09:40:54.856378Z","iopub.status.idle":"2023-06-20T09:41:16.865796Z","shell.execute_reply.started":"2023-06-20T09:40:54.856349Z","shell.execute_reply":"2023-06-20T09:41:16.864761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = np.load('/kaggle/input/train-labels-cafa5/top_1500_labels.npy',allow_pickle=True)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T09:41:42.95471Z","iopub.execute_input":"2023-06-20T09:41:42.955271Z","iopub.status.idle":"2023-06-20T09:41:42.964358Z","shell.execute_reply.started":"2023-06-20T09:41:42.955238Z","shell.execute_reply":"2023-06-20T09:41:42.963377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_protein_ids = np.load('/kaggle/input/t5embeds/test_ids.npy')\nl = []\nfor k in list(test_protein_ids):\n    l += [ k] * predictions.shape[1]","metadata":{"execution":{"iopub.status.busy":"2023-06-20T09:42:37.029361Z","iopub.execute_input":"2023-06-20T09:42:37.029731Z","iopub.status.idle":"2023-06-20T09:42:39.391413Z","shell.execute_reply.started":"2023-06-20T09:42:37.029696Z","shell.execute_reply":"2023-06-20T09:42:39.390406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission = pd.DataFrame(\n    {\n        'Protein ID': l,\n        'GO Term ID': np.tile(labels, predictions.shape[0]),\n        'Prediction': np.round(predictions.ravel(),3)\n    }\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T09:45:17.922646Z","iopub.execute_input":"2023-06-20T09:45:17.923011Z","iopub.status.idle":"2023-06-20T09:45:42.716788Z","shell.execute_reply.started":"2023-06-20T09:45:17.922982Z","shell.execute_reply":"2023-06-20T09:45:42.712328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-20T09:46:53.214149Z","iopub.execute_input":"2023-06-20T09:46:53.214521Z","iopub.status.idle":"2023-06-20T09:46:53.260958Z","shell.execute_reply.started":"2023-06-20T09:46:53.214476Z","shell.execute_reply":"2023-06-20T09:46:53.259931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission.to_csv('submission.tsv', sep='\\t', header=None, index= None)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T09:47:14.771776Z","iopub.execute_input":"2023-06-20T09:47:14.772129Z","iopub.status.idle":"2023-06-20T10:01:15.818159Z","shell.execute_reply.started":"2023-06-20T09:47:14.772101Z","shell.execute_reply":"2023-06-20T10:01:15.817163Z"},"trusted":true},"execution_count":null,"outputs":[]}]}