{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "source": [ ], "metadata": { "id": "jIeixcQwAAJv" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "ozrMJDDP_9Xg" }, "outputs": [], "source": [ "!pip install minicons\n", "!pip install pandas\n", "!pip install numpy" ] }, { "cell_type": "code", "source": [ "from minicons import scorer\n", "import pandas as pd\n", "import numpy as np\n", "import random\n", "from transformers import GPTNeoXForCausalLM, AutoTokenizer" ], "metadata": { "id": "oC-dBRq0AI9w" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "The evaluation works like this: for each item (row) 0f the datasetm, the conditional probability of both Maxim-following and Maxim-violating answers are obtained from the model. The conditional probabilities are sentence probabilities (averaged sum of token probabilities) of the answers, conditioned on the tokens of the question.\n", "\n", "Whenever the model assigns a higher probability to the correct answer (i.e., Maxim-following alternative), a \"True\" entry is then added to the Results dataframe.\n", "\n", "For computing accuracy, the number of True values is then counted. There are 450 items for each Maxim, and the accuracy score shows the amount of items for which the model correctly assigned the higher probability to the Maxim-following answer." ], "metadata": { "id": "fRHfXy2wALS3" } }, { "cell_type": "code", "source": [ "def compute_probability(model, df, model_name):\n", "\n", " result_df=pd.DataFrame()\n", "\n", " questions=df[\"question\"].to_list()\n", " following=df[\"follower\"].to_list()\n", " violating=df[\"violator\"].to_list()\n", "\n", " score_follower=[]\n", " score_violator=[]\n", "\n", " for i in range(0, len(questions), 150):\n", " q_chunk=questions[i:i+150]\n", " f_chunk=following[i:i+150]\n", " v_chunk=violating[i:i+150]\n", "\n", " f_scores=model.conditional_score(q_chunk, f_chunk, reduction=lambda x: x.mean(0).item())\n", " v_scores=model.conditional_score(q_chunk, v_chunk, reduction=lambda x: x.mean(0).item())\n", "\n", " score_follower.extend(f_scores)\n", " score_violator.extend(v_scores)\n", "\n", " accuracy=[f > v for f, v in zip(score_follower, score_violator)]\n", "\n", " result_df[f\"{model_name} | P. Follower\"]=score_follower\n", " result_df[f\"{model_name} | P. violator\"]=score_violator\n", " result_df[f\"{model_name} | Accuracy (F. > V.)\"]=accuracy\n", "\n", " return result_df\n" ], "metadata": { "id": "rEp_B6XDBNAH" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "dataset=pd.read_csv(\"eval_dataset.csv\")" ], "metadata": { "id": "cnJoyH_ZBQlW" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "For demonstration purposes, I will now evaluate the models that I trained from scratch for our paper. All models share identical architecture and have been trained on the same amount of data, but vary in data quality." ], "metadata": { "id": "4gfRA8ZfBkwK" } }, { "cell_type": "code", "source": [ "#NOW MY MODELS <3\n", "\n", "spontaneous=scorer.IncrementalLMScorer(\"rahaaskari/pythia-160m-spontaneous-speech\", device=\"cuda\")\n", "staged=scorer.IncrementalLMScorer(\"rahaaskari/pythia-160m-staged-dialogues\", device=\"cuda\")\n", "nodialog=scorer.IncrementalLMScorer(\"rahaaskari/pythia-160m-no-dialog\", device=\"cuda\")\n", "mixed=scorer.IncrementalLMScorer(\"rahaaskari/pythia-160m-mixed\", device=\"cuda\")" ], "metadata": { "id": "v8t3QWtkBSVF" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "prob_spontaneous = compute_probability(spontaneous, dataset, \"spontaneous\")" ], "metadata": { "id": "c0X-TK-lCTcE" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "prob_staged = compute_probability(staged, dataset, \"staged\")" ], "metadata": { "id": "bWpB4O0VCXWt" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "prob_nodialog = compute_probability(nodialog, dataset, \"nodialog\")" ], "metadata": { "id": "BVRcLtUnCdlD" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "prob_mixed = compute_probability(mixed, dataset, \"mixed\")" ], "metadata": { "id": "MgQ3CE8OCgM4" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "mymodel_results=pd.concat([dataset, prob_spontaneous, prob_staged, prob_nodialog, prob_mixed], axis=1)" ], "metadata": { "id": "mwrCAkOTClku" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "my_models=[\"spontaneous\", \"staged\", \"nodialog\", \"mixed\"]\n", "\n", "mymodels_overall=[]\n", "for model in my_models:\n", " df=mymodel_results.groupby(\"maxim\")[f\"{model} | Accuracy (F. > V.)\"].mean()\n", " mymodels_overall.append(df)\n", "\n", "mymodels_overall=pd.concat(mymodels_overall, axis=1)\n", "mymodels_overall=mymodels_overall.loc[ordered_maxims].T\n", "mymodels_overall" ], "metadata": { "id": "id05ltehCxk0" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "mymodel_results.to_csv(\"my models results.csv\", index=False)" ], "metadata": { "id": "On0vzrGgC7uG" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "Cheers." ], "metadata": { "id": "uSN_aePSC9zm" } } ] }