{ "cells": [ { "cell_type": "markdown", "id": "9b8b5817", "metadata": {}, "source": [ "# Gender Bias Evaluation for Masked Language modelling: HONEST\n", "\n", "This notebook contains code to evaluate large language models tasked with Masked Language Modelling (MLM) for gender bias. To this end, we use the [HONEST](https://github.com/MilaNLProc/honest) score to measure hurtful sentence completions. We build up on the [code](https://huggingface.co/spaces/sasha/BiasDetection/blob/main/honestbias.py) by Sasha Luccioni from Hugging Face (HF)." ] }, { "cell_type": "markdown", "id": "b576ac89", "metadata": {}, "source": [ "## Setup\n", "\n", "To begin with, let's load install some packages as needed, then load the model to be evlauated." ] }, { "cell_type": "code", "execution_count": 14, "id": "8d97df5d", "metadata": {}, "outputs": [], "source": [ "# !pip install -qq transformers plotly\n", "# !pip install -qq datasets evaluate honest\n", "from transformers import AutoTokenizer, AutoModel , AutoModelForMaskedLM\n", "from transformers import pipeline\n", "import plotly.graph_objects as go\n", "from honest import honest\n", "from pathlib import Path\n", "import pandas as pd\n", "import pickle\n", "import ast" ] }, { "cell_type": "markdown", "id": "f23b7765", "metadata": {}, "source": [ "## Function Definitions\n", "\n", "We now define functions to calculate the HONEST score, which is essentially the proportion of sentence completions of templates given in the HONEST package that are hurtful along given lexical categories, such as `animals`, `crime and immoral behavior`, `derogatory words`." ] }, { "cell_type": "code", "execution_count": 10, "id": "c9528c40", "metadata": {}, "outputs": [], "source": [ "# Initialize HONEST evaluator\n", "# data_set can be \"binary\" as in (Nozza et al., 2021) or \"queer_nonqueer\" (Nozza et al., 2022) or combined as \"all\"\n", "def get_completion(sentence, template, tokenizer):\n", " start = sentence.find(template[:-4])+len(template[:-4])\n", " if tokenizer.eos_token != None and tokenizer.eos_token in sentence:\n", " end = sentence.find(tokenizer.eos_token)\n", " elif tokenizer.sep_token != None and tokenizer.sep_token in sentence:\n", " end = sentence.find(tokenizer.sep_token)\n", " else:\n", " end = len(sentence)-1\n", "\n", " return sentence[start:end]\n", "\n", "# Define nlp_fill pipeline\n", "def calculate_HONEST(modelname, modeltype):\n", " honestdatapath = 'data/'+modelname.replace('/','')+'_HONESTdata.csv'\n", " honestscorepath = 'data/'+modelname.replace('/','')+'_HONESTscore.pkl'\n", " if Path(honestdatapath).is_file():\n", " print(\"loading local data\")\n", " honest_df = pd.read_csv(honestdatapath)\n", " honest_score = pickle.load(open(honestscorepath, 'rb'))\n", " else:\n", " evaluator = honest.HonestEvaluator(\"en\")\n", " masked_templates = evaluator.templates(data_set=\"binary\")\n", " if modeltype == \"CLM\":\n", " generator = pipeline('text-generation', model=modelname)\n", " filled_templates = [[fill['generated_text'][len(masked_sentence.replace(' [M]','')):] for fill in generator(masked_sentence.replace(' [M].',''), max_length=len(masked_sentence.replace(' [M].','').split(\" \"))+10, num_return_sequences=5)] for masked_sentence in masked_templates.keys()]\n", " honest_score, honest_df = evaluator.honest_dataframe(filled_templates, masked_templates)\n", " honest_df.to_csv(honestdatapath)\n", " pickle.dump(honest_score,open(honestscorepath,'wb'))\n", " elif modeltype == \"MLM\":\n", " tokenizer = AutoTokenizer.from_pretrained(modelname, add_special_tokens = False)\n", " model = AutoModelForMaskedLM.from_pretrained(modelname)\n", " nlp_fill = pipeline('fill-mask', model=model, tokenizer=tokenizer, top_k=5)\n", " filled_templates = [[get_completion(fill['sequence'],masked_sentence,tokenizer) for fill in nlp_fill(masked_sentence.replace('[M]',tokenizer.mask_token))] for masked_sentence in masked_templates.keys()]\n", " honest_score, honest_df = evaluator.honest_dataframe(filled_templates, masked_templates)\n", " honest_df.to_csv(honestdatapath)\n", " pickle.dump(honest_score,open(honestscorepath,'wb'))\n", " return(honest_score, honest_df)" ] }, { "cell_type": "markdown", "id": "f2d1155a", "metadata": {}, "source": [ "Each template is tagged with a gender label, `female` or `male`. Thus, the percentage or hurtful sentence completions can be compared along each category for `female`-tagged vs. `male`-tagged templates. Furthermore, a two-sample test of proportions can be used to determine whether the female-vs-male difference of these percentage pairs for a category is statistically significant." ] }, { "cell_type": "code", "execution_count": 5, "id": "49a59388", "metadata": {}, "outputs": [], "source": [ "\"\"\"Analyze completion dataframe\"\"\"\n", "def analyze_honest(honest_df):\n", " hurtlex_cat = ['animals', 'crime and immoral behavior', 'derogatory words', 'female genitalia', 'male genitalia', 'homosexuality', 'potential negative connotations', 'professions and occupations', 'prostitution', 'social and economic disadvantage']\n", " hurtlex_cat_names = ['an', 're', 'cds', 'asf', 'asm', 'om', 'qas', 'pa', 'pr', 'is']\n", " df_identity = honest_df.groupby('category')[hurtlex_cat_names].sum()\n", " df_count = honest_df.groupby('category')[hurtlex_cat_names].count()*5\n", " df_perc = df_identity/df_count*100\n", "\n", " plots = [go.Scatterpolar(r=df_perc.loc[i], theta=hurtlex_cat, fill='toself',\n", " name=i) for i, row in df_perc.iterrows()]\n", "\n", " honest_fig = go.Figure(\n", " data=plots,\n", " layout=go.Layout(\n", " polar={'radialaxis': {'visible': True}}\n", " )\n", " )\n", " return(honest_fig)\n", "\n", "\"\"\"bias detection using two-sample test of proportions: looks for significant differences for each lexicon category\"\"\"\n", "def detect_bias(honest_df, plot=True):\n", "\n", " from statsmodels.stats.proportion import proportions_ztest\n", " \n", " # Initialize selected lexicon categories\n", " hurtlex_cat = ['animals', 'crime and immoral behavior', 'derogatory words', 'female genitalia', 'male genitalia', 'homosexuality', 'potential negative connotations', 'professions and occupations', 'prostitution', 'social and economic disadvantage']\n", " hurtlex_cat_names = ['an', 're', 'cds', 'asf', 'asm', 'om', 'qas', 'pa', 'pr', 'is']\n", " \n", " k = 1\n", " df_identity = honest_df.groupby('category')[hurtlex_cat_names].sum()\n", " df_count = honest_df.groupby('category')[hurtlex_cat_names].count()*k\n", " df_perc = df_identity/df_count*100\n", " \n", " # statistical test: two-sample proportions z-test\n", "\n", " df_idt = df_identity.transpose()\n", " df_idt.columns = ['count_f','count_m']\n", "\n", " df_nt = df_count.transpose()\n", " df_nt.columns = ['nobs_f','nobs_m']\n", " df = df_idt.reset_index().merge(df_nt.reset_index()).set_index('index')\n", "\n", " pval = []\n", " for i in range(df.shape[0]):\n", " idf = df.iloc[i]\n", " stat, pv = proportions_ztest([idf['count_f'], idf['count_m']], [idf['nobs_f'], idf['nobs_m']])\n", " pval.append(pv)\n", "\n", " df['p_value'] = pval\n", " return df" ] }, { "cell_type": "markdown", "id": "47022102", "metadata": {}, "source": [ "## Evaluation\n", "\n", "We now use the above code to detect gender bias in completions performed by a LLM. Here we use two of the most widely used pretrained models, but any suitable model on the HF hub can be evaluated similarly." ] }, { "cell_type": "code", "execution_count": 12, "id": "eeedc957", "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "loading local data\n", "index\n", "an NaN\n", "re 0.138817\n", "cds 0.524184\n", "asf 0.211906\n", "asm 0.025857\n", "om NaN\n", "qas NaN\n", "pa NaN\n", "pr NaN\n", "is NaN\n", "Name: p_value, dtype: float64\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/home/shubhobm/.local/lib/python3.8/site-packages/statsmodels/stats/weightstats.py:790: RuntimeWarning: invalid value encountered in double_scalars\n", " zstat = value / std\n" ] }, { "data": { "text/html": [ " \n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.plotly.v1+json": { "config": { "plotlyServerURL": "https://plot.ly" }, "data": [ { "fill": "toself", "name": "female", "r": [ 0, 0.11560693641618498, 1.7341040462427744, 0.05780346820809249, 3.9884393063583814, 0, 0, 0, 0, 0 ], "theta": [ "animals", "crime and immoral behavior", "derogatory words", "female genitalia", "male genitalia", "homosexuality", "potential negative connotations", "professions and occupations", "prostitution", "social and economic disadvantage" ], "type": "scatterpolar" }, { "fill": "toself", "name": "male", "r": [ 0, 0, 2.0105820105820107, 0.21164021164021166, 2.751322751322751, 0, 0, 0, 0, 0 ], "theta": [ "animals", "crime and immoral behavior", "derogatory words", "female genitalia", "male genitalia", "homosexuality", "potential negative connotations", "professions and occupations", "prostitution", "social and economic disadvantage" ], "type": "scatterpolar" } ], "layout": { "polar": { "radialaxis": { "visible": true } }, "template": { "data": { "bar": [ { "error_x": { "color": "#2a3f5f" }, "error_y": { "color": "#2a3f5f" }, "marker": { "line": { "color": "#E5ECF6", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "bar" } ], "barpolar": [ { "marker": { "line": { "color": "#E5ECF6", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "barpolar" } ], "carpet": [ { "aaxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "baxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "type": "carpet" } ], "choropleth": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "choropleth" } ], "contour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "contour" } ], "contourcarpet": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "contourcarpet" } ], "heatmap": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmap" } ], "heatmapgl": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmapgl" } ], "histogram": [ { "marker": { "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "histogram" } ], "histogram2d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2d" } ], "histogram2dcontour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2dcontour" } ], "mesh3d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "mesh3d" } ], "parcoords": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "parcoords" } ], "pie": [ { "automargin": true, "type": "pie" } ], "scatter": [ { "fillpattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 }, "type": "scatter" } ], "scatter3d": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatter3d" } ], "scattercarpet": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattercarpet" } ], "scattergeo": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergeo" } ], "scattergl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergl" } ], "scattermapbox": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattermapbox" } ], "scatterpolar": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolar" } ], "scatterpolargl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolargl" } ], "scatterternary": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterternary" } ], "surface": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "surface" } ], "table": [ { "cells": { "fill": { "color": "#EBF0F8" }, "line": { "color": "white" } }, "header": { "fill": { "color": "#C8D4E3" }, "line": { "color": "white" } }, "type": "table" } ] }, "layout": { "annotationdefaults": { "arrowcolor": "#2a3f5f", "arrowhead": 0, "arrowwidth": 1 }, "autotypenumbers": "strict", "coloraxis": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "colorscale": { "diverging": [ [ 0, "#8e0152" ], [ 0.1, "#c51b7d" ], [ 0.2, "#de77ae" ], [ 0.3, "#f1b6da" ], [ 0.4, "#fde0ef" ], [ 0.5, "#f7f7f7" ], [ 0.6, "#e6f5d0" ], [ 0.7, "#b8e186" ], [ 0.8, "#7fbc41" ], [ 0.9, "#4d9221" ], [ 1, "#276419" ] ], "sequential": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "sequentialminus": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ] }, "colorway": [ "#636efa", "#EF553B", "#00cc96", "#ab63fa", "#FFA15A", "#19d3f3", "#FF6692", "#B6E880", "#FF97FF", "#FECB52" ], "font": { "color": "#2a3f5f" }, "geo": { "bgcolor": "white", "lakecolor": "white", "landcolor": "#E5ECF6", "showlakes": true, "showland": true, "subunitcolor": "white" }, "hoverlabel": { "align": "left" }, "hovermode": "closest", "mapbox": { "style": "light" }, "paper_bgcolor": "white", "plot_bgcolor": "#E5ECF6", "polar": { "angularaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "radialaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "scene": { "xaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "yaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "zaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" } }, "shapedefaults": { "line": { "color": "#2a3f5f" } }, "ternary": { "aaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "baxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "caxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "title": { "x": 0.05 }, "xaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 }, "yaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 } } } } }, "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# xlm-roberta-base\n", "roberta_evalH = calculate_HONEST(\"xlm-roberta-base\",\"MLM\")\n", "print(detect_bias(roberta_evalH[1])['p_value'])\n", "analyze_honest(roberta_evalH[1])" ] }, { "cell_type": "code", "execution_count": 13, "id": "6a0e92f4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "loading local data\n", "index\n", "an 0.194407\n", "re NaN\n", "cds 0.674674\n", "asf 0.125378\n", "asm 0.951296\n", "om NaN\n", "qas NaN\n", "pa 0.055012\n", "pr 0.000042\n", "is 0.338367\n", "Name: p_value, dtype: float64\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/home/shubhobm/.local/lib/python3.8/site-packages/statsmodels/stats/weightstats.py:790: RuntimeWarning:\n", "\n", "invalid value encountered in double_scalars\n", "\n" ] }, { "data": { "application/vnd.plotly.v1+json": { "config": { "plotlyServerURL": "https://plot.ly" }, "data": [ { "fill": "toself", "name": "female", "r": [ 0.11560693641618498, 0, 1.2716763005780347, 0.05780346820809249, 0.46242774566473993, 0, 0, 0, 1.2716763005780347, 0 ], "theta": [ "animals", "crime and immoral behavior", "derogatory words", "female genitalia", "male genitalia", "homosexuality", "potential negative connotations", "professions and occupations", "prostitution", "social and economic disadvantage" ], "type": "scatterpolar" }, { "fill": "toself", "name": "male", "r": [ 0.31746031746031744, 0, 1.4285714285714286, 0.26455026455026454, 0.4761904761904762, 0, 0, 0.21164021164021166, 0.15873015873015872, 0.052910052910052914 ], "theta": [ "animals", "crime and immoral behavior", "derogatory words", "female genitalia", "male genitalia", "homosexuality", "potential negative connotations", "professions and occupations", "prostitution", "social and economic disadvantage" ], "type": "scatterpolar" } ], "layout": { "polar": { "radialaxis": { "visible": true } }, "template": { "data": { "bar": [ { "error_x": { "color": "#2a3f5f" }, "error_y": { "color": "#2a3f5f" }, "marker": { "line": { "color": "#E5ECF6", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "bar" } ], "barpolar": [ { "marker": { "line": { "color": "#E5ECF6", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "barpolar" } ], "carpet": [ { "aaxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "baxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "type": "carpet" } ], "choropleth": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "choropleth" } ], "contour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "contour" } ], "contourcarpet": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "contourcarpet" } ], "heatmap": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmap" } ], "heatmapgl": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmapgl" } ], "histogram": [ { "marker": { "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "histogram" } ], "histogram2d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2d" } ], "histogram2dcontour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2dcontour" } ], "mesh3d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "mesh3d" } ], "parcoords": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "parcoords" } ], "pie": [ { "automargin": true, "type": "pie" } ], "scatter": [ { "fillpattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 }, "type": "scatter" } ], "scatter3d": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatter3d" } ], "scattercarpet": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattercarpet" } ], "scattergeo": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergeo" } ], "scattergl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergl" } ], "scattermapbox": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattermapbox" } ], "scatterpolar": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolar" } ], "scatterpolargl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolargl" } ], "scatterternary": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterternary" } ], "surface": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "surface" } ], "table": [ { "cells": { "fill": { "color": "#EBF0F8" }, "line": { "color": "white" } }, "header": { "fill": { "color": "#C8D4E3" }, "line": { "color": "white" } }, "type": "table" } ] }, "layout": { "annotationdefaults": { "arrowcolor": "#2a3f5f", "arrowhead": 0, "arrowwidth": 1 }, "autotypenumbers": "strict", "coloraxis": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "colorscale": { "diverging": [ [ 0, "#8e0152" ], [ 0.1, "#c51b7d" ], [ 0.2, "#de77ae" ], [ 0.3, "#f1b6da" ], [ 0.4, "#fde0ef" ], [ 0.5, "#f7f7f7" ], [ 0.6, "#e6f5d0" ], [ 0.7, "#b8e186" ], [ 0.8, "#7fbc41" ], [ 0.9, "#4d9221" ], [ 1, "#276419" ] ], "sequential": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "sequentialminus": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ] }, "colorway": [ "#636efa", "#EF553B", "#00cc96", "#ab63fa", "#FFA15A", "#19d3f3", "#FF6692", "#B6E880", "#FF97FF", "#FECB52" ], "font": { "color": "#2a3f5f" }, "geo": { "bgcolor": "white", "lakecolor": "white", "landcolor": "#E5ECF6", "showlakes": true, "showland": true, "subunitcolor": "white" }, "hoverlabel": { "align": "left" }, "hovermode": "closest", "mapbox": { "style": "light" }, "paper_bgcolor": "white", "plot_bgcolor": "#E5ECF6", "polar": { "angularaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "radialaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "scene": { "xaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "yaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "zaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" } }, "shapedefaults": { "line": { "color": "#2a3f5f" } }, "ternary": { "aaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "baxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "caxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "title": { "x": 0.05 }, "xaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 }, "yaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 } } } } }, "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# bert-base-uncased\n", "bert_evalH = calculate_HONEST(\"bert-base-uncased\",\"MLM\")\n", "print(detect_bias(bert_evalH[1])['p_value'])\n", "analyze_honest(bert_evalH[1])" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.2" } }, "nbformat": 4, "nbformat_minor": 5 }