{"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":"import torch\nimport numpy as np\nimport pandas as pd\nfrom sklearn import model_selection, metrics\nfrom transformers import AutoModelForSequenceClassification, AutoTokenizer, TrainingArguments, Trainer","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-22T08:49:29.731047Z","iopub.execute_input":"2022-10-22T08:49:29.731577Z","iopub.status.idle":"2022-10-22T08:49:37.803836Z","shell.execute_reply.started":"2022-10-22T08:49:29.731466Z","shell.execute_reply":"2022-10-22T08:49:37.802467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ClassificationDataset:\n    def __init__(self, data, tokenizer):\n        self.data = data\n        self.tokenizer = tokenizer\n\n    def __len__(self):\n        return len(self.data)\n\n    def __getitem__(self, item):\n        text = str(self.data[\"review_text\"].values[item])\n        target = int(self.data[\"rating\"].values[item])\n        inputs = self.tokenizer(\n            text,\n            max_length=512, \n            padding=\"max_length\", \n            truncation=True\n        )\n\n        ids = inputs[\"input_ids\"]\n        mask = inputs[\"attention_mask\"]\n\n        return {\n            \"input_ids\": torch.tensor(ids, dtype=torch.long),\n            \"attention_mask\": torch.tensor(mask, dtype=torch.long),\n            \"labels\": torch.tensor(target, dtype=torch.long),\n        }","metadata":{"execution":{"iopub.status.busy":"2022-10-22T08:52:04.466102Z","iopub.execute_input":"2022-10-22T08:52:04.466470Z","iopub.status.idle":"2022-10-22T08:52:04.474427Z","shell.execute_reply.started":"2022-10-22T08:52:04.466437Z","shell.execute_reply":"2022-10-22T08:52:04.473258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"../input/goodreads-books-reviews-290312/goodreads_test.csv\")\ntokenizer = AutoTokenizer.from_pretrained(\"abhishek/autotrain-goodreads-review-rating-1828462866\", use_fast=True)\nmodel = AutoModelForSequenceClassification.from_pretrained(\"abhishek/autotrain-goodreads-review-rating-1828462866\")","metadata":{"execution":{"iopub.status.busy":"2022-10-22T08:54:08.841092Z","iopub.execute_input":"2022-10-22T08:54:08.841446Z","iopub.status.idle":"2022-10-22T08:54:20.252720Z","shell.execute_reply.started":"2022-10-22T08:54:08.841411Z","shell.execute_reply":"2022-10-22T08:54:20.251580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.loc[:, \"rating\"] = 0\ndataset = ClassificationDataset(df, tokenizer)\ntrainer = Trainer(model)\npreds = trainer.predict(dataset).predictions","metadata":{"execution":{"iopub.status.busy":"2022-10-22T08:54:20.259941Z","iopub.execute_input":"2022-10-22T08:54:20.262443Z","iopub.status.idle":"2022-10-22T08:54:20.271250Z","shell.execute_reply.started":"2022-10-22T08:54:20.262402Z","shell.execute_reply":"2022-10-22T08:54:20.269961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = pd.read_csv(\"../input/goodreads-books-reviews-290312/goodreads_sample_submission.csv\")\npreds = np.argmax(preds, axis=1)\nsample_submission.rating = preds\nsample_submission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-10-22T08:54:20.926389Z","iopub.execute_input":"2022-10-22T08:54:20.926799Z","iopub.status.idle":"2022-10-22T08:54:20.934476Z","shell.execute_reply.started":"2022-10-22T08:54:20.926752Z","shell.execute_reply":"2022-10-22T08:54:20.933513Z"},"trusted":true},"execution_count":null,"outputs":[]}]}