File size: 6,312 Bytes
6fe7180
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-01-12T16:31:17.690349522Z",
     "start_time": "2024-01-12T16:31:15.472874479Z"
    }
   },
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "%reload_ext autoreload\n",
    "%autoreload 2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-12T16:31:17.717430741Z",
     "start_time": "2024-01-12T16:31:17.695066680Z"
    }
   },
   "id": "ecefdad828c7daa3"
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Some weights of BartForConditionalGeneration were not initialized from the model checkpoint at facebook/bart-large-cnn and are newly initialized: ['model.shared.weight']\n",
      "You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.\n"
     ]
    }
   ],
   "source": [
    "\n",
    "from transformers import AutoTokenizer, BartForConditionalGeneration\n",
    "\n",
    "tokenizer = AutoTokenizer.from_pretrained(\"facebook/bart-large-cnn\")\n",
    "model = BartForConditionalGeneration.from_pretrained(\"facebook/bart-large-cnn\")\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-12T16:31:26.058437142Z",
     "start_time": "2024-01-12T16:31:17.720106168Z"
    }
   },
   "id": "8c32b182fbcac2b6"
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [
    "from rsasumm.beam_search import RSAContextualDecoding"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-12T16:31:26.097766981Z",
     "start_time": "2024-01-12T16:31:26.056626187Z"
    }
   },
   "id": "cb33d902fe736c25"
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "texts = ['The paper gives really interesting insights on the topic of transfer learning. It is well presented and the experiment are extensive. I believe the authors missed Jane and al 2021. In addition, I think, there is a mistake in the math.',\n",
    "         'The paper gives really interesting insights on the topic of transfer learning. It is well presented and the experiment are extensive. However, some parts remain really unclear and I would like to see a more detailed explanation of the proposed method.',\n",
    "         'The paper gives really interesting insights on the topic of transfer learning. It is not well presented and lack experiments. In addition, some parts remain really unclear and I would like to see a more detailed explanation of the proposed method.'\n",
    "         ]\n",
    "\n",
    "# texts = [texts[2], texts[1], texts[0]]\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-12T16:31:26.127922110Z",
     "start_time": "2024-01-12T16:31:26.098805312Z"
    }
   },
   "id": "436ef1482c361159"
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [],
   "source": [
    "source_texts = tokenizer(texts, return_tensors=\"pt\", padding=True)\n",
    "\n",
    "rsa = RSAContextualDecoding(model, tokenizer, 'cpu')\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-12T16:31:26.169520864Z",
     "start_time": "2024-01-12T16:31:26.125283164Z"
    }
   },
   "id": "84b9943cac6cd7b2"
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [],
   "source": [
    "output = rsa.generate(target_id=1, source_texts_ids=source_texts.input_ids, source_text_attention_mask=source_texts.attention_mask, max_length=50, top_p=0.95, do_sample=True, rationality=8.0, temperature=1.0, process_logits_before_rsa=True)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-12T16:32:14.857034731Z",
     "start_time": "2024-01-12T16:31:26.164578792Z"
    }
   },
   "id": "620e54a63dd2099c"
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "['Some parts of the paper remain unclear. I would like to see a more detailed explanation of the proposed method.',\n 'Some parts of the paper remain unclear. I would like to see a more detailed explanation of the proposed method.',\n 'Some parts of the paper remain unclear. I would like to see a more detailed explanation of the proposed method.',\n 'Some parts of the paper remain unclear. I would like to see a more detailed explanation of the proposed method.',\n 'Some parts of the paper remain unclear. I would like to see a more detailed explanation of the proposed method.',\n 'Some parts of the paper remain unclear. I would like to see a more detailed explanation of the proposed method.',\n 'Some parts of the paper remain unclear. I would like to see a more detailed explanation of the proposed method.',\n 'Some parts of the paper remain unclear. I would like to see a more detailed explanation of the proposed method.']"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "tokenizer.batch_decode(output[0], skip_special_tokens=True)\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-12T16:32:14.858531480Z",
     "start_time": "2024-01-12T16:32:14.856763396Z"
    }
   },
   "id": "fb3a5a9a8f9990ee"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}