{"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 -q /lib/wheels/tensorflow-2.9.1-cp38-cp38-linux_x86_64.whl\n!pip install -q tensorflow-addons==0.18.0\n!pip install -q tensorflow-probability==0.17.0\n!pip install numba\n!pip install scikit-learn","metadata":{"execution":{"iopub.status.busy":"2023-03-06T17:41:48.210541Z","iopub.execute_input":"2023-03-06T17:41:48.211419Z","iopub.status.idle":"2023-03-06T17:43:03.147986Z","shell.execute_reply.started":"2023-03-06T17:41:48.211383Z","shell.execute_reply":"2023-03-06T17:43:03.147029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport numpy as np \nimport pandas as pd\nimport keras\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2023-03-06T17:44:53.719758Z","iopub.execute_input":"2023-03-06T17:44:53.720478Z","iopub.status.idle":"2023-03-06T17:45:08.835463Z","shell.execute_reply.started":"2023-03-06T17:44:53.720439Z","shell.execute_reply":"2023-03-06T17:45:08.834138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tpu = 'local'\nprint(\"connecting to TPU...\")\ntpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect(tpu=tpu)\nstrategy = tf.distribute.TPUStrategy(tpu)","metadata":{"execution":{"iopub.status.busy":"2023-03-06T17:45:08.837742Z","iopub.execute_input":"2023-03-06T17:45:08.838365Z","iopub.status.idle":"2023-03-06T17:45:18.867026Z","shell.execute_reply.started":"2023-03-06T17:45:08.838330Z","shell.execute_reply":"2023-03-06T17:45:18.865846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv(\"../input/goodreads-books-reviews-290312/goodreads_train.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-03-06T17:45:18.868336Z","iopub.execute_input":"2023-03-06T17:45:18.868618Z","iopub.status.idle":"2023-03-06T17:45:40.903060Z","shell.execute_reply.started":"2023-03-06T17:45:18.868592Z","shell.execute_reply":"2023-03-06T17:45:40.901845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"VOCAB_SIZE = 10000\nwith strategy.scope():\n    encoder = keras.layers.TextVectorization(max_tokens=VOCAB_SIZE, output_sequence_length=1024)\n    encoder.adapt(train_data[\"review_text\"])","metadata":{"execution":{"iopub.status.busy":"2023-03-06T17:45:40.905614Z","iopub.execute_input":"2023-03-06T17:45:40.906073Z","iopub.status.idle":"2023-03-06T17:48:01.967713Z","shell.execute_reply.started":"2023-03-06T17:45:40.906043Z","shell.execute_reply":"2023-03-06T17:48:01.966400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vocab = np.array(encoder.get_vocabulary())","metadata":{"execution":{"iopub.status.busy":"2023-03-06T17:48:01.971513Z","iopub.execute_input":"2023-03-06T17:48:01.971908Z","iopub.status.idle":"2023-03-06T17:48:01.991138Z","shell.execute_reply.started":"2023-03-06T17:48:01.971873Z","shell.execute_reply":"2023-03-06T17:48:01.990032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encoded_example = encoder(train_data.iloc[0][\"review_text\"])[:3].numpy()\nencoded_example","metadata":{"execution":{"iopub.status.busy":"2023-03-06T17:48:01.992331Z","iopub.execute_input":"2023-03-06T17:48:01.992613Z","iopub.status.idle":"2023-03-06T17:48:02.013799Z","shell.execute_reply.started":"2023-03-06T17:48:01.992588Z","shell.execute_reply":"2023-03-06T17:48:02.012866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from numba import njit\n\n@njit()\ndef get_encoded_labels(ratings, precompile=False):\n    labels = np.zeros((len(ratings),6))\n    if precompile == True:\n        iterations = 10\n    else:\n        iterations = len(labels)\n    for n in range(iterations):\n        labels[n][ratings[n]] = 1\n    return labels\n\n_ = get_encoded_labels(train_data[\"rating\"].to_numpy(), precompile=True)\n\nX = encoder(train_data[\"review_text\"])\nY = get_encoded_labels(train_data[\"rating\"].to_numpy())","metadata":{"execution":{"iopub.status.busy":"2023-03-06T17:48:02.014916Z","iopub.execute_input":"2023-03-06T17:48:02.015182Z","iopub.status.idle":"2023-03-06T17:48:46.236219Z","shell.execute_reply.started":"2023-03-06T17:48:02.015157Z","shell.execute_reply":"2023-03-06T17:48:46.234992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for n in range(10):\n    data = train_data.iloc[n][\"rating\"]\n    print(f\"rating: {data}\")\n    print(Y[n])","metadata":{"execution":{"iopub.status.busy":"2023-03-06T17:48:46.237633Z","iopub.execute_input":"2023-03-06T17:48:46.238051Z","iopub.status.idle":"2023-03-06T17:48:46.248358Z","shell.execute_reply.started":"2023-03-06T17:48:46.238020Z","shell.execute_reply":"2023-03-06T17:48:46.247125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_dataset(batch_size, X, Y):\n    dataset = tf.data.Dataset.from_tensor_slices((X,Y))\n    dataset = dataset.repeat()\n    dataset = dataset.shuffle(len(X), reshuffle_each_iteration=True)\n    dataset = dataset.batch(batch_size, drop_remainder=True)\n    return dataset","metadata":{"execution":{"iopub.status.busy":"2023-03-06T17:48:46.249489Z","iopub.execute_input":"2023-03-06T17:48:46.249827Z","iopub.status.idle":"2023-03-06T17:48:46.261644Z","shell.execute_reply.started":"2023-03-06T17:48:46.249782Z","shell.execute_reply":"2023-03-06T17:48:46.260810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 8 * 128 * 8\nTEST_SIZE = 0.1\nX_train, X_test, y_train, y_test = train_test_split(X.numpy(), Y, test_size=TEST_SIZE)\ntrain_dataset = get_dataset(BATCH_SIZE, X_train, y_train)\ntest_dataset = get_dataset(BATCH_SIZE, X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2023-03-06T17:48:46.264076Z","iopub.execute_input":"2023-03-06T17:48:46.264359Z","iopub.status.idle":"2023-03-06T17:48:56.700519Z","shell.execute_reply.started":"2023-03-06T17:48:46.264332Z","shell.execute_reply":"2023-03-06T17:48:56.699459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with strategy.scope():\n    model = tf.keras.Sequential([\n        tf.keras.layers.Embedding(\n            input_dim=1024,\n            output_dim=256,\n            mask_zero=True),\n        tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(256)),\n        tf.keras.layers.Dense(256),\n        tf.keras.layers.BatchNormalization(),\n        tf.keras.layers.Dense(256),\n        tf.keras.layers.BatchNormalization(),\n        tf.keras.layers.Activation('sigmoid'),\n        tf.keras.layers.Dense(6, activation='softmax')\n    ])\n\n\n    model.compile(loss=tf.keras.losses.CategoricalCrossentropy(),\n                  optimizer=tf.keras.optimizers.Adam(),\n                  metrics=['accuracy'])\n    \n    \nsteps_per_epoch = (len(X) * (1 - TEST_SIZE))//BATCH_SIZE\nprint(f\"steps_per_epoch: {steps_per_epoch}\")\nvalidation_steps=(len(X) * TEST_SIZE)//BATCH_SIZE\nprint(f\"validation_steps: {validation_steps}\")\nhistory = model.fit(\n    train_dataset, \n    epochs=50, \n    steps_per_epoch=steps_per_epoch, \n    validation_steps=validation_steps, \n    validation_data=test_dataset\n)","metadata":{"execution":{"iopub.status.busy":"2023-03-06T17:49:49.302433Z","iopub.execute_input":"2023-03-06T17:49:49.303277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = pd.read_csv(\"../input/goodreads-books-reviews-290312/goodreads_test.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-03-06T16:08:05.067962Z","iopub.execute_input":"2023-03-06T16:08:05.068299Z","iopub.status.idle":"2023-03-06T16:08:16.620883Z","shell.execute_reply.started":"2023-03-06T16:08:05.068269Z","shell.execute_reply":"2023-03-06T16:08:16.619387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\nt1 = time.time()\ndata = encoder(test_data[\"review_text\"])\npreds = model.predict(data, batch_size=2048)\nt2 = time.time()\nprint(f\"took: {t2 - t1}s\")","metadata":{"execution":{"iopub.status.busy":"2023-03-06T16:13:21.514146Z","iopub.execute_input":"2023-03-06T16:13:21.514742Z","iopub.status.idle":"2023-03-06T16:14:12.374109Z","shell.execute_reply.started":"2023-03-06T16:13:21.514704Z","shell.execute_reply":"2023-03-06T16:14:12.372762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = np.zeros((len(test_data)))\n\nfor n in range(len(test_data)):\n    results[n] = int(np.argmax(preds[n]))\n\nmy_submission = pd.DataFrame({\n    \"review_id\": test_data[\"review_id\"],\n    \"rating\": results.astype(int)\n})\n\nmy_submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-06T16:18:15.049357Z","iopub.execute_input":"2023-03-06T16:18:15.050111Z","iopub.status.idle":"2023-03-06T16:18:18.171520Z","shell.execute_reply.started":"2023-03-06T16:18:15.050060Z","shell.execute_reply":"2023-03-06T16:18:18.170131Z"},"trusted":true},"execution_count":null,"outputs":[]}]}