|
a |
|
b/EL_pubmedbert.ipynb |
|
|
1 |
{ |
|
|
2 |
"cells": [ |
|
|
3 |
{ |
|
|
4 |
"cell_type": "code", |
|
|
5 |
"execution_count": 1, |
|
|
6 |
"id": "138778c4", |
|
|
7 |
"metadata": { |
|
|
8 |
"id": "138778c4" |
|
|
9 |
}, |
|
|
10 |
"outputs": [], |
|
|
11 |
"source": [ |
|
|
12 |
"from tqdm import tqdm\n", |
|
|
13 |
"import pandas as pd\n", |
|
|
14 |
"from sklearn import metrics\n", |
|
|
15 |
"from scipy.spatial.distance import cdist" |
|
|
16 |
] |
|
|
17 |
}, |
|
|
18 |
{ |
|
|
19 |
"cell_type": "code", |
|
|
20 |
"execution_count": 2, |
|
|
21 |
"id": "dTSILRD7hIHG", |
|
|
22 |
"metadata": { |
|
|
23 |
"id": "dTSILRD7hIHG" |
|
|
24 |
}, |
|
|
25 |
"outputs": [ |
|
|
26 |
{ |
|
|
27 |
"name": "stdout", |
|
|
28 |
"output_type": "stream", |
|
|
29 |
"text": [ |
|
|
30 |
"Requirement already satisfied: transformers in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (4.24.0)\n", |
|
|
31 |
"Requirement already satisfied: filelock in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from transformers) (3.6.0)\n", |
|
|
32 |
"Requirement already satisfied: packaging>=20.0 in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from transformers) (21.3)\n", |
|
|
33 |
"Requirement already satisfied: tokenizers!=0.11.3,<0.14,>=0.11.1 in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from transformers) (0.13.1)\n", |
|
|
34 |
"Requirement already satisfied: huggingface-hub<1.0,>=0.10.0 in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from transformers) (0.10.1)\n", |
|
|
35 |
"Requirement already satisfied: regex!=2019.12.17 in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from transformers) (2022.7.25)\n", |
|
|
36 |
"Requirement already satisfied: numpy>=1.17 in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from transformers) (1.21.5)\n", |
|
|
37 |
"Requirement already satisfied: pyyaml>=5.1 in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from transformers) (6.0)\n", |
|
|
38 |
"Requirement already satisfied: tqdm>=4.27 in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from transformers) (4.64.1)\n", |
|
|
39 |
"Requirement already satisfied: requests in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from transformers) (2.27.1)\n", |
|
|
40 |
"Requirement already satisfied: typing-extensions>=3.7.4.3 in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from huggingface-hub<1.0,>=0.10.0->transformers) (4.1.1)\n", |
|
|
41 |
"Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from packaging>=20.0->transformers) (2.4.7)\n", |
|
|
42 |
"Requirement already satisfied: idna<4,>=2.5 in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from requests->transformers) (3.3)\n", |
|
|
43 |
"Requirement already satisfied: certifi>=2017.4.17 in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from requests->transformers) (2021.10.8)\n", |
|
|
44 |
"Requirement already satisfied: charset-normalizer~=2.0.0 in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from requests->transformers) (2.0.4)\n", |
|
|
45 |
"Requirement already satisfied: urllib3<1.27,>=1.21.1 in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from requests->transformers) (1.26.9)\n" |
|
|
46 |
] |
|
|
47 |
} |
|
|
48 |
], |
|
|
49 |
"source": [ |
|
|
50 |
"!pip install transformers" |
|
|
51 |
] |
|
|
52 |
}, |
|
|
53 |
{ |
|
|
54 |
"cell_type": "code", |
|
|
55 |
"execution_count": 3, |
|
|
56 |
"id": "70512d4e", |
|
|
57 |
"metadata": { |
|
|
58 |
"id": "70512d4e" |
|
|
59 |
}, |
|
|
60 |
"outputs": [ |
|
|
61 |
{ |
|
|
62 |
"name": "stdout", |
|
|
63 |
"output_type": "stream", |
|
|
64 |
"text": [ |
|
|
65 |
"Requirement already satisfied: parallelformers in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (1.2.7)\n", |
|
|
66 |
"Requirement already satisfied: dacite in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from parallelformers) (1.6.0)\n", |
|
|
67 |
"Requirement already satisfied: torch in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from parallelformers) (1.11.0)\n", |
|
|
68 |
"Requirement already satisfied: transformers>=4.2 in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from parallelformers) (4.24.0)\n", |
|
|
69 |
"Requirement already satisfied: requests in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from transformers>=4.2->parallelformers) (2.27.1)\n", |
|
|
70 |
"Requirement already satisfied: tokenizers!=0.11.3,<0.14,>=0.11.1 in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from transformers>=4.2->parallelformers) (0.13.1)\n", |
|
|
71 |
"Requirement already satisfied: regex!=2019.12.17 in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from transformers>=4.2->parallelformers) (2022.7.25)\n", |
|
|
72 |
"Requirement already satisfied: huggingface-hub<1.0,>=0.10.0 in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from transformers>=4.2->parallelformers) (0.10.1)\n", |
|
|
73 |
"Requirement already satisfied: tqdm>=4.27 in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from transformers>=4.2->parallelformers) (4.64.1)\n", |
|
|
74 |
"Requirement already satisfied: pyyaml>=5.1 in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from transformers>=4.2->parallelformers) (6.0)\n", |
|
|
75 |
"Requirement already satisfied: packaging>=20.0 in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from transformers>=4.2->parallelformers) (21.3)\n", |
|
|
76 |
"Requirement already satisfied: filelock in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from transformers>=4.2->parallelformers) (3.6.0)\n", |
|
|
77 |
"Requirement already satisfied: numpy>=1.17 in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from transformers>=4.2->parallelformers) (1.21.5)\n", |
|
|
78 |
"Requirement already satisfied: typing-extensions>=3.7.4.3 in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from huggingface-hub<1.0,>=0.10.0->transformers>=4.2->parallelformers) (4.1.1)\n", |
|
|
79 |
"Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from packaging>=20.0->transformers>=4.2->parallelformers) (2.4.7)\n", |
|
|
80 |
"Requirement already satisfied: charset-normalizer~=2.0.0 in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from requests->transformers>=4.2->parallelformers) (2.0.4)\n", |
|
|
81 |
"Requirement already satisfied: idna<4,>=2.5 in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from requests->transformers>=4.2->parallelformers) (3.3)\n", |
|
|
82 |
"Requirement already satisfied: urllib3<1.27,>=1.21.1 in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from requests->transformers>=4.2->parallelformers) (1.26.9)\n", |
|
|
83 |
"Requirement already satisfied: certifi>=2017.4.17 in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (from requests->transformers>=4.2->parallelformers) (2021.10.8)\n" |
|
|
84 |
] |
|
|
85 |
} |
|
|
86 |
], |
|
|
87 |
"source": [ |
|
|
88 |
"!pip install parallelformers" |
|
|
89 |
] |
|
|
90 |
}, |
|
|
91 |
{ |
|
|
92 |
"cell_type": "code", |
|
|
93 |
"execution_count": 4, |
|
|
94 |
"id": "6b2ace9d", |
|
|
95 |
"metadata": { |
|
|
96 |
"id": "6b2ace9d" |
|
|
97 |
}, |
|
|
98 |
"outputs": [ |
|
|
99 |
{ |
|
|
100 |
"name": "stdout", |
|
|
101 |
"output_type": "stream", |
|
|
102 |
"text": [ |
|
|
103 |
"Requirement already satisfied: faiss-gpu in /home2/sashank.sridhar/miniconda3/envs/TripletLoss/lib/python3.9/site-packages (1.7.2)\r\n" |
|
|
104 |
] |
|
|
105 |
} |
|
|
106 |
], |
|
|
107 |
"source": [ |
|
|
108 |
"!pip install faiss-gpu" |
|
|
109 |
] |
|
|
110 |
}, |
|
|
111 |
{ |
|
|
112 |
"cell_type": "markdown", |
|
|
113 |
"id": "5a0d2481", |
|
|
114 |
"metadata": { |
|
|
115 |
"id": "5a0d2481" |
|
|
116 |
}, |
|
|
117 |
"source": [ |
|
|
118 |
"Download snomed term-concept file from UMLS website" |
|
|
119 |
] |
|
|
120 |
}, |
|
|
121 |
{ |
|
|
122 |
"cell_type": "code", |
|
|
123 |
"execution_count": 5, |
|
|
124 |
"id": "ea498c9d", |
|
|
125 |
"metadata": { |
|
|
126 |
"id": "ea498c9d" |
|
|
127 |
}, |
|
|
128 |
"outputs": [ |
|
|
129 |
{ |
|
|
130 |
"name": "stdout", |
|
|
131 |
"output_type": "stream", |
|
|
132 |
"text": [ |
|
|
133 |
"1569232\n" |
|
|
134 |
] |
|
|
135 |
} |
|
|
136 |
], |
|
|
137 |
"source": [ |
|
|
138 |
"snomed_csv = pd.read_csv(\"sct2_Description_Snapshot-en_INT_20220831.txt\", delimiter=\"\\t\")\n", |
|
|
139 |
"print(len(snomed_csv))" |
|
|
140 |
] |
|
|
141 |
}, |
|
|
142 |
{ |
|
|
143 |
"cell_type": "code", |
|
|
144 |
"execution_count": 6, |
|
|
145 |
"id": "DkqmgM5uxfob", |
|
|
146 |
"metadata": { |
|
|
147 |
"id": "DkqmgM5uxfob" |
|
|
148 |
}, |
|
|
149 |
"outputs": [], |
|
|
150 |
"source": [ |
|
|
151 |
"# from google.colab import drive\n", |
|
|
152 |
"# drive.mount('/content/drive')" |
|
|
153 |
] |
|
|
154 |
}, |
|
|
155 |
{ |
|
|
156 |
"cell_type": "code", |
|
|
157 |
"execution_count": 7, |
|
|
158 |
"id": "d811cd3c", |
|
|
159 |
"metadata": { |
|
|
160 |
"id": "d811cd3c" |
|
|
161 |
}, |
|
|
162 |
"outputs": [ |
|
|
163 |
{ |
|
|
164 |
"data": { |
|
|
165 |
"text/plain": [ |
|
|
166 |
"Index(['id', 'effectiveTime', 'active', 'moduleId', 'conceptId',\n", |
|
|
167 |
" 'languageCode', 'typeId', 'term', 'caseSignificanceId'],\n", |
|
|
168 |
" dtype='object')" |
|
|
169 |
] |
|
|
170 |
}, |
|
|
171 |
"execution_count": 7, |
|
|
172 |
"metadata": {}, |
|
|
173 |
"output_type": "execute_result" |
|
|
174 |
} |
|
|
175 |
], |
|
|
176 |
"source": [ |
|
|
177 |
"snomed_csv.columns" |
|
|
178 |
] |
|
|
179 |
}, |
|
|
180 |
{ |
|
|
181 |
"cell_type": "code", |
|
|
182 |
"execution_count": 8, |
|
|
183 |
"id": "3327f0d9", |
|
|
184 |
"metadata": { |
|
|
185 |
"id": "3327f0d9" |
|
|
186 |
}, |
|
|
187 |
"outputs": [ |
|
|
188 |
{ |
|
|
189 |
"data": { |
|
|
190 |
"text/html": [ |
|
|
191 |
"<div>\n", |
|
|
192 |
"<style scoped>\n", |
|
|
193 |
" .dataframe tbody tr th:only-of-type {\n", |
|
|
194 |
" vertical-align: middle;\n", |
|
|
195 |
" }\n", |
|
|
196 |
"\n", |
|
|
197 |
" .dataframe tbody tr th {\n", |
|
|
198 |
" vertical-align: top;\n", |
|
|
199 |
" }\n", |
|
|
200 |
"\n", |
|
|
201 |
" .dataframe thead th {\n", |
|
|
202 |
" text-align: right;\n", |
|
|
203 |
" }\n", |
|
|
204 |
"</style>\n", |
|
|
205 |
"<table border=\"1\" class=\"dataframe\">\n", |
|
|
206 |
" <thead>\n", |
|
|
207 |
" <tr style=\"text-align: right;\">\n", |
|
|
208 |
" <th></th>\n", |
|
|
209 |
" <th>id</th>\n", |
|
|
210 |
" <th>effectiveTime</th>\n", |
|
|
211 |
" <th>active</th>\n", |
|
|
212 |
" <th>moduleId</th>\n", |
|
|
213 |
" <th>conceptId</th>\n", |
|
|
214 |
" <th>languageCode</th>\n", |
|
|
215 |
" <th>typeId</th>\n", |
|
|
216 |
" <th>term</th>\n", |
|
|
217 |
" <th>caseSignificanceId</th>\n", |
|
|
218 |
" </tr>\n", |
|
|
219 |
" </thead>\n", |
|
|
220 |
" <tbody>\n", |
|
|
221 |
" <tr>\n", |
|
|
222 |
" <th>0</th>\n", |
|
|
223 |
" <td>101013</td>\n", |
|
|
224 |
" <td>20170731</td>\n", |
|
|
225 |
" <td>1</td>\n", |
|
|
226 |
" <td>900000000000207008</td>\n", |
|
|
227 |
" <td>126813005</td>\n", |
|
|
228 |
" <td>en</td>\n", |
|
|
229 |
" <td>900000000000013009</td>\n", |
|
|
230 |
" <td>Neoplasm of anterior aspect of epiglottis</td>\n", |
|
|
231 |
" <td>900000000000448009</td>\n", |
|
|
232 |
" </tr>\n", |
|
|
233 |
" <tr>\n", |
|
|
234 |
" <th>1</th>\n", |
|
|
235 |
" <td>102018</td>\n", |
|
|
236 |
" <td>20170731</td>\n", |
|
|
237 |
" <td>1</td>\n", |
|
|
238 |
" <td>900000000000207008</td>\n", |
|
|
239 |
" <td>126814004</td>\n", |
|
|
240 |
" <td>en</td>\n", |
|
|
241 |
" <td>900000000000013009</td>\n", |
|
|
242 |
" <td>Neoplasm of junctional region of epiglottis</td>\n", |
|
|
243 |
" <td>900000000000448009</td>\n", |
|
|
244 |
" </tr>\n", |
|
|
245 |
" <tr>\n", |
|
|
246 |
" <th>2</th>\n", |
|
|
247 |
" <td>103011</td>\n", |
|
|
248 |
" <td>20170731</td>\n", |
|
|
249 |
" <td>1</td>\n", |
|
|
250 |
" <td>900000000000207008</td>\n", |
|
|
251 |
" <td>126815003</td>\n", |
|
|
252 |
" <td>en</td>\n", |
|
|
253 |
" <td>900000000000013009</td>\n", |
|
|
254 |
" <td>Neoplasm of lateral wall of oropharynx</td>\n", |
|
|
255 |
" <td>900000000000448009</td>\n", |
|
|
256 |
" </tr>\n", |
|
|
257 |
" <tr>\n", |
|
|
258 |
" <th>3</th>\n", |
|
|
259 |
" <td>104017</td>\n", |
|
|
260 |
" <td>20170731</td>\n", |
|
|
261 |
" <td>1</td>\n", |
|
|
262 |
" <td>900000000000207008</td>\n", |
|
|
263 |
" <td>126816002</td>\n", |
|
|
264 |
" <td>en</td>\n", |
|
|
265 |
" <td>900000000000013009</td>\n", |
|
|
266 |
" <td>Neoplasm of posterior wall of oropharynx</td>\n", |
|
|
267 |
" <td>900000000000448009</td>\n", |
|
|
268 |
" </tr>\n", |
|
|
269 |
" <tr>\n", |
|
|
270 |
" <th>4</th>\n", |
|
|
271 |
" <td>105016</td>\n", |
|
|
272 |
" <td>20170731</td>\n", |
|
|
273 |
" <td>1</td>\n", |
|
|
274 |
" <td>900000000000207008</td>\n", |
|
|
275 |
" <td>126817006</td>\n", |
|
|
276 |
" <td>en</td>\n", |
|
|
277 |
" <td>900000000000013009</td>\n", |
|
|
278 |
" <td>Neoplasm of esophagus</td>\n", |
|
|
279 |
" <td>900000000000448009</td>\n", |
|
|
280 |
" </tr>\n", |
|
|
281 |
" </tbody>\n", |
|
|
282 |
"</table>\n", |
|
|
283 |
"</div>" |
|
|
284 |
], |
|
|
285 |
"text/plain": [ |
|
|
286 |
" id effectiveTime active moduleId conceptId languageCode \\\n", |
|
|
287 |
"0 101013 20170731 1 900000000000207008 126813005 en \n", |
|
|
288 |
"1 102018 20170731 1 900000000000207008 126814004 en \n", |
|
|
289 |
"2 103011 20170731 1 900000000000207008 126815003 en \n", |
|
|
290 |
"3 104017 20170731 1 900000000000207008 126816002 en \n", |
|
|
291 |
"4 105016 20170731 1 900000000000207008 126817006 en \n", |
|
|
292 |
"\n", |
|
|
293 |
" typeId term \\\n", |
|
|
294 |
"0 900000000000013009 Neoplasm of anterior aspect of epiglottis \n", |
|
|
295 |
"1 900000000000013009 Neoplasm of junctional region of epiglottis \n", |
|
|
296 |
"2 900000000000013009 Neoplasm of lateral wall of oropharynx \n", |
|
|
297 |
"3 900000000000013009 Neoplasm of posterior wall of oropharynx \n", |
|
|
298 |
"4 900000000000013009 Neoplasm of esophagus \n", |
|
|
299 |
"\n", |
|
|
300 |
" caseSignificanceId \n", |
|
|
301 |
"0 900000000000448009 \n", |
|
|
302 |
"1 900000000000448009 \n", |
|
|
303 |
"2 900000000000448009 \n", |
|
|
304 |
"3 900000000000448009 \n", |
|
|
305 |
"4 900000000000448009 " |
|
|
306 |
] |
|
|
307 |
}, |
|
|
308 |
"execution_count": 8, |
|
|
309 |
"metadata": {}, |
|
|
310 |
"output_type": "execute_result" |
|
|
311 |
} |
|
|
312 |
], |
|
|
313 |
"source": [ |
|
|
314 |
"snomed_csv.head()" |
|
|
315 |
] |
|
|
316 |
}, |
|
|
317 |
{ |
|
|
318 |
"cell_type": "markdown", |
|
|
319 |
"id": "eee36071", |
|
|
320 |
"metadata": { |
|
|
321 |
"id": "eee36071" |
|
|
322 |
}, |
|
|
323 |
"source": [ |
|
|
324 |
"Process snomed terms" |
|
|
325 |
] |
|
|
326 |
}, |
|
|
327 |
{ |
|
|
328 |
"cell_type": "code", |
|
|
329 |
"execution_count": 9, |
|
|
330 |
"id": "fc74afa8", |
|
|
331 |
"metadata": { |
|
|
332 |
"id": "fc74afa8" |
|
|
333 |
}, |
|
|
334 |
"outputs": [], |
|
|
335 |
"source": [ |
|
|
336 |
"all_ids = snomed_csv['conceptId']\n", |
|
|
337 |
"all_names = []\n", |
|
|
338 |
"for i in snomed_csv['term']:\n", |
|
|
339 |
" try:\n", |
|
|
340 |
" all_names.append(i.lower())\n", |
|
|
341 |
" except:\n", |
|
|
342 |
" all_names.append('not applicable')\n", |
|
|
343 |
"# print(i)" |
|
|
344 |
] |
|
|
345 |
}, |
|
|
346 |
{ |
|
|
347 |
"cell_type": "code", |
|
|
348 |
"execution_count": 10, |
|
|
349 |
"id": "ecbc8292", |
|
|
350 |
"metadata": { |
|
|
351 |
"id": "ecbc8292" |
|
|
352 |
}, |
|
|
353 |
"outputs": [ |
|
|
354 |
{ |
|
|
355 |
"data": { |
|
|
356 |
"text/plain": [ |
|
|
357 |
"id 1491117014\n", |
|
|
358 |
"effectiveTime 20030131\n", |
|
|
359 |
"active 1\n", |
|
|
360 |
"moduleId 900000000000207008\n", |
|
|
361 |
"conceptId 385432009\n", |
|
|
362 |
"languageCode en\n", |
|
|
363 |
"typeId 900000000000013009\n", |
|
|
364 |
"term NaN\n", |
|
|
365 |
"caseSignificanceId 900000000000020002\n", |
|
|
366 |
"Name: 906846, dtype: object" |
|
|
367 |
] |
|
|
368 |
}, |
|
|
369 |
"execution_count": 10, |
|
|
370 |
"metadata": {}, |
|
|
371 |
"output_type": "execute_result" |
|
|
372 |
} |
|
|
373 |
], |
|
|
374 |
"source": [ |
|
|
375 |
"snomed_csv.iloc[906846]" |
|
|
376 |
] |
|
|
377 |
}, |
|
|
378 |
{ |
|
|
379 |
"cell_type": "code", |
|
|
380 |
"execution_count": 11, |
|
|
381 |
"id": "6d11f0d6", |
|
|
382 |
"metadata": { |
|
|
383 |
"id": "6d11f0d6" |
|
|
384 |
}, |
|
|
385 |
"outputs": [], |
|
|
386 |
"source": [ |
|
|
387 |
"snomed_name_id = [(all_names[i], all_ids[i]) for i in range(len(all_ids))]" |
|
|
388 |
] |
|
|
389 |
}, |
|
|
390 |
{ |
|
|
391 |
"cell_type": "code", |
|
|
392 |
"execution_count": 12, |
|
|
393 |
"id": "f61e031c", |
|
|
394 |
"metadata": { |
|
|
395 |
"id": "f61e031c" |
|
|
396 |
}, |
|
|
397 |
"outputs": [ |
|
|
398 |
{ |
|
|
399 |
"data": { |
|
|
400 |
"text/plain": [ |
|
|
401 |
"1569232" |
|
|
402 |
] |
|
|
403 |
}, |
|
|
404 |
"execution_count": 12, |
|
|
405 |
"metadata": {}, |
|
|
406 |
"output_type": "execute_result" |
|
|
407 |
} |
|
|
408 |
], |
|
|
409 |
"source": [ |
|
|
410 |
"len(all_ids)" |
|
|
411 |
] |
|
|
412 |
}, |
|
|
413 |
{ |
|
|
414 |
"cell_type": "code", |
|
|
415 |
"execution_count": 13, |
|
|
416 |
"id": "8b2c1e53", |
|
|
417 |
"metadata": { |
|
|
418 |
"id": "8b2c1e53" |
|
|
419 |
}, |
|
|
420 |
"outputs": [ |
|
|
421 |
{ |
|
|
422 |
"data": { |
|
|
423 |
"text/plain": [ |
|
|
424 |
"['neoplasm of anterior aspect of epiglottis',\n", |
|
|
425 |
" 'neoplasm of junctional region of epiglottis',\n", |
|
|
426 |
" 'neoplasm of lateral wall of oropharynx',\n", |
|
|
427 |
" 'neoplasm of posterior wall of oropharynx',\n", |
|
|
428 |
" 'neoplasm of esophagus',\n", |
|
|
429 |
" 'neoplasm of cervical esophagus',\n", |
|
|
430 |
" 'neoplasm of thoracic esophagus',\n", |
|
|
431 |
" 'neoplasm of abdominal esophagus',\n", |
|
|
432 |
" 'neoplasm of middle third of esophagus',\n", |
|
|
433 |
" 'neoplasm of lower third of esophagus']" |
|
|
434 |
] |
|
|
435 |
}, |
|
|
436 |
"execution_count": 13, |
|
|
437 |
"metadata": {}, |
|
|
438 |
"output_type": "execute_result" |
|
|
439 |
} |
|
|
440 |
], |
|
|
441 |
"source": [ |
|
|
442 |
"all_names[:10]" |
|
|
443 |
] |
|
|
444 |
}, |
|
|
445 |
{ |
|
|
446 |
"cell_type": "code", |
|
|
447 |
"execution_count": 14, |
|
|
448 |
"id": "4de928c7", |
|
|
449 |
"metadata": { |
|
|
450 |
"id": "4de928c7" |
|
|
451 |
}, |
|
|
452 |
"outputs": [ |
|
|
453 |
{ |
|
|
454 |
"data": { |
|
|
455 |
"text/plain": [ |
|
|
456 |
"0 126813005\n", |
|
|
457 |
"1 126814004\n", |
|
|
458 |
"2 126815003\n", |
|
|
459 |
"3 126816002\n", |
|
|
460 |
"4 126817006\n", |
|
|
461 |
"5 126818001\n", |
|
|
462 |
"6 126819009\n", |
|
|
463 |
"7 126820003\n", |
|
|
464 |
"8 126822006\n", |
|
|
465 |
"9 126823001\n", |
|
|
466 |
"Name: conceptId, dtype: int64" |
|
|
467 |
] |
|
|
468 |
}, |
|
|
469 |
"execution_count": 14, |
|
|
470 |
"metadata": {}, |
|
|
471 |
"output_type": "execute_result" |
|
|
472 |
} |
|
|
473 |
], |
|
|
474 |
"source": [ |
|
|
475 |
"all_ids[:10]" |
|
|
476 |
] |
|
|
477 |
}, |
|
|
478 |
{ |
|
|
479 |
"cell_type": "markdown", |
|
|
480 |
"id": "0b808263", |
|
|
481 |
"metadata": { |
|
|
482 |
"id": "0b808263" |
|
|
483 |
}, |
|
|
484 |
"source": [ |
|
|
485 |
"# load pubmedbert" |
|
|
486 |
] |
|
|
487 |
}, |
|
|
488 |
{ |
|
|
489 |
"cell_type": "code", |
|
|
490 |
"execution_count": 15, |
|
|
491 |
"id": "a7c7ac5b", |
|
|
492 |
"metadata": { |
|
|
493 |
"id": "a7c7ac5b" |
|
|
494 |
}, |
|
|
495 |
"outputs": [], |
|
|
496 |
"source": [ |
|
|
497 |
"import numpy as np\n", |
|
|
498 |
"import torch" |
|
|
499 |
] |
|
|
500 |
}, |
|
|
501 |
{ |
|
|
502 |
"cell_type": "code", |
|
|
503 |
"execution_count": 16, |
|
|
504 |
"id": "d2c96dea", |
|
|
505 |
"metadata": { |
|
|
506 |
"id": "d2c96dea" |
|
|
507 |
}, |
|
|
508 |
"outputs": [], |
|
|
509 |
"source": [ |
|
|
510 |
"GPU_COUNT = torch.cuda.device_count()" |
|
|
511 |
] |
|
|
512 |
}, |
|
|
513 |
{ |
|
|
514 |
"cell_type": "code", |
|
|
515 |
"execution_count": 17, |
|
|
516 |
"id": "5c3cfade", |
|
|
517 |
"metadata": { |
|
|
518 |
"id": "5c3cfade" |
|
|
519 |
}, |
|
|
520 |
"outputs": [ |
|
|
521 |
{ |
|
|
522 |
"data": { |
|
|
523 |
"text/plain": [ |
|
|
524 |
"4" |
|
|
525 |
] |
|
|
526 |
}, |
|
|
527 |
"execution_count": 17, |
|
|
528 |
"metadata": {}, |
|
|
529 |
"output_type": "execute_result" |
|
|
530 |
} |
|
|
531 |
], |
|
|
532 |
"source": [ |
|
|
533 |
"GPU_COUNT" |
|
|
534 |
] |
|
|
535 |
}, |
|
|
536 |
{ |
|
|
537 |
"cell_type": "code", |
|
|
538 |
"execution_count": 18, |
|
|
539 |
"id": "7bd1e1f2", |
|
|
540 |
"metadata": { |
|
|
541 |
"id": "7bd1e1f2" |
|
|
542 |
}, |
|
|
543 |
"outputs": [ |
|
|
544 |
{ |
|
|
545 |
"data": { |
|
|
546 |
"text/plain": [ |
|
|
547 |
"device(type='cuda')" |
|
|
548 |
] |
|
|
549 |
}, |
|
|
550 |
"execution_count": 18, |
|
|
551 |
"metadata": {}, |
|
|
552 |
"output_type": "execute_result" |
|
|
553 |
} |
|
|
554 |
], |
|
|
555 |
"source": [ |
|
|
556 |
"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\") ## specify the GPU id's, GPU id's start from 0.\n", |
|
|
557 |
"device" |
|
|
558 |
] |
|
|
559 |
}, |
|
|
560 |
{ |
|
|
561 |
"cell_type": "code", |
|
|
562 |
"execution_count": 19, |
|
|
563 |
"id": "528023ac", |
|
|
564 |
"metadata": { |
|
|
565 |
"id": "528023ac" |
|
|
566 |
}, |
|
|
567 |
"outputs": [], |
|
|
568 |
"source": [ |
|
|
569 |
"# from transformers import AutoTokenizer, AutoModel\n", |
|
|
570 |
"# tokenizer = AutoTokenizer.from_pretrained(\"cambridgeltl/SapBERT-from-PubMedBERT-fulltext\")\n", |
|
|
571 |
"# model = AutoModel.from_pretrained(\"cambridgeltl/SapBERT-from-PubMedBERT-fulltext\")" |
|
|
572 |
] |
|
|
573 |
}, |
|
|
574 |
{ |
|
|
575 |
"cell_type": "code", |
|
|
576 |
"execution_count": 20, |
|
|
577 |
"id": "Lhh12FnfuwMq", |
|
|
578 |
"metadata": { |
|
|
579 |
"id": "Lhh12FnfuwMq" |
|
|
580 |
}, |
|
|
581 |
"outputs": [ |
|
|
582 |
{ |
|
|
583 |
"name": "stderr", |
|
|
584 |
"output_type": "stream", |
|
|
585 |
"text": [ |
|
|
586 |
"2022-11-15 11:09:16.635924: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA\n", |
|
|
587 |
"To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n", |
|
|
588 |
"2022-11-15 11:09:16.898184: E tensorflow/stream_executor/cuda/cuda_blas.cc:2981] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n", |
|
|
589 |
"2022-11-15 11:09:18.830720: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/local/cuda-10.2/lib64:/opt/cudnn-7.6.5.32-cuda-10.2/lib64\n", |
|
|
590 |
"2022-11-15 11:09:18.830877: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/local/cuda-10.2/lib64:/opt/cudnn-7.6.5.32-cuda-10.2/lib64\n", |
|
|
591 |
"2022-11-15 11:09:18.830892: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.\n" |
|
|
592 |
] |
|
|
593 |
}, |
|
|
594 |
{ |
|
|
595 |
"data": { |
|
|
596 |
"application/vnd.jupyter.widget-view+json": { |
|
|
597 |
"model_id": "6f2fe6e76459468cb502634e09fadc37", |
|
|
598 |
"version_major": 2, |
|
|
599 |
"version_minor": 0 |
|
|
600 |
}, |
|
|
601 |
"text/plain": [ |
|
|
602 |
"Downloading: 0%| | 0.00/28.0 [00:00<?, ?B/s]" |
|
|
603 |
] |
|
|
604 |
}, |
|
|
605 |
"metadata": {}, |
|
|
606 |
"output_type": "display_data" |
|
|
607 |
}, |
|
|
608 |
{ |
|
|
609 |
"data": { |
|
|
610 |
"application/vnd.jupyter.widget-view+json": { |
|
|
611 |
"model_id": "f889047f747d45ecb0602cbe37b891ea", |
|
|
612 |
"version_major": 2, |
|
|
613 |
"version_minor": 0 |
|
|
614 |
}, |
|
|
615 |
"text/plain": [ |
|
|
616 |
"Downloading: 0%| | 0.00/385 [00:00<?, ?B/s]" |
|
|
617 |
] |
|
|
618 |
}, |
|
|
619 |
"metadata": {}, |
|
|
620 |
"output_type": "display_data" |
|
|
621 |
}, |
|
|
622 |
{ |
|
|
623 |
"data": { |
|
|
624 |
"application/vnd.jupyter.widget-view+json": { |
|
|
625 |
"model_id": "dfd44f0b70de46a49826a8cd74c184da", |
|
|
626 |
"version_major": 2, |
|
|
627 |
"version_minor": 0 |
|
|
628 |
}, |
|
|
629 |
"text/plain": [ |
|
|
630 |
"Downloading: 0%| | 0.00/225k [00:00<?, ?B/s]" |
|
|
631 |
] |
|
|
632 |
}, |
|
|
633 |
"metadata": {}, |
|
|
634 |
"output_type": "display_data" |
|
|
635 |
}, |
|
|
636 |
{ |
|
|
637 |
"data": { |
|
|
638 |
"application/vnd.jupyter.widget-view+json": { |
|
|
639 |
"model_id": "4c9a209544b54c7691798d76c563af26", |
|
|
640 |
"version_major": 2, |
|
|
641 |
"version_minor": 0 |
|
|
642 |
}, |
|
|
643 |
"text/plain": [ |
|
|
644 |
"Downloading: 0%| | 0.00/440M [00:00<?, ?B/s]" |
|
|
645 |
] |
|
|
646 |
}, |
|
|
647 |
"metadata": {}, |
|
|
648 |
"output_type": "display_data" |
|
|
649 |
}, |
|
|
650 |
{ |
|
|
651 |
"name": "stderr", |
|
|
652 |
"output_type": "stream", |
|
|
653 |
"text": [ |
|
|
654 |
"Some weights of the model checkpoint at microsoft/BiomedNLP-PubMedBERT-base-uncased-abstract were not used when initializing BertModel: ['cls.predictions.transform.dense.weight', 'cls.seq_relationship.bias', 'cls.predictions.decoder.weight', 'cls.predictions.transform.dense.bias', 'cls.predictions.bias', 'cls.predictions.transform.LayerNorm.bias', 'cls.seq_relationship.weight', 'cls.predictions.decoder.bias', 'cls.predictions.transform.LayerNorm.weight']\n", |
|
|
655 |
"- This IS expected if you are initializing BertModel from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n", |
|
|
656 |
"- This IS NOT expected if you are initializing BertModel from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).\n" |
|
|
657 |
] |
|
|
658 |
} |
|
|
659 |
], |
|
|
660 |
"source": [ |
|
|
661 |
"from transformers import AutoTokenizer, AutoModel\n", |
|
|
662 |
"tokenizer = AutoTokenizer.from_pretrained(\"microsoft/BiomedNLP-PubMedBERT-base-uncased-abstract\")\n", |
|
|
663 |
"model = AutoModel.from_pretrained(\"microsoft/BiomedNLP-PubMedBERT-base-uncased-abstract\")\n", |
|
|
664 |
"# model = AutoModelForMaskedLM.from_pretrained(\"microsoft/BiomedNLP-PubMedBERT-base-uncased-abstract\")" |
|
|
665 |
] |
|
|
666 |
}, |
|
|
667 |
{ |
|
|
668 |
"cell_type": "code", |
|
|
669 |
"execution_count": 21, |
|
|
670 |
"id": "tlzJasirUq6Y", |
|
|
671 |
"metadata": { |
|
|
672 |
"id": "tlzJasirUq6Y" |
|
|
673 |
}, |
|
|
674 |
"outputs": [ |
|
|
675 |
{ |
|
|
676 |
"name": "stderr", |
|
|
677 |
"output_type": "stream", |
|
|
678 |
"text": [ |
|
|
679 |
"2022-11-15 11:10:10.057137: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA\n", |
|
|
680 |
"To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n", |
|
|
681 |
"2022-11-15 11:10:10.057137: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA\n", |
|
|
682 |
"To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n", |
|
|
683 |
"2022-11-15 11:10:10.057137: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA\n", |
|
|
684 |
"To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n", |
|
|
685 |
"2022-11-15 11:10:10.071504: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA\n", |
|
|
686 |
"To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n", |
|
|
687 |
"2022-11-15 11:10:10.318816: E tensorflow/stream_executor/cuda/cuda_blas.cc:2981] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n", |
|
|
688 |
"2022-11-15 11:10:10.320732: E tensorflow/stream_executor/cuda/cuda_blas.cc:2981] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n", |
|
|
689 |
"2022-11-15 11:10:10.334204: E tensorflow/stream_executor/cuda/cuda_blas.cc:2981] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n", |
|
|
690 |
"2022-11-15 11:10:10.341789: E tensorflow/stream_executor/cuda/cuda_blas.cc:2981] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n", |
|
|
691 |
"2022-11-15 11:10:12.013349: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/local/cuda-10.2/lib64:/opt/cudnn-7.6.5.32-cuda-10.2/lib64\n", |
|
|
692 |
"2022-11-15 11:10:12.013347: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/local/cuda-10.2/lib64:/opt/cudnn-7.6.5.32-cuda-10.2/lib64\n", |
|
|
693 |
"2022-11-15 11:10:12.013377: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/local/cuda-10.2/lib64:/opt/cudnn-7.6.5.32-cuda-10.2/lib64\n", |
|
|
694 |
"2022-11-15 11:10:12.013433: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/local/cuda-10.2/lib64:/opt/cudnn-7.6.5.32-cuda-10.2/lib64\n", |
|
|
695 |
"2022-11-15 11:10:12.013604: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/local/cuda-10.2/lib64:/opt/cudnn-7.6.5.32-cuda-10.2/lib64\n", |
|
|
696 |
"2022-11-15 11:10:12.013609: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/local/cuda-10.2/lib64:/opt/cudnn-7.6.5.32-cuda-10.2/lib64\n", |
|
|
697 |
"2022-11-15 11:10:12.013630: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.\n", |
|
|
698 |
"2022-11-15 11:10:12.013636: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.\n", |
|
|
699 |
"2022-11-15 11:10:12.013659: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/local/cuda-10.2/lib64:/opt/cudnn-7.6.5.32-cuda-10.2/lib64\n", |
|
|
700 |
"2022-11-15 11:10:12.013679: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/local/cuda-10.2/lib64:/opt/cudnn-7.6.5.32-cuda-10.2/lib64\n", |
|
|
701 |
"2022-11-15 11:10:12.013705: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.\n", |
|
|
702 |
"2022-11-15 11:10:12.013700: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.\n" |
|
|
703 |
] |
|
|
704 |
}, |
|
|
705 |
{ |
|
|
706 |
"data": { |
|
|
707 |
"text/plain": [ |
|
|
708 |
"<parallelformers.parallelize.parallelize at 0x149786bd6a60>" |
|
|
709 |
] |
|
|
710 |
}, |
|
|
711 |
"execution_count": 21, |
|
|
712 |
"metadata": {}, |
|
|
713 |
"output_type": "execute_result" |
|
|
714 |
} |
|
|
715 |
], |
|
|
716 |
"source": [ |
|
|
717 |
"# model = torch.nn.DataParallel(model)\n", |
|
|
718 |
"# model.to(device)\n", |
|
|
719 |
"from parallelformers import parallelize\n", |
|
|
720 |
"parallelize(model, num_gpus=4, fp16=True)" |
|
|
721 |
] |
|
|
722 |
}, |
|
|
723 |
{ |
|
|
724 |
"cell_type": "markdown", |
|
|
725 |
"id": "a3a24048", |
|
|
726 |
"metadata": { |
|
|
727 |
"id": "a3a24048" |
|
|
728 |
}, |
|
|
729 |
"source": [ |
|
|
730 |
"Generate embeddings for snomed labels" |
|
|
731 |
] |
|
|
732 |
}, |
|
|
733 |
{ |
|
|
734 |
"cell_type": "code", |
|
|
735 |
"execution_count": null, |
|
|
736 |
"id": "bb0b8655", |
|
|
737 |
"metadata": { |
|
|
738 |
"id": "bb0b8655" |
|
|
739 |
}, |
|
|
740 |
"outputs": [], |
|
|
741 |
"source": [ |
|
|
742 |
"# all_names1 = all_names[:100]" |
|
|
743 |
] |
|
|
744 |
}, |
|
|
745 |
{ |
|
|
746 |
"cell_type": "code", |
|
|
747 |
"execution_count": 22, |
|
|
748 |
"id": "5c5ff31c", |
|
|
749 |
"metadata": { |
|
|
750 |
"id": "5c5ff31c" |
|
|
751 |
}, |
|
|
752 |
"outputs": [ |
|
|
753 |
{ |
|
|
754 |
"name": "stderr", |
|
|
755 |
"output_type": "stream", |
|
|
756 |
"text": [ |
|
|
757 |
"100%|█████████████████████████████████████| 12260/12260 [13:26<00:00, 15.20it/s]\n" |
|
|
758 |
] |
|
|
759 |
} |
|
|
760 |
], |
|
|
761 |
"source": [ |
|
|
762 |
"bs = 128\n", |
|
|
763 |
"all_reps = []\n", |
|
|
764 |
"for i in tqdm(np.arange(0, len(all_names), bs)):\n", |
|
|
765 |
" toks = tokenizer.batch_encode_plus(all_names[i:i+bs],\n", |
|
|
766 |
" padding=\"max_length\",\n", |
|
|
767 |
" max_length=25,\n", |
|
|
768 |
" truncation=True,\n", |
|
|
769 |
" return_tensors=\"pt\")\n", |
|
|
770 |
" toks = toks.to(device)\n", |
|
|
771 |
" output = model(**toks)\n", |
|
|
772 |
" cls_rep = output[0][:,0,:]\n", |
|
|
773 |
" \n", |
|
|
774 |
" all_reps.append(cls_rep.cpu().detach().numpy())\n", |
|
|
775 |
"all_reps_emb = np.concatenate(all_reps, axis=0)" |
|
|
776 |
] |
|
|
777 |
}, |
|
|
778 |
{ |
|
|
779 |
"cell_type": "code", |
|
|
780 |
"execution_count": 23, |
|
|
781 |
"id": "c1230654", |
|
|
782 |
"metadata": { |
|
|
783 |
"id": "c1230654" |
|
|
784 |
}, |
|
|
785 |
"outputs": [ |
|
|
786 |
{ |
|
|
787 |
"name": "stdout", |
|
|
788 |
"output_type": "stream", |
|
|
789 |
"text": [ |
|
|
790 |
"(1569232, 768)\n" |
|
|
791 |
] |
|
|
792 |
} |
|
|
793 |
], |
|
|
794 |
"source": [ |
|
|
795 |
"print(all_reps_emb.shape)" |
|
|
796 |
] |
|
|
797 |
}, |
|
|
798 |
{ |
|
|
799 |
"cell_type": "code", |
|
|
800 |
"execution_count": 24, |
|
|
801 |
"id": "00a427c1", |
|
|
802 |
"metadata": { |
|
|
803 |
"id": "00a427c1" |
|
|
804 |
}, |
|
|
805 |
"outputs": [], |
|
|
806 |
"source": [ |
|
|
807 |
"all_reps_emb = all_reps_emb.astype(np.float32)" |
|
|
808 |
] |
|
|
809 |
}, |
|
|
810 |
{ |
|
|
811 |
"cell_type": "code", |
|
|
812 |
"execution_count": 25, |
|
|
813 |
"id": "3c884582", |
|
|
814 |
"metadata": { |
|
|
815 |
"id": "3c884582" |
|
|
816 |
}, |
|
|
817 |
"outputs": [], |
|
|
818 |
"source": [ |
|
|
819 |
"import faiss" |
|
|
820 |
] |
|
|
821 |
}, |
|
|
822 |
{ |
|
|
823 |
"cell_type": "code", |
|
|
824 |
"execution_count": 26, |
|
|
825 |
"id": "9d7d069d", |
|
|
826 |
"metadata": { |
|
|
827 |
"id": "9d7d069d" |
|
|
828 |
}, |
|
|
829 |
"outputs": [ |
|
|
830 |
{ |
|
|
831 |
"name": "stdout", |
|
|
832 |
"output_type": "stream", |
|
|
833 |
"text": [ |
|
|
834 |
"True\n" |
|
|
835 |
] |
|
|
836 |
} |
|
|
837 |
], |
|
|
838 |
"source": [ |
|
|
839 |
"d = all_reps_emb.shape[1]\n", |
|
|
840 |
"index = faiss.IndexFlatL2(d) # build the index\n", |
|
|
841 |
"print(index.is_trained)" |
|
|
842 |
] |
|
|
843 |
}, |
|
|
844 |
{ |
|
|
845 |
"cell_type": "code", |
|
|
846 |
"execution_count": 27, |
|
|
847 |
"id": "77b258e0", |
|
|
848 |
"metadata": { |
|
|
849 |
"id": "77b258e0" |
|
|
850 |
}, |
|
|
851 |
"outputs": [ |
|
|
852 |
{ |
|
|
853 |
"name": "stdout", |
|
|
854 |
"output_type": "stream", |
|
|
855 |
"text": [ |
|
|
856 |
"1569232\n" |
|
|
857 |
] |
|
|
858 |
} |
|
|
859 |
], |
|
|
860 |
"source": [ |
|
|
861 |
"index.add(all_reps_emb) # add vectors to the index\n", |
|
|
862 |
"print(index.ntotal)" |
|
|
863 |
] |
|
|
864 |
}, |
|
|
865 |
{ |
|
|
866 |
"cell_type": "markdown", |
|
|
867 |
"id": "40fe39a4", |
|
|
868 |
"metadata": { |
|
|
869 |
"id": "40fe39a4" |
|
|
870 |
}, |
|
|
871 |
"source": [ |
|
|
872 |
"Load ground truth data" |
|
|
873 |
] |
|
|
874 |
}, |
|
|
875 |
{ |
|
|
876 |
"cell_type": "code", |
|
|
877 |
"execution_count": 28, |
|
|
878 |
"id": "44851e30", |
|
|
879 |
"metadata": { |
|
|
880 |
"id": "44851e30" |
|
|
881 |
}, |
|
|
882 |
"outputs": [ |
|
|
883 |
{ |
|
|
884 |
"data": { |
|
|
885 |
"text/html": [ |
|
|
886 |
"<div>\n", |
|
|
887 |
"<style scoped>\n", |
|
|
888 |
" .dataframe tbody tr th:only-of-type {\n", |
|
|
889 |
" vertical-align: middle;\n", |
|
|
890 |
" }\n", |
|
|
891 |
"\n", |
|
|
892 |
" .dataframe tbody tr th {\n", |
|
|
893 |
" vertical-align: top;\n", |
|
|
894 |
" }\n", |
|
|
895 |
"\n", |
|
|
896 |
" .dataframe thead th {\n", |
|
|
897 |
" text-align: right;\n", |
|
|
898 |
" }\n", |
|
|
899 |
"</style>\n", |
|
|
900 |
"<table border=\"1\" class=\"dataframe\">\n", |
|
|
901 |
" <thead>\n", |
|
|
902 |
" <tr style=\"text-align: right;\">\n", |
|
|
903 |
" <th></th>\n", |
|
|
904 |
" <th>filename</th>\n", |
|
|
905 |
" <th>mark</th>\n", |
|
|
906 |
" <th>label</th>\n", |
|
|
907 |
" <th>offset1</th>\n", |
|
|
908 |
" <th>offset2</th>\n", |
|
|
909 |
" <th>span</th>\n", |
|
|
910 |
" <th>code</th>\n", |
|
|
911 |
" </tr>\n", |
|
|
912 |
" </thead>\n", |
|
|
913 |
" <tbody>\n", |
|
|
914 |
" <tr>\n", |
|
|
915 |
" <th>0</th>\n", |
|
|
916 |
" <td>es-S0212-71992007000100007-1</td>\n", |
|
|
917 |
" <td>T1</td>\n", |
|
|
918 |
" <td>ENFERMEDAD</td>\n", |
|
|
919 |
" <td>40</td>\n", |
|
|
920 |
" <td>61</td>\n", |
|
|
921 |
" <td>arterial hypertension</td>\n", |
|
|
922 |
" <td>38341003</td>\n", |
|
|
923 |
" </tr>\n", |
|
|
924 |
" <tr>\n", |
|
|
925 |
" <th>1</th>\n", |
|
|
926 |
" <td>es-S0212-71992007000100007-1</td>\n", |
|
|
927 |
" <td>T2</td>\n", |
|
|
928 |
" <td>ENFERMEDAD</td>\n", |
|
|
929 |
" <td>66</td>\n", |
|
|
930 |
" <td>79</td>\n", |
|
|
931 |
" <td>polyarthrosis</td>\n", |
|
|
932 |
" <td>36186002</td>\n", |
|
|
933 |
" </tr>\n", |
|
|
934 |
" <tr>\n", |
|
|
935 |
" <th>2</th>\n", |
|
|
936 |
" <td>es-S0212-71992007000100007-1</td>\n", |
|
|
937 |
" <td>T3</td>\n", |
|
|
938 |
" <td>ENFERMEDAD</td>\n", |
|
|
939 |
" <td>1682</td>\n", |
|
|
940 |
" <td>1698</td>\n", |
|
|
941 |
" <td>pleural effusion</td>\n", |
|
|
942 |
" <td>60046008</td>\n", |
|
|
943 |
" </tr>\n", |
|
|
944 |
" <tr>\n", |
|
|
945 |
" <th>3</th>\n", |
|
|
946 |
" <td>es-S0212-71992007000100007-1</td>\n", |
|
|
947 |
" <td>T4</td>\n", |
|
|
948 |
" <td>ENFERMEDAD</td>\n", |
|
|
949 |
" <td>1859</td>\n", |
|
|
950 |
" <td>1875</td>\n", |
|
|
951 |
" <td>pleural effusion</td>\n", |
|
|
952 |
" <td>60046008</td>\n", |
|
|
953 |
" </tr>\n", |
|
|
954 |
" <tr>\n", |
|
|
955 |
" <th>4</th>\n", |
|
|
956 |
" <td>es-S0212-71992007000100007-1</td>\n", |
|
|
957 |
" <td>T5</td>\n", |
|
|
958 |
" <td>ENFERMEDAD</td>\n", |
|
|
959 |
" <td>1626</td>\n", |
|
|
960 |
" <td>1648</td>\n", |
|
|
961 |
" <td>lower lobe atelectasis</td>\n", |
|
|
962 |
" <td>46621007</td>\n", |
|
|
963 |
" </tr>\n", |
|
|
964 |
" </tbody>\n", |
|
|
965 |
"</table>\n", |
|
|
966 |
"</div>" |
|
|
967 |
], |
|
|
968 |
"text/plain": [ |
|
|
969 |
" filename mark label offset1 offset2 \\\n", |
|
|
970 |
"0 es-S0212-71992007000100007-1 T1 ENFERMEDAD 40 61 \n", |
|
|
971 |
"1 es-S0212-71992007000100007-1 T2 ENFERMEDAD 66 79 \n", |
|
|
972 |
"2 es-S0212-71992007000100007-1 T3 ENFERMEDAD 1682 1698 \n", |
|
|
973 |
"3 es-S0212-71992007000100007-1 T4 ENFERMEDAD 1859 1875 \n", |
|
|
974 |
"4 es-S0212-71992007000100007-1 T5 ENFERMEDAD 1626 1648 \n", |
|
|
975 |
"\n", |
|
|
976 |
" span code \n", |
|
|
977 |
"0 arterial hypertension 38341003 \n", |
|
|
978 |
"1 polyarthrosis 36186002 \n", |
|
|
979 |
"2 pleural effusion 60046008 \n", |
|
|
980 |
"3 pleural effusion 60046008 \n", |
|
|
981 |
"4 lower lobe atelectasis 46621007 " |
|
|
982 |
] |
|
|
983 |
}, |
|
|
984 |
"execution_count": 28, |
|
|
985 |
"metadata": {}, |
|
|
986 |
"output_type": "execute_result" |
|
|
987 |
} |
|
|
988 |
], |
|
|
989 |
"source": [ |
|
|
990 |
"entities = pd.read_csv(\"entities.tsv\", delimiter=\"\\t\")\n", |
|
|
991 |
"entities.head()" |
|
|
992 |
] |
|
|
993 |
}, |
|
|
994 |
{ |
|
|
995 |
"cell_type": "code", |
|
|
996 |
"execution_count": 88, |
|
|
997 |
"id": "8a009c68", |
|
|
998 |
"metadata": { |
|
|
999 |
"id": "8a009c68", |
|
|
1000 |
"scrolled": true |
|
|
1001 |
}, |
|
|
1002 |
"outputs": [ |
|
|
1003 |
{ |
|
|
1004 |
"name": "stdout", |
|
|
1005 |
"output_type": "stream", |
|
|
1006 |
"text": [ |
|
|
1007 |
"['arterial hypertension', 'polyarthrosis', 'pleural effusion', 'pleural effusion', 'lower lobe atelectasis', 'infectious spondylodiscitis d10-d11', 'pleural effusion', 'brucellosis', 'orchiepididymitis', 'goitre']\n", |
|
|
1008 |
"0 38341003\n", |
|
|
1009 |
"1 36186002\n", |
|
|
1010 |
"2 60046008\n", |
|
|
1011 |
"3 60046008\n", |
|
|
1012 |
"4 46621007\n", |
|
|
1013 |
"5 302935008\n", |
|
|
1014 |
"6 60046008\n", |
|
|
1015 |
"7 75702008\n", |
|
|
1016 |
"8 197983000\n", |
|
|
1017 |
"9 3716002\n", |
|
|
1018 |
"Name: code, dtype: object\n" |
|
|
1019 |
] |
|
|
1020 |
} |
|
|
1021 |
], |
|
|
1022 |
"source": [ |
|
|
1023 |
"inp_names = [i.lower() for i in entities['span']]\n", |
|
|
1024 |
"inp_labels = entities['code']\n", |
|
|
1025 |
"print(inp_names[:10])\n", |
|
|
1026 |
"print(inp_labels[:10])" |
|
|
1027 |
] |
|
|
1028 |
}, |
|
|
1029 |
{ |
|
|
1030 |
"cell_type": "code", |
|
|
1031 |
"execution_count": 89, |
|
|
1032 |
"id": "90bbf268", |
|
|
1033 |
"metadata": { |
|
|
1034 |
"id": "90bbf268" |
|
|
1035 |
}, |
|
|
1036 |
"outputs": [], |
|
|
1037 |
"source": [ |
|
|
1038 |
"# c=0\n", |
|
|
1039 |
"# for i in inp_label:\n", |
|
|
1040 |
"# # if type(i)!=float:\n", |
|
|
1041 |
"# try:\n", |
|
|
1042 |
"# [float(i)]\n", |
|
|
1043 |
"# except:\n", |
|
|
1044 |
"# c+=1\n", |
|
|
1045 |
"# # print(i.split('+'))\n", |
|
|
1046 |
"# c" |
|
|
1047 |
] |
|
|
1048 |
}, |
|
|
1049 |
{ |
|
|
1050 |
"cell_type": "code", |
|
|
1051 |
"execution_count": 90, |
|
|
1052 |
"id": "49562d03", |
|
|
1053 |
"metadata": { |
|
|
1054 |
"id": "49562d03" |
|
|
1055 |
}, |
|
|
1056 |
"outputs": [], |
|
|
1057 |
"source": [ |
|
|
1058 |
"# inp_names1 = inp_names[:10]" |
|
|
1059 |
] |
|
|
1060 |
}, |
|
|
1061 |
{ |
|
|
1062 |
"cell_type": "markdown", |
|
|
1063 |
"id": "e6cf5d29", |
|
|
1064 |
"metadata": { |
|
|
1065 |
"id": "e6cf5d29" |
|
|
1066 |
}, |
|
|
1067 |
"source": [ |
|
|
1068 |
"Generate embeddings for ground truth terms, get their closest snomedct embedding and list out its corresponding snomedct code" |
|
|
1069 |
] |
|
|
1070 |
}, |
|
|
1071 |
{ |
|
|
1072 |
"cell_type": "code", |
|
|
1073 |
"execution_count": 91, |
|
|
1074 |
"id": "049818b3", |
|
|
1075 |
"metadata": { |
|
|
1076 |
"id": "049818b3" |
|
|
1077 |
}, |
|
|
1078 |
"outputs": [], |
|
|
1079 |
"source": [ |
|
|
1080 |
"query_toks = tokenizer.batch_encode_plus(list(inp_names),\n", |
|
|
1081 |
" padding = \"max_length\",\n", |
|
|
1082 |
" max_length = 25,\n", |
|
|
1083 |
" truncation=True,\n", |
|
|
1084 |
" return_tensors=\"pt\")\n", |
|
|
1085 |
"query_toks = query_toks.to(device)\n", |
|
|
1086 |
"query_output = model(**query_toks)\n", |
|
|
1087 |
"query_cls_rep = query_output[0][:,0,:]" |
|
|
1088 |
] |
|
|
1089 |
}, |
|
|
1090 |
{ |
|
|
1091 |
"cell_type": "code", |
|
|
1092 |
"execution_count": 92, |
|
|
1093 |
"id": "f0ab19b8", |
|
|
1094 |
"metadata": { |
|
|
1095 |
"id": "f0ab19b8" |
|
|
1096 |
}, |
|
|
1097 |
"outputs": [], |
|
|
1098 |
"source": [ |
|
|
1099 |
"query_cls_rep = query_cls_rep.cpu().detach().numpy()" |
|
|
1100 |
] |
|
|
1101 |
}, |
|
|
1102 |
{ |
|
|
1103 |
"cell_type": "code", |
|
|
1104 |
"execution_count": 93, |
|
|
1105 |
"id": "3d90a519", |
|
|
1106 |
"metadata": { |
|
|
1107 |
"id": "3d90a519" |
|
|
1108 |
}, |
|
|
1109 |
"outputs": [], |
|
|
1110 |
"source": [ |
|
|
1111 |
"query_cls_rep = query_cls_rep.astype(np.float32)" |
|
|
1112 |
] |
|
|
1113 |
}, |
|
|
1114 |
{ |
|
|
1115 |
"cell_type": "code", |
|
|
1116 |
"execution_count": 94, |
|
|
1117 |
"id": "184cd570", |
|
|
1118 |
"metadata": { |
|
|
1119 |
"id": "184cd570" |
|
|
1120 |
}, |
|
|
1121 |
"outputs": [], |
|
|
1122 |
"source": [ |
|
|
1123 |
"k= 1 # take the 1 closest neighbor" |
|
|
1124 |
] |
|
|
1125 |
}, |
|
|
1126 |
{ |
|
|
1127 |
"cell_type": "code", |
|
|
1128 |
"execution_count": 95, |
|
|
1129 |
"id": "ac0965a5", |
|
|
1130 |
"metadata": { |
|
|
1131 |
"id": "ac0965a5" |
|
|
1132 |
}, |
|
|
1133 |
"outputs": [], |
|
|
1134 |
"source": [ |
|
|
1135 |
"D, I = index.search(query_cls_rep, k)" |
|
|
1136 |
] |
|
|
1137 |
}, |
|
|
1138 |
{ |
|
|
1139 |
"cell_type": "code", |
|
|
1140 |
"execution_count": null, |
|
|
1141 |
"id": "4cfb69ca", |
|
|
1142 |
"metadata": { |
|
|
1143 |
"id": "4cfb69ca" |
|
|
1144 |
}, |
|
|
1145 |
"outputs": [], |
|
|
1146 |
"source": [] |
|
|
1147 |
}, |
|
|
1148 |
{ |
|
|
1149 |
"cell_type": "code", |
|
|
1150 |
"execution_count": 96, |
|
|
1151 |
"id": "2145e65a", |
|
|
1152 |
"metadata": { |
|
|
1153 |
"id": "2145e65a" |
|
|
1154 |
}, |
|
|
1155 |
"outputs": [], |
|
|
1156 |
"source": [ |
|
|
1157 |
"pred_ids = [all_ids[i[0]] for i in I]\n", |
|
|
1158 |
"# score=sum((pred_ids[i]==inp_label[i])*1 for i in range(len(pred_ids)))\n", |
|
|
1159 |
"# score/len(inp_label)" |
|
|
1160 |
] |
|
|
1161 |
}, |
|
|
1162 |
{ |
|
|
1163 |
"cell_type": "markdown", |
|
|
1164 |
"id": "85c1243b", |
|
|
1165 |
"metadata": { |
|
|
1166 |
"id": "85c1243b" |
|
|
1167 |
}, |
|
|
1168 |
"source": [ |
|
|
1169 |
"In ground truth, zero or more than one codes are also present for each term; here only one code is predicted for each query" |
|
|
1170 |
] |
|
|
1171 |
}, |
|
|
1172 |
{ |
|
|
1173 |
"cell_type": "code", |
|
|
1174 |
"execution_count": 97, |
|
|
1175 |
"id": "d7476a77", |
|
|
1176 |
"metadata": { |
|
|
1177 |
"id": "d7476a77" |
|
|
1178 |
}, |
|
|
1179 |
"outputs": [], |
|
|
1180 |
"source": [ |
|
|
1181 |
"p = [[i] for i in pred_ids]\n", |
|
|
1182 |
"t = []\n", |
|
|
1183 |
"for i in inp_labels:\n", |
|
|
1184 |
" try:\n", |
|
|
1185 |
" t.append([int(i)])\n", |
|
|
1186 |
" except:\n", |
|
|
1187 |
" try:\n", |
|
|
1188 |
" t.append([int(j) for j in (i.split('+'))])\n", |
|
|
1189 |
" except:\n", |
|
|
1190 |
"# print('nomap')\n", |
|
|
1191 |
" t.append([])\n" |
|
|
1192 |
] |
|
|
1193 |
}, |
|
|
1194 |
{ |
|
|
1195 |
"cell_type": "code", |
|
|
1196 |
"execution_count": 98, |
|
|
1197 |
"id": "5a676132", |
|
|
1198 |
"metadata": { |
|
|
1199 |
"id": "5a676132" |
|
|
1200 |
}, |
|
|
1201 |
"outputs": [ |
|
|
1202 |
{ |
|
|
1203 |
"data": { |
|
|
1204 |
"text/plain": [ |
|
|
1205 |
"48146000" |
|
|
1206 |
] |
|
|
1207 |
}, |
|
|
1208 |
"execution_count": 98, |
|
|
1209 |
"metadata": {}, |
|
|
1210 |
"output_type": "execute_result" |
|
|
1211 |
} |
|
|
1212 |
], |
|
|
1213 |
"source": [ |
|
|
1214 |
"p[0][0]" |
|
|
1215 |
] |
|
|
1216 |
}, |
|
|
1217 |
{ |
|
|
1218 |
"cell_type": "code", |
|
|
1219 |
"execution_count": 99, |
|
|
1220 |
"id": "424b2281", |
|
|
1221 |
"metadata": { |
|
|
1222 |
"id": "424b2281" |
|
|
1223 |
}, |
|
|
1224 |
"outputs": [ |
|
|
1225 |
{ |
|
|
1226 |
"data": { |
|
|
1227 |
"text/plain": [ |
|
|
1228 |
"False" |
|
|
1229 |
] |
|
|
1230 |
}, |
|
|
1231 |
"execution_count": 99, |
|
|
1232 |
"metadata": {}, |
|
|
1233 |
"output_type": "execute_result" |
|
|
1234 |
} |
|
|
1235 |
], |
|
|
1236 |
"source": [ |
|
|
1237 |
"p[0][0] in t[0]" |
|
|
1238 |
] |
|
|
1239 |
}, |
|
|
1240 |
{ |
|
|
1241 |
"cell_type": "code", |
|
|
1242 |
"execution_count": 100, |
|
|
1243 |
"id": "ae535967", |
|
|
1244 |
"metadata": { |
|
|
1245 |
"id": "ae535967" |
|
|
1246 |
}, |
|
|
1247 |
"outputs": [ |
|
|
1248 |
{ |
|
|
1249 |
"name": "stdout", |
|
|
1250 |
"output_type": "stream", |
|
|
1251 |
"text": [ |
|
|
1252 |
"precision 0.293984962406015\n", |
|
|
1253 |
"recall 0.2872887582659809\n", |
|
|
1254 |
"f1 0.290598231001568\n" |
|
|
1255 |
] |
|
|
1256 |
} |
|
|
1257 |
], |
|
|
1258 |
"source": [ |
|
|
1259 |
"pre = 0\n", |
|
|
1260 |
"for i in range(len(p)):\n", |
|
|
1261 |
" if p[i][0] in t[i]:\n", |
|
|
1262 |
" pre+=1\n", |
|
|
1263 |
"\n", |
|
|
1264 |
"pre /= len(p)\n", |
|
|
1265 |
"print('precision', pre)\n", |
|
|
1266 |
"\n", |
|
|
1267 |
"\n", |
|
|
1268 |
"rec = 0\n", |
|
|
1269 |
"for i in range(len(t)):\n", |
|
|
1270 |
" if len(t[i])==1:\n", |
|
|
1271 |
" if t[i][0] in p[i]:\n", |
|
|
1272 |
" rec+=1\n", |
|
|
1273 |
" elif len(t[i])>1:\n", |
|
|
1274 |
" for j in range(len(t[i])):\n", |
|
|
1275 |
" if t[i][j] in p[i]:\n", |
|
|
1276 |
" rec+=1\n", |
|
|
1277 |
"\n", |
|
|
1278 |
"rec /= sum(len(i) for i in t)\n", |
|
|
1279 |
"print('recall', rec) \n", |
|
|
1280 |
"\n", |
|
|
1281 |
"\n", |
|
|
1282 |
"f1 = 2*pre*rec/(pre+rec+np.finfo(np.float32).eps)\n", |
|
|
1283 |
"print('f1', f1)" |
|
|
1284 |
] |
|
|
1285 |
}, |
|
|
1286 |
{ |
|
|
1287 |
"cell_type": "code", |
|
|
1288 |
"execution_count": null, |
|
|
1289 |
"id": "5e963520", |
|
|
1290 |
"metadata": {}, |
|
|
1291 |
"outputs": [], |
|
|
1292 |
"source": [] |
|
|
1293 |
} |
|
|
1294 |
], |
|
|
1295 |
"metadata": { |
|
|
1296 |
"accelerator": "GPU", |
|
|
1297 |
"colab": { |
|
|
1298 |
"collapsed_sections": [], |
|
|
1299 |
"machine_shape": "hm", |
|
|
1300 |
"provenance": [ |
|
|
1301 |
{ |
|
|
1302 |
"file_id": "14SK4V1zyuaUOFhPIxRTzTnuMj0WTBpqR", |
|
|
1303 |
"timestamp": 1668422445307 |
|
|
1304 |
} |
|
|
1305 |
] |
|
|
1306 |
}, |
|
|
1307 |
"gpuClass": "premium", |
|
|
1308 |
"kernelspec": { |
|
|
1309 |
"display_name": "Python 3 (ipykernel)", |
|
|
1310 |
"language": "python", |
|
|
1311 |
"name": "python3" |
|
|
1312 |
}, |
|
|
1313 |
"language_info": { |
|
|
1314 |
"codemirror_mode": { |
|
|
1315 |
"name": "ipython", |
|
|
1316 |
"version": 3 |
|
|
1317 |
}, |
|
|
1318 |
"file_extension": ".py", |
|
|
1319 |
"mimetype": "text/x-python", |
|
|
1320 |
"name": "python", |
|
|
1321 |
"nbconvert_exporter": "python", |
|
|
1322 |
"pygments_lexer": "ipython3", |
|
|
1323 |
"version": "3.9.12" |
|
|
1324 |
} |
|
|
1325 |
}, |
|
|
1326 |
"nbformat": 4, |
|
|
1327 |
"nbformat_minor": 5 |
|
|
1328 |
} |