File size: 7,019 Bytes
eda1665
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
{
  "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"
      }
    }
  ]
}