File size: 15,721 Bytes
9839ba6 49e56e4 9839ba6 49e56e4 9839ba6 c4956a2 9839ba6 c4956a2 9839ba6 c4956a2 9839ba6 c4956a2 9839ba6 c4956a2 9839ba6 c4956a2 9839ba6 c4956a2 9839ba6 c4956a2 9839ba6 c4956a2 9839ba6 c4956a2 9839ba6 c4956a2 9839ba6 c4956a2 9839ba6 c4956a2 9839ba6 c4956a2 9839ba6 c4956a2 9839ba6 c4956a2 9839ba6 c4956a2 9839ba6 c4956a2 9839ba6 c4956a2 9839ba6 c4956a2 9839ba6 c4956a2 9839ba6 c4956a2 9839ba6 c4956a2 9839ba6 c4956a2 9839ba6 c4956a2 9839ba6 c4956a2 9839ba6 |
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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "33df4373-a37b-4fd0-bc67-c297812871e4",
"metadata": {},
"outputs": [],
"source": [
"# Use the trained astroBERT model with the fill-mask pipeline"
]
},
{
"cell_type": "markdown",
"id": "164ee9bd-27f9-40a4-8461-3ce12fc928b0",
"metadata": {},
"source": [
"# Tutorial 1: using astroBERT with the fill-mask pipeline"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "59429414-f07e-45e5-8825-6fc6a8d26653",
"metadata": {},
"outputs": [],
"source": [
"# 1 - load models and tokenizer"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "db8ee724-6a2a-4ea5-820e-5e2aa0a0f622",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2022-10-31 11:29:29.185816: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcudart.so.11.0\n"
]
}
],
"source": [
"from transformers import AutoTokenizer, BertForMaskedLM"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "9a98fb63-0793-4684-a202-931cad17c7ca",
"metadata": {},
"outputs": [],
"source": [
"# the model path can either be the name of the Huggingface repository\n",
"remote_model_path = 'adsabs/astroBERT'\n",
"# or the local path to the directory containing model weight and tokenizer vocab\n",
"local_model_path = '../'"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "25fedd16-283b-4817-9b19-2a5ff1c5ba88",
"metadata": {},
"outputs": [],
"source": [
"# make sure you load the tokenier with do_lower_case=False\n",
"astroBERT_tokenizer = AutoTokenizer.from_pretrained(pretrained_model_name_or_path=remote_model_path,\n",
" add_special_tokens=False,\n",
" do_lower_case=False,\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "fb10db03-a5f0-44f7-8d41-0285f898a90d",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Some weights of the model checkpoint at adsabs/astroBERT were not used when initializing BertForMaskedLM: ['cls.seq_relationship.bias', 'cls.seq_relationship.weight']\n",
"- This IS expected if you are initializing BertForMaskedLM 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",
"- This IS NOT expected if you are initializing BertForMaskedLM from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).\n"
]
}
],
"source": [
"astroBERT_automodel_for_mlm = BertForMaskedLM.from_pretrained(pretrained_model_name_or_path=remote_model_path,\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "e8b9b073-3876-4d0b-b8b2-e46fa25c76f0",
"metadata": {},
"outputs": [],
"source": [
"# for pipeline to work you have to ensure that the model returns a dict\n",
"astroBERT_automodel_for_mlm.config.return_dict=True"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "94338f6f-3467-4696-bf7d-f41a12eb889d",
"metadata": {},
"outputs": [],
"source": [
"from transformers import FillMaskPipeline"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "7b980d9f-4d86-4b54-9324-d57dd9b4b64f",
"metadata": {},
"outputs": [],
"source": [
"astroBERT_pipeline = FillMaskPipeline(model=astroBERT_automodel_for_mlm,\n",
" tokenizer=astroBERT_tokenizer,\n",
" task='fill-mask',\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "5cb4d27b-ee3c-4ac7-ace2-4cc57ea9ce7a",
"metadata": {},
"outputs": [],
"source": [
"clean_sentences = ['M67 is one of the most studied open clusters .',\n",
"'A solar twin is a star with atmospheric parameters and chemical composition very similar to our Sun .',\n",
"'The dynamical evolution of planets close to their star is affected by tidal effects .',\n",
"'The Kepler satellite collected high-precision long-term and continuous light curves for more than 100,000 solar-type stars .',\n",
"'The Local Group is composed of the Milky Way, the Andromeda Galaxy, and numerous smaller satellite galaxies .',\n",
"'Cepheid variables are used to determine the distances to galaxies in the local universe .',\n",
"'Jets are created and sustained by accretion of matter onto a compact massive object .',\n",
"'A single star of one solar mass will evolve into a white dwarf .',\n",
"'The Very Large Array observes the sky at radio wavelengths .',\n",
"'Elements heavier than iron are generated in supernovae explosions .',\n",
"'Spitzer was the first spacecraft to fly in an Earth-trailing orbit .',\n",
"'Galaxy mergers can occur when two (or more) galaxies collide .',\n",
"'Dark matter is a hypothetical form of matter thought to account for approximately 85% of the matter in the universe .',\n",
"'The Local Group of galaxies is pulled toward The Great Attractor .',\n",
"'The Moon is the only satellite of the Earth .',\n",
"'Galaxies are categorized according to their visual morphology as elliptical, spiral, or irregular .',\n",
"'Stars are made mostly of hydrogen .',\n",
"'Comet tails are created as comets approach the Sun .',\n",
"'Pluto is a dwarf planet in the Kuiper Belt .',\n",
"'The Milky Way has a supermassive black hole, Sagittarius A*, at its center .',\n",
"'Andromeda is the nearest large galaxy to the Milky Way and is roughly its equal in mass .',\n",
"'The interstellar medium is the gas and dust between stars .',\n",
"'The cosmic microwave background (CMB, CMBR), in Big Bang cosmology, is electromagnetic radiation which is a remnant from an early stage of the universe .',\n",
"'The Large and Small Magellanic Clouds are irregular dwarf galaxies and are two satellite galaxies of the Milky Way .']"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "9f3a6fdc-182f-4edb-8ef4-7e4253c2d4db",
"metadata": {},
"outputs": [],
"source": [
"masked_sentences = ['M67 is one of the most studied [MASK] clusters .',\n",
"'A solar twin is a star with [MASK] parameters and chemical composition very similar to our Sun .',\n",
"'The dynamical evolution of planets close to their star is affected by [MASK] effects .',\n",
"'The Kepler satellite collected high-precision long-term and continuous light [MASK] for more than 100,000 solar-type stars .',\n",
"'The Local Group is composed of the Milky Way, the [MASK] Galaxy, and numerous smaller satellite galaxies .',\n",
"'Cepheid variables are used to determine the [MASK] to galaxies in the local universe .',\n",
"'Jets are created and sustained by [MASK] of matter onto a compact massive object .',\n",
"'A single star of one solar mass will evolve into a [MASK] dwarf .',\n",
"'The Very Large Array observes the sky at [MASK] wavelengths .',\n",
"'Elements heavier than [MASK] are generated in supernovae explosions .',\n",
"'Spitzer was the first [MASK] to fly in an Earth-trailing orbit .',\n",
"'Galaxy [MASK] can occur when two (or more) galaxies collide .',\n",
"'Dark [MASK] is a hypothetical form of matter thought to account for approximately 85% of the matter in the universe .',\n",
"'The Local Group of galaxies is pulled toward The Great [MASK] .',\n",
"'The Moon is the only [MASK] of the Earth .',\n",
"'Galaxies are categorized according to their visual morphology as [MASK] , spiral, or irregular .',\n",
"'Stars are made mostly of [MASK] .',\n",
"'Comet tails are created as comets approach the [MASK] .',\n",
"'Pluto is a dwarf [MASK] in the Kuiper Belt .',\n",
"'The Milky Way has a [MASK] black hole, Sagittarius A*, at its center .',\n",
"'Andromeda is the nearest large [MASK] to the Milky Way and is roughly its equal in mass .',\n",
"'The [MASK] medium is the gas and dust between stars .',\n",
"'The cosmic microwave background (CMB, CMBR), in Big Bang cosmology, is electromagnetic radiation which is a remnant from an early stage of the [MASK] .',\n",
"'The Large and Small Magellanic Clouds are irregular [MASK] galaxies and are two satellite galaxies of the Milky Way .',\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "d4c729ad-89f4-4e70-b433-a65b6035c10b",
"metadata": {},
"outputs": [],
"source": [
"masked_words = [x for s1,s2 in zip(clean_sentences, masked_sentences) \n",
" for x,y in zip(s1.split(), s2.split()) if y=='[MASK]']"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "2a07a641-61a7-42dd-b70e-62eb97ad4e4b",
"metadata": {},
"outputs": [],
"source": [
"results = astroBERT_pipeline(inputs=masked_sentences, \n",
" top_k=3\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "ec2880d9-a8ad-4919-ab5b-732f3bcc21ae",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"M67 is one of the most studied [MASK] clusters .\n",
"original: open\n",
"\t open 0.87\n",
"\t globular 0.07\n",
"\t star 0.03\n",
"\n",
"A solar twin is a star with [MASK] parameters and chemical composition very similar to our Sun .\n",
"original: atmospheric\n",
"\t fundamental 0.56\n",
"\t physical 0.25\n",
"\t stellar 0.05\n",
"\n",
"The dynamical evolution of planets close to their star is affected by [MASK] effects .\n",
"original: tidal\n",
"\t tidal 0.38\n",
"\t secular 0.09\n",
"\t relativistic 0.05\n",
"\n",
"The Kepler satellite collected high-precision long-term and continuous light [MASK] for more than 100,000 solar-type stars .\n",
"original: curves\n",
"\t curves 0.99\n",
"\t curve 0.00\n",
"\t variations 0.00\n",
"\n",
"The Local Group is composed of the Milky Way, the [MASK] Galaxy, and numerous smaller satellite galaxies .\n",
"original: Andromeda\n",
"\t Andromeda 0.99\n",
"\t M31 0.00\n",
"\t Sagittarius 0.00\n",
"\n",
"Cepheid variables are used to determine the [MASK] to galaxies in the local universe .\n",
"original: distances\n",
"\t distances 0.79\n",
"\t distance 0.21\n",
"\t redshifts 0.00\n",
"\n",
"Jets are created and sustained by [MASK] of matter onto a compact massive object .\n",
"original: accretion\n",
"\t accretion 0.79\n",
"\t infall 0.13\n",
"\t fall 0.02\n",
"\n",
"A single star of one solar mass will evolve into a [MASK] dwarf .\n",
"original: white\n",
"\t white 0.77\n",
"\t brown 0.19\n",
"\t red 0.02\n",
"\n",
"The Very Large Array observes the sky at [MASK] wavelengths .\n",
"original: radio\n",
"\t radio 0.29\n",
"\t centimeter 0.10\n",
"\t all 0.09\n",
"\n",
"Elements heavier than [MASK] are generated in supernovae explosions .\n",
"original: iron\n",
"\t iron 0.34\n",
"\t helium 0.16\n",
"\t oxygen 0.07\n",
"\n",
"Spitzer was the first [MASK] to fly in an Earth-trailing orbit .\n",
"original: spacecraft\n",
"\t satellite 0.42\n",
"\t spacecraft 0.20\n",
"\t observatory 0.16\n",
"\n",
"Galaxy [MASK] can occur when two (or more) galaxies collide .\n",
"original: mergers\n",
"\t collisions 0.25\n",
"\t collision 0.22\n",
"\t merging 0.21\n",
"\n",
"Dark [MASK] is a hypothetical form of matter thought to account for approximately 85% of the matter in the universe .\n",
"original: matter\n",
"\t energy 0.64\n",
"\t Energy 0.24\n",
"\t matter 0.10\n",
"\n",
"The Local Group of galaxies is pulled toward The Great [MASK] .\n",
"original: Attractor\n",
"\t Wall 0.96\n",
"\t East 0.01\n",
"\t Planet 0.00\n",
"\n",
"The Moon is the only [MASK] of the Earth .\n",
"original: satellite\n",
"\t satellite 0.38\n",
"\t moon 0.31\n",
"\t constituent 0.07\n",
"\n",
"Galaxies are categorized according to their visual morphology as [MASK] , spiral, or irregular .\n",
"original: elliptical,\n",
"\t elliptical 0.92\n",
"\t spheroidal 0.02\n",
"\t irregular 0.01\n",
"\n",
"Stars are made mostly of [MASK] .\n",
"original: hydrogen\n",
"\t hydrogen 0.20\n",
"\t helium 0.14\n",
"\t carbon 0.12\n",
"\n",
"Comet tails are created as comets approach the [MASK] .\n",
"original: Sun\n",
"\t Sun 0.45\n",
"\t sun 0.23\n",
"\t Earth 0.19\n",
"\n",
"Pluto is a dwarf [MASK] in the Kuiper Belt .\n",
"original: planet\n",
"\t planet 0.96\n",
"\t satellite 0.02\n",
"\t nova 0.00\n",
"\n",
"The Milky Way has a [MASK] black hole, Sagittarius A*, at its center .\n",
"original: supermassive\n",
"\t supermassive 0.80\n",
"\t massive 0.17\n",
"\t stellar 0.00\n",
"\n",
"Andromeda is the nearest large [MASK] to the Milky Way and is roughly its equal in mass .\n",
"original: galaxy\n",
"\t galaxy 0.68\n",
"\t spiral 0.12\n",
"\t satellite 0.09\n",
"\n",
"The [MASK] medium is the gas and dust between stars .\n",
"original: interstellar\n",
"\t interstellar 0.87\n",
"\t interplanetary 0.05\n",
"\t intracluster 0.03\n",
"\n",
"The cosmic microwave background (CMB, CMBR), in Big Bang cosmology, is electromagnetic radiation which is a remnant from an early stage of the [MASK] .\n",
"original: universe\n",
"\t universe 0.45\n",
"\t Universe 0.26\n",
"\t expansion 0.09\n",
"\n",
"The Large and Small Magellanic Clouds are irregular [MASK] galaxies and are two satellite galaxies of the Milky Way .\n",
"original: dwarf\n",
"\t dwarf 0.68\n",
"\t satellite 0.13\n",
"\t Magellanic 0.08\n",
"\n"
]
}
],
"source": [
"for w, s, rs in zip(masked_words, masked_sentences, results):\n",
" print(s)\n",
" print('original: {}'.format(w))\n",
" for r in rs:\n",
" print('\\t {} {:0.2f}'.format(r['token_str'], r['score']))\n",
" print()"
]
}
],
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|