{"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":"! pip install tables","metadata":{"execution":{"iopub.status.busy":"2022-10-09T21:19:28.331807Z","iopub.execute_input":"2022-10-09T21:19:28.332198Z","iopub.status.idle":"2022-10-09T21:19:42.807716Z","shell.execute_reply.started":"2022-10-09T21:19:28.332102Z","shell.execute_reply":"2022-10-09T21:19:42.806553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-09T21:19:42.810023Z","iopub.execute_input":"2022-10-09T21:19:42.810423Z","iopub.status.idle":"2022-10-09T21:19:42.816512Z","shell.execute_reply.started":"2022-10-09T21:19:42.810371Z","shell.execute_reply":"2022-10-09T21:19:42.815279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x = pd.read_hdf('../input/open-problems-multimodal/train_cite_inputs.h5', stop=1000) \n\ntrain_x.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-10T21:44:51.259239Z","iopub.execute_input":"2022-09-10T21:44:51.259683Z","iopub.status.idle":"2022-09-10T21:44:52.141530Z","shell.execute_reply.started":"2022-09-10T21:44:51.259611Z","shell.execute_reply":"2022-09-10T21:44:52.140264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x = train_x.to_numpy()","metadata":{"execution":{"iopub.status.busy":"2022-09-10T21:44:52.145497Z","iopub.execute_input":"2022-09-10T21:44:52.146577Z","iopub.status.idle":"2022-09-10T21:44:52.152214Z","shell.execute_reply.started":"2022-09-10T21:44:52.146525Z","shell.execute_reply":"2022-09-10T21:44:52.151000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_y = pd.read_hdf('../input/open-problems-multimodal/train_cite_targets.h5', stop=1000) \n\ntrain_y.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-10T21:44:52.153599Z","iopub.execute_input":"2022-09-10T21:44:52.154025Z","iopub.status.idle":"2022-09-10T21:44:52.222073Z","shell.execute_reply.started":"2022-09-10T21:44:52.153991Z","shell.execute_reply":"2022-09-10T21:44:52.220718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_y = train_y.to_numpy()","metadata":{"execution":{"iopub.status.busy":"2022-09-10T21:44:52.226119Z","iopub.execute_input":"2022-09-10T21:44:52.226552Z","iopub.status.idle":"2022-09-10T21:44:52.232070Z","shell.execute_reply.started":"2022-09-10T21:44:52.226516Z","shell.execute_reply":"2022-09-10T21:44:52.230574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\nfrom tensorflow.keras import datasets, layers, models\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-10-09T22:40:51.654471Z","iopub.execute_input":"2022-10-09T22:40:51.654894Z","iopub.status.idle":"2022-10-09T22:40:57.653031Z","shell.execute_reply.started":"2022-10-09T22:40:51.654851Z","shell.execute_reply":"2022-10-09T22:40:57.652045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.Sequential()\nmodel.add(layers.Conv1D(100, 40, activation='linear', input_shape=(22050, 1)))\nmodel.add(layers.MaxPooling1D())\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(140, activation = 'linear'))","metadata":{"execution":{"iopub.status.busy":"2022-09-10T21:48:12.042457Z","iopub.execute_input":"2022-09-10T21:48:12.042979Z","iopub.status.idle":"2022-09-10T21:48:12.669117Z","shell.execute_reply.started":"2022-09-10T21:48:12.042938Z","shell.execute_reply":"2022-09-10T21:48:12.668074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x = train_x.reshape(train_x.shape[0], train_x.shape[1], 1)\ntrain_x.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-10T21:48:15.441702Z","iopub.execute_input":"2022-09-10T21:48:15.442614Z","iopub.status.idle":"2022-09-10T21:48:15.450850Z","shell.execute_reply.started":"2022-09-10T21:48:15.442563Z","shell.execute_reply":"2022-09-10T21:48:15.449554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_y = train_y.reshape(train_y.shape[0], train_y.shape[1], 1)\ntrain_y.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-10T21:48:16.304129Z","iopub.execute_input":"2022-09-10T21:48:16.305028Z","iopub.status.idle":"2022-09-10T21:48:16.313506Z","shell.execute_reply.started":"2022-09-10T21:48:16.304974Z","shell.execute_reply":"2022-09-10T21:48:16.312474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss=tf.keras.losses.MeanAbsoluteError())\n\nhistory = model.fit(train_x, train_y, epochs=10)","metadata":{"execution":{"iopub.status.busy":"2022-09-10T21:48:17.012566Z","iopub.execute_input":"2022-09-10T21:48:17.013558Z","iopub.status.idle":"2022-09-10T21:52:28.152668Z","shell.execute_reply.started":"2022-09-10T21:48:17.013512Z","shell.execute_reply":"2022-09-10T21:52:28.151448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from scipy.stats import pearsonr","metadata":{"execution":{"iopub.status.busy":"2022-09-10T21:52:44.659001Z","iopub.execute_input":"2022-09-10T21:52:44.659428Z","iopub.status.idle":"2022-09-10T21:52:44.898630Z","shell.execute_reply.started":"2022-09-10T21:52:44.659392Z","shell.execute_reply":"2022-09-10T21:52:44.897468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = train_y.flatten()\ny_pred = model.predict(train_x).flatten()","metadata":{"execution":{"iopub.status.busy":"2022-09-10T21:54:27.625689Z","iopub.execute_input":"2022-09-10T21:54:27.626092Z","iopub.status.idle":"2022-09-10T21:54:34.628709Z","shell.execute_reply.started":"2022-09-10T21:54:27.626059Z","shell.execute_reply":"2022-09-10T21:54:34.627735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pearsonr(y_test, y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-09-10T21:54:44.437120Z","iopub.execute_input":"2022-09-10T21:54:44.441020Z","iopub.status.idle":"2022-09-10T21:54:44.490022Z","shell.execute_reply.started":"2022-09-10T21:54:44.440915Z","shell.execute_reply":"2022-09-10T21:54:44.488534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = pd.read_hdf('../input/open-problems-multimodal/train_multi_inputs.h5', stop=1000) \n\nx.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-09T21:20:01.421695Z","iopub.execute_input":"2022-10-09T21:20:01.422153Z","iopub.status.idle":"2022-10-09T21:20:07.025443Z","shell.execute_reply.started":"2022-10-09T21:20:01.422118Z","shell.execute_reply":"2022-10-09T21:20:07.023603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = pd.read_hdf('../input/open-problems-multimodal/train_multi_targets.h5', stop=1000) \n\ny.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-09T21:20:07.028776Z","iopub.execute_input":"2022-10-09T21:20:07.029400Z","iopub.status.idle":"2022-10-09T21:20:07.738664Z","shell.execute_reply.started":"2022-10-09T21:20:07.029358Z","shell.execute_reply":"2022-10-09T21:20:07.737827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import logging  # Setting up the loggings to monitor gensim\nlogging.basicConfig(format=\"%(levelname)s - %(asctime)s: %(message)s\", datefmt= '%H:%M:%S', level=logging.INFO)","metadata":{"execution":{"iopub.status.busy":"2022-10-09T21:20:37.873773Z","iopub.execute_input":"2022-10-09T21:20:37.874697Z","iopub.status.idle":"2022-10-09T21:20:37.879950Z","shell.execute_reply.started":"2022-10-09T21:20:37.874652Z","shell.execute_reply":"2022-10-09T21:20:37.878783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import multiprocessing\n\nfrom gensim.models import Word2Vec","metadata":{"execution":{"iopub.status.busy":"2022-10-09T21:21:24.967180Z","iopub.execute_input":"2022-10-09T21:21:24.967779Z","iopub.status.idle":"2022-10-09T21:21:26.023210Z","shell.execute_reply.started":"2022-10-09T21:21:24.967730Z","shell.execute_reply":"2022-10-09T21:21:26.022127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cores = multiprocessing.cpu_count()","metadata":{"execution":{"iopub.status.busy":"2022-10-09T21:21:39.340594Z","iopub.execute_input":"2022-10-09T21:21:39.340972Z","iopub.status.idle":"2022-10-09T21:21:39.345781Z","shell.execute_reply.started":"2022-10-09T21:21:39.340940Z","shell.execute_reply":"2022-10-09T21:21:39.344724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"w2v_model = Word2Vec(min_count=0,\n                     window=100,\n                     vector_size=100,\n                     sample=6e-5, \n                     alpha=0.03, \n                     min_alpha=0.0007, \n                     negative=20,\n                     workers=cores-1)","metadata":{"execution":{"iopub.status.busy":"2022-10-09T22:23:38.789172Z","iopub.execute_input":"2022-10-09T22:23:38.789573Z","iopub.status.idle":"2022-10-09T22:23:38.795968Z","shell.execute_reply.started":"2022-10-09T22:23:38.789539Z","shell.execute_reply":"2022-10-09T22:23:38.794805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols = x.columns\nbt = x.apply(lambda x: x > 0)\nsentences = bt.apply(lambda x: list(cols[x.values]), axis=1)\nsentences[:5]","metadata":{"execution":{"iopub.status.busy":"2022-10-09T22:23:39.202188Z","iopub.execute_input":"2022-10-09T22:23:39.202600Z","iopub.status.idle":"2022-10-09T22:24:31.986699Z","shell.execute_reply.started":"2022-10-09T22:23:39.202566Z","shell.execute_reply":"2022-10-09T22:24:31.985649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from time import time\nt = time()\n\nw2v_model.build_vocab(sentences.values, progress_per=10000)\n\nprint('Time to build vocab: {} mins'.format(round((time() - t) / 60, 2)))","metadata":{"execution":{"iopub.status.busy":"2022-10-09T22:24:31.989140Z","iopub.execute_input":"2022-10-09T22:24:31.989577Z","iopub.status.idle":"2022-10-09T22:24:38.889891Z","shell.execute_reply.started":"2022-10-09T22:24:31.989536Z","shell.execute_reply":"2022-10-09T22:24:38.888836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t = time()\n\nw2v_model.train(sentences, total_examples=w2v_model.corpus_count, epochs=10, report_delay=1)\n\nprint('Time to train the model: {} mins'.format(round((time() - t) / 60, 2)))","metadata":{"execution":{"iopub.status.busy":"2022-10-09T22:24:38.891132Z","iopub.execute_input":"2022-10-09T22:24:38.891517Z","iopub.status.idle":"2022-10-09T22:30:29.920594Z","shell.execute_reply.started":"2022-10-09T22:24:38.891473Z","shell.execute_reply":"2022-10-09T22:30:29.919759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"word_vectors = w2v_model.wv","metadata":{"execution":{"iopub.status.busy":"2022-10-09T22:30:29.922694Z","iopub.execute_input":"2022-10-09T22:30:29.923012Z","iopub.status.idle":"2022-10-09T22:30:29.927893Z","shell.execute_reply.started":"2022-10-09T22:30:29.922981Z","shell.execute_reply":"2022-10-09T22:30:29.927016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cells_emb = np.zeros((len(x), 100))","metadata":{"execution":{"iopub.status.busy":"2022-10-09T22:34:17.075580Z","iopub.execute_input":"2022-10-09T22:34:17.076020Z","iopub.status.idle":"2022-10-09T22:34:17.081355Z","shell.execute_reply.started":"2022-10-09T22:34:17.075986Z","shell.execute_reply":"2022-10-09T22:34:17.080315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c = 0\nfor e,i in x.iterrows():\n    pos = i!=0\n    embe_vec = [word_vectors[j]  for j in cols[pos] if j in word_vectors]\n    w = i[pos].to_numpy()\n    w = w.reshape(1,-1)\n    cell_embe = np.matmul(i[pos], embe_vec) \n    cells_emb[c] = cell_embe\n    c+=1","metadata":{"execution":{"iopub.status.busy":"2022-10-09T22:35:24.232333Z","iopub.execute_input":"2022-10-09T22:35:24.232746Z","iopub.status.idle":"2022-10-09T22:36:21.656095Z","shell.execute_reply.started":"2022-10-09T22:35:24.232711Z","shell.execute_reply":"2022-10-09T22:36:21.654489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.Sequential()\nmodel.add(tf.keras.Input(shape=(100,)))\nmodel.add(tf.keras.layers.Dense(200))\nmodel.add(tf.keras.layers.Dropout(0.2))\nmodel.add(tf.keras.layers.Dense(400))\nmodel.add(tf.keras.layers.Dropout(0.2))\nmodel.add(tf.keras.layers.Dense(500))\nmodel.add(tf.keras.layers.Dropout(0.2))\nmodel.add(tf.keras.layers.Dense(800))\nmodel.add(tf.keras.layers.Dropout(0.2))\nmodel.add(tf.keras.layers.Dense(1000))\nmodel.add(layers.Dense(23418, activation = 'linear'))","metadata":{"execution":{"iopub.status.busy":"2022-10-09T22:45:12.384103Z","iopub.execute_input":"2022-10-09T22:45:12.384549Z","iopub.status.idle":"2022-10-09T22:45:12.580531Z","shell.execute_reply.started":"2022-10-09T22:45:12.384515Z","shell.execute_reply":"2022-10-09T22:45:12.579584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow_probability as tfp\ndef pearson(y_true, y_pred):\n    return tfp.stats.correlation(y_true, y_pred, sample_axis=None, event_axis=None)","metadata":{"execution":{"iopub.status.busy":"2022-10-09T22:45:13.370961Z","iopub.execute_input":"2022-10-09T22:45:13.371374Z","iopub.status.idle":"2022-10-09T22:45:13.376977Z","shell.execute_reply.started":"2022-10-09T22:45:13.371338Z","shell.execute_reply":"2022-10-09T22:45:13.375884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss=tf.keras.losses.MeanAbsoluteError(), metrics=[pearson])\n\nhistory = model.fit(cells_emb, y, epochs=50)","metadata":{"execution":{"iopub.status.busy":"2022-10-09T22:45:50.743228Z","iopub.execute_input":"2022-10-09T22:45:50.743656Z","iopub.status.idle":"2022-10-09T22:51:11.942080Z","shell.execute_reply.started":"2022-10-09T22:45:50.743621Z","shell.execute_reply":"2022-10-09T22:51:11.941231Z"},"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}