Ethan L. Mines commited on
Commit ·
d173119
1
Parent(s): 0f3c94a
Add tutorial notebook
Browse files- Example.ipynb +113 -0
Example.ipynb
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 13,
|
| 6 |
+
"id": "5338af6d",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [],
|
| 9 |
+
"source": [
|
| 10 |
+
"from transformers import AutoModel, AutoTokenizer\n",
|
| 11 |
+
"import numpy as np"
|
| 12 |
+
]
|
| 13 |
+
},
|
| 14 |
+
{
|
| 15 |
+
"cell_type": "code",
|
| 16 |
+
"execution_count": 2,
|
| 17 |
+
"id": "3e5e27fb",
|
| 18 |
+
"metadata": {},
|
| 19 |
+
"outputs": [],
|
| 20 |
+
"source": [
|
| 21 |
+
"model = AutoModel.from_pretrained(\"UF-NLPC-Lab/bart-stance-mixed\", trust_remote_code=True)"
|
| 22 |
+
]
|
| 23 |
+
},
|
| 24 |
+
{
|
| 25 |
+
"cell_type": "code",
|
| 26 |
+
"execution_count": 3,
|
| 27 |
+
"id": "eccdbd4e",
|
| 28 |
+
"metadata": {},
|
| 29 |
+
"outputs": [],
|
| 30 |
+
"source": [
|
| 31 |
+
"tokenizer = AutoTokenizer.from_pretrained(\"UF-NLPC-Lab/bart-stance-mixed\")"
|
| 32 |
+
]
|
| 33 |
+
},
|
| 34 |
+
{
|
| 35 |
+
"cell_type": "code",
|
| 36 |
+
"execution_count": null,
|
| 37 |
+
"id": "86cf1631",
|
| 38 |
+
"metadata": {},
|
| 39 |
+
"outputs": [],
|
| 40 |
+
"source": [
|
| 41 |
+
"# One claim target, and one noun-phrase target\n",
|
| 42 |
+
"# First prediction should be FAVOR, second should be NONE as the author says nothing about his opinion on salad itself.\n",
|
| 43 |
+
"samples = [\"Carrots are the superior vegetable.\", \"I don't like ranch on my salad.\"]\n",
|
| 44 |
+
"targets = [\"Brocoli is inferior to carrots.\", \"salad\"]\n",
|
| 45 |
+
"# Model was trained with target-then-sample. \n",
|
| 46 |
+
"encoding = tokenizer(text=targets, text_pair=samples, return_tensors='pt', padding=True)"
|
| 47 |
+
]
|
| 48 |
+
},
|
| 49 |
+
{
|
| 50 |
+
"cell_type": "code",
|
| 51 |
+
"execution_count": 8,
|
| 52 |
+
"id": "5d93f032",
|
| 53 |
+
"metadata": {},
|
| 54 |
+
"outputs": [],
|
| 55 |
+
"source": [
|
| 56 |
+
"output = model(**encoding)"
|
| 57 |
+
]
|
| 58 |
+
},
|
| 59 |
+
{
|
| 60 |
+
"cell_type": "code",
|
| 61 |
+
"execution_count": 16,
|
| 62 |
+
"id": "6a83f378",
|
| 63 |
+
"metadata": {},
|
| 64 |
+
"outputs": [
|
| 65 |
+
{
|
| 66 |
+
"data": {
|
| 67 |
+
"text/plain": [
|
| 68 |
+
"['FAVOR', 'NONE']"
|
| 69 |
+
]
|
| 70 |
+
},
|
| 71 |
+
"execution_count": 16,
|
| 72 |
+
"metadata": {},
|
| 73 |
+
"output_type": "execute_result"
|
| 74 |
+
}
|
| 75 |
+
],
|
| 76 |
+
"source": [
|
| 77 |
+
"logits = output.logits.detach().cpu().numpy()\n",
|
| 78 |
+
"preds = np.argmax(logits, axis=-1)\n",
|
| 79 |
+
"str_preds = [model.config.id2label[p] for p in preds]\n",
|
| 80 |
+
"str_preds"
|
| 81 |
+
]
|
| 82 |
+
},
|
| 83 |
+
{
|
| 84 |
+
"cell_type": "code",
|
| 85 |
+
"execution_count": null,
|
| 86 |
+
"id": "26496ec3",
|
| 87 |
+
"metadata": {},
|
| 88 |
+
"outputs": [],
|
| 89 |
+
"source": []
|
| 90 |
+
}
|
| 91 |
+
],
|
| 92 |
+
"metadata": {
|
| 93 |
+
"kernelspec": {
|
| 94 |
+
"display_name": "min_transformers",
|
| 95 |
+
"language": "python",
|
| 96 |
+
"name": "python3"
|
| 97 |
+
},
|
| 98 |
+
"language_info": {
|
| 99 |
+
"codemirror_mode": {
|
| 100 |
+
"name": "ipython",
|
| 101 |
+
"version": 3
|
| 102 |
+
},
|
| 103 |
+
"file_extension": ".py",
|
| 104 |
+
"mimetype": "text/x-python",
|
| 105 |
+
"name": "python",
|
| 106 |
+
"nbconvert_exporter": "python",
|
| 107 |
+
"pygments_lexer": "ipython3",
|
| 108 |
+
"version": "3.9.22"
|
| 109 |
+
}
|
| 110 |
+
},
|
| 111 |
+
"nbformat": 4,
|
| 112 |
+
"nbformat_minor": 5
|
| 113 |
+
}
|