fgrezes commited on
Commit
9839ba6
·
1 Parent(s): 99bf156

added tutorials

Browse files
Tutorials/0_Embeddings.ipynb ADDED
@@ -0,0 +1,257 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "id": "274e6135-2d97-4244-9183-65bcb1d24c80",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "# Use the trained astroBERT model to generate embedings of text\n",
11
+ "# to be used for downstream tasks"
12
+ ]
13
+ },
14
+ {
15
+ "cell_type": "markdown",
16
+ "id": "2cc88ed3-6f52-49a2-99c0-344387758ab5",
17
+ "metadata": {},
18
+ "source": [
19
+ "# Tutorial 0: Loading astroBERT to produce text embeddings\n",
20
+ "This tutorial will show you how to load astroBERT and produce text embeddings that can be used on downstream tasks."
21
+ ]
22
+ },
23
+ {
24
+ "cell_type": "code",
25
+ "execution_count": 2,
26
+ "id": "9e65c041-9d66-4fb1-96b9-4937000da02e",
27
+ "metadata": {},
28
+ "outputs": [],
29
+ "source": [
30
+ "# 1 - load models and tokenizer"
31
+ ]
32
+ },
33
+ {
34
+ "cell_type": "code",
35
+ "execution_count": 3,
36
+ "id": "67d99e96-c532-49ef-8542-a48eef818956",
37
+ "metadata": {},
38
+ "outputs": [
39
+ {
40
+ "name": "stderr",
41
+ "output_type": "stream",
42
+ "text": [
43
+ "2022-10-17 12:10:19.355203: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcudart.so.11.0\n"
44
+ ]
45
+ }
46
+ ],
47
+ "source": [
48
+ "from transformers import AutoTokenizer, AutoModel"
49
+ ]
50
+ },
51
+ {
52
+ "cell_type": "code",
53
+ "execution_count": 4,
54
+ "id": "00e1d48e-9898-44ef-b00e-43e3ab7fed7d",
55
+ "metadata": {},
56
+ "outputs": [],
57
+ "source": [
58
+ "# the model path can either be the name of the Huggingface repository\n",
59
+ "remote_model_path = 'adsabs/astroBERT'\n",
60
+ "# or the local path to the directory containing model weight and tokenizer vocab\n",
61
+ "local_model_path = '../'"
62
+ ]
63
+ },
64
+ {
65
+ "cell_type": "code",
66
+ "execution_count": 5,
67
+ "id": "9bcc6009-6009-463f-a7da-f010c5fae27e",
68
+ "metadata": {},
69
+ "outputs": [],
70
+ "source": [
71
+ "# make sure you load the tokenier with do_lower_case=False\n",
72
+ "astroBERT_tokenizer = AutoTokenizer.from_pretrained(pretrained_model_name_or_path=remote_model_path,\n",
73
+ " use_auth_token=True,\n",
74
+ " add_special_tokens=True,\n",
75
+ " do_lower_case=False,\n",
76
+ " )"
77
+ ]
78
+ },
79
+ {
80
+ "cell_type": "code",
81
+ "execution_count": 6,
82
+ "id": "dbd144f0-6038-4917-94b0-aea9da72cac5",
83
+ "metadata": {},
84
+ "outputs": [
85
+ {
86
+ "data": {
87
+ "text/plain": [
88
+ "PreTrainedTokenizerFast(name_or_path='adsabs/astroBERT', vocab_size=30000, model_max_len=1000000000000000019884624838656, is_fast=True, padding_side='right', truncation_side='right', special_tokens={'unk_token': '[UNK]', 'sep_token': '[SEP]', 'pad_token': '[PAD]', 'cls_token': '[CLS]', 'mask_token': '[MASK]'})"
89
+ ]
90
+ },
91
+ "execution_count": 6,
92
+ "metadata": {},
93
+ "output_type": "execute_result"
94
+ }
95
+ ],
96
+ "source": [
97
+ "astroBERT_tokenizer"
98
+ ]
99
+ },
100
+ {
101
+ "cell_type": "code",
102
+ "execution_count": 7,
103
+ "id": "dd9a9257-cbe4-4908-a9f4-8e1431dc375a",
104
+ "metadata": {},
105
+ "outputs": [
106
+ {
107
+ "name": "stderr",
108
+ "output_type": "stream",
109
+ "text": [
110
+ "Some weights of the model checkpoint at adsabs/astroBERT were not used when initializing BertModel: ['cls.predictions.bias', 'cls.predictions.transform.dense.weight', 'cls.predictions.decoder.bias', 'cls.predictions.decoder.weight', 'cls.predictions.transform.LayerNorm.weight', 'cls.seq_relationship.bias', 'cls.predictions.transform.dense.bias', 'cls.predictions.transform.LayerNorm.bias', 'cls.seq_relationship.weight']\n",
111
+ "- 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",
112
+ "- 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"
113
+ ]
114
+ }
115
+ ],
116
+ "source": [
117
+ "# automodels: defaults to BertModel\n",
118
+ "# it's normal to get warnings as a BertModel will not load the weights used for PreTraining\n",
119
+ "astroBERT_automodel = AutoModel.from_pretrained(remote_model_path, \n",
120
+ " use_auth_token=True,\n",
121
+ " )"
122
+ ]
123
+ },
124
+ {
125
+ "cell_type": "code",
126
+ "execution_count": 8,
127
+ "id": "572ddd38-a0dc-4583-a5a6-c4f3b2cb2553",
128
+ "metadata": {},
129
+ "outputs": [],
130
+ "source": [
131
+ "# 2 - make some inference, the outputs are the embeddings"
132
+ ]
133
+ },
134
+ {
135
+ "cell_type": "code",
136
+ "execution_count": 9,
137
+ "id": "32fc0b97-4a2d-42ab-aa83-f5d8b39672b1",
138
+ "metadata": {},
139
+ "outputs": [
140
+ {
141
+ "name": "stdout",
142
+ "output_type": "stream",
143
+ "text": [
144
+ "torch.Size([3, 54])\n"
145
+ ]
146
+ }
147
+ ],
148
+ "source": [
149
+ "# list of strings for which we want embeddings\n",
150
+ "strings = ['The Chandra X-ray Observatory (CXO), previously known as the Advanced X-ray Astrophysics Facility (AXAF), is a Flagship-class space telescope launched aboard the Space Shuttle Columbia during STS-93 by NASA on July 23, 1999.',\n",
151
+ " 'Independent lines of evidence from Type Ia supernovae and the CMB imply that the universe today is dominated by a mysterious form of energy known as dark energy, which appears to homogeneously permeate all of space.',\n",
152
+ " 'This work has been developed in the framework of the ‘Darklight’ programme, supported by the European Research Council through an Advanced Research Grant to LG (Project # 291521).'\n",
153
+ " ]\n",
154
+ "\n",
155
+ "# tokenizer the strings, with padding (needed to process multiple strings efficiently)\n",
156
+ "inputs = astroBERT_tokenizer(strings, \n",
157
+ " padding=True, \n",
158
+ " return_tensors='pt'\n",
159
+ " )\n",
160
+ "\n",
161
+ "# check the shape of the inputs\n",
162
+ "print(inputs['input_ids'].shape)"
163
+ ]
164
+ },
165
+ {
166
+ "cell_type": "code",
167
+ "execution_count": 10,
168
+ "id": "8b7c9456-573a-48e7-9bc2-839fcc25631d",
169
+ "metadata": {},
170
+ "outputs": [],
171
+ "source": [
172
+ "# pass the inputs through astroBERT\n",
173
+ "import torch\n",
174
+ "# no need for gradients, since we are only doing inference\n",
175
+ "with torch.no_grad():\n",
176
+ " output = astroBERT_automodel(**inputs, \n",
177
+ " output_hidden_states=False\n",
178
+ " ) "
179
+ ]
180
+ },
181
+ {
182
+ "cell_type": "code",
183
+ "execution_count": 11,
184
+ "id": "116de57a-bb31-48d7-9556-64e01a16d56f",
185
+ "metadata": {},
186
+ "outputs": [
187
+ {
188
+ "name": "stdout",
189
+ "output_type": "stream",
190
+ "text": [
191
+ "torch.Size([3, 54, 768])\n"
192
+ ]
193
+ }
194
+ ],
195
+ "source": [
196
+ "# BertModel outputs two tensors: last_hidden_state (our embeddings) and pooler_output (to be discarded as it's not meaningful)\n",
197
+ "# see https://huggingface.co/docs/transformers/model_doc/bert#transformers.BertModel.forward\n",
198
+ "# embeddings will have shape = (# of strings, size of tokenized strings(padded), 768 (BERT embedding size))\n",
199
+ "embeddings = output[0]\n",
200
+ "print(embeddings.shape)"
201
+ ]
202
+ },
203
+ {
204
+ "cell_type": "code",
205
+ "execution_count": 12,
206
+ "id": "38e45291-6fd7-48cf-83df-e1cc5c8a699f",
207
+ "metadata": {},
208
+ "outputs": [
209
+ {
210
+ "name": "stdout",
211
+ "output_type": "stream",
212
+ "text": [
213
+ "tensor([[ 0.5546, 0.9121, 0.6550, ..., -0.1925, 0.7077, -0.2405],\n",
214
+ " [ 0.6252, 0.3175, 1.0899, ..., 0.0576, 0.0529, 0.0603],\n",
215
+ " [ 0.1803, -0.4567, 1.2688, ..., 0.6026, -0.5718, -0.2060],\n",
216
+ " ...,\n",
217
+ " [-0.4397, -0.5334, 1.1682, ..., 0.9541, 0.4046, -0.4756],\n",
218
+ " [-0.3911, 0.7793, 0.2432, ..., 0.2268, -1.0489, -1.4864],\n",
219
+ " [-0.4529, -0.7346, 0.0675, ..., -0.3246, -0.2333, -0.6154]])\n"
220
+ ]
221
+ }
222
+ ],
223
+ "source": [
224
+ "print(embeddings[0])"
225
+ ]
226
+ },
227
+ {
228
+ "cell_type": "code",
229
+ "execution_count": null,
230
+ "id": "26acf89f-b7fc-4872-ac81-0ee65030b465",
231
+ "metadata": {},
232
+ "outputs": [],
233
+ "source": []
234
+ }
235
+ ],
236
+ "metadata": {
237
+ "kernelspec": {
238
+ "display_name": "Python 3 (ipykernel)",
239
+ "language": "python",
240
+ "name": "python3"
241
+ },
242
+ "language_info": {
243
+ "codemirror_mode": {
244
+ "name": "ipython",
245
+ "version": 3
246
+ },
247
+ "file_extension": ".py",
248
+ "mimetype": "text/x-python",
249
+ "name": "python",
250
+ "nbconvert_exporter": "python",
251
+ "pygments_lexer": "ipython3",
252
+ "version": "3.8.5"
253
+ }
254
+ },
255
+ "nbformat": 4,
256
+ "nbformat_minor": 5
257
+ }
Tutorials/1_Fill-Mask.ipynb ADDED
@@ -0,0 +1,427 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "id": "33df4373-a37b-4fd0-bc67-c297812871e4",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "# Use the trained astroBERT model with the fill-mask pipeline"
11
+ ]
12
+ },
13
+ {
14
+ "cell_type": "markdown",
15
+ "id": "164ee9bd-27f9-40a4-8461-3ce12fc928b0",
16
+ "metadata": {},
17
+ "source": [
18
+ "# Tutorial 1: using astroBERT with the fill-mask pipeline"
19
+ ]
20
+ },
21
+ {
22
+ "cell_type": "code",
23
+ "execution_count": 2,
24
+ "id": "59429414-f07e-45e5-8825-6fc6a8d26653",
25
+ "metadata": {},
26
+ "outputs": [],
27
+ "source": [
28
+ "# 1 - load models and tokenizer"
29
+ ]
30
+ },
31
+ {
32
+ "cell_type": "code",
33
+ "execution_count": 3,
34
+ "id": "db8ee724-6a2a-4ea5-820e-5e2aa0a0f622",
35
+ "metadata": {},
36
+ "outputs": [
37
+ {
38
+ "name": "stderr",
39
+ "output_type": "stream",
40
+ "text": [
41
+ "2022-10-14 15:27:35.809315: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcudart.so.11.0\n"
42
+ ]
43
+ }
44
+ ],
45
+ "source": [
46
+ "from transformers import AutoTokenizer, BertForMaskedLM"
47
+ ]
48
+ },
49
+ {
50
+ "cell_type": "code",
51
+ "execution_count": 4,
52
+ "id": "9a98fb63-0793-4684-a202-931cad17c7ca",
53
+ "metadata": {},
54
+ "outputs": [],
55
+ "source": [
56
+ "# the model path can either be the name of the Huggingface repository\n",
57
+ "remote_model_path = 'adsabs/astroBERT'\n",
58
+ "# or the local path to the directory containing model weight and tokenizer vocab\n",
59
+ "local_model_path = '../'"
60
+ ]
61
+ },
62
+ {
63
+ "cell_type": "code",
64
+ "execution_count": 5,
65
+ "id": "25fedd16-283b-4817-9b19-2a5ff1c5ba88",
66
+ "metadata": {},
67
+ "outputs": [],
68
+ "source": [
69
+ "# make sure you load the tokenier with do_lower_case=False\n",
70
+ "astroBERT_tokenizer = AutoTokenizer.from_pretrained(pretrained_model_name_or_path=remote_model_path,\n",
71
+ " use_auth_token=True,\n",
72
+ " add_special_tokens=False,\n",
73
+ " do_lower_case=False,\n",
74
+ " )"
75
+ ]
76
+ },
77
+ {
78
+ "cell_type": "code",
79
+ "execution_count": 6,
80
+ "id": "fb10db03-a5f0-44f7-8d41-0285f898a90d",
81
+ "metadata": {},
82
+ "outputs": [
83
+ {
84
+ "name": "stderr",
85
+ "output_type": "stream",
86
+ "text": [
87
+ "Some weights of the model checkpoint at adsabs/astroBERT were not used when initializing BertForMaskedLM: ['cls.seq_relationship.bias', 'cls.seq_relationship.weight']\n",
88
+ "- 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",
89
+ "- 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"
90
+ ]
91
+ }
92
+ ],
93
+ "source": [
94
+ "astroBERT_automodel_for_mlm = BertForMaskedLM.from_pretrained(pretrained_model_name_or_path=remote_model_path, \n",
95
+ " use_auth_token=True,\n",
96
+ " )"
97
+ ]
98
+ },
99
+ {
100
+ "cell_type": "code",
101
+ "execution_count": 7,
102
+ "id": "e8b9b073-3876-4d0b-b8b2-e46fa25c76f0",
103
+ "metadata": {},
104
+ "outputs": [],
105
+ "source": [
106
+ "# for pipeline to work you have to ensure that the model returns a dict\n",
107
+ "astroBERT_automodel_for_mlm.config.return_dict=True"
108
+ ]
109
+ },
110
+ {
111
+ "cell_type": "code",
112
+ "execution_count": 8,
113
+ "id": "94338f6f-3467-4696-bf7d-f41a12eb889d",
114
+ "metadata": {},
115
+ "outputs": [],
116
+ "source": [
117
+ "from transformers import FillMaskPipeline"
118
+ ]
119
+ },
120
+ {
121
+ "cell_type": "code",
122
+ "execution_count": 9,
123
+ "id": "7b980d9f-4d86-4b54-9324-d57dd9b4b64f",
124
+ "metadata": {},
125
+ "outputs": [],
126
+ "source": [
127
+ "astroBERT_pipeline = FillMaskPipeline(model=astroBERT_automodel_for_mlm,\n",
128
+ " tokenizer=astroBERT_tokenizer,\n",
129
+ " task='fill-mask',\n",
130
+ " )"
131
+ ]
132
+ },
133
+ {
134
+ "cell_type": "code",
135
+ "execution_count": 10,
136
+ "id": "5cb4d27b-ee3c-4ac7-ace2-4cc57ea9ce7a",
137
+ "metadata": {},
138
+ "outputs": [],
139
+ "source": [
140
+ "clean_sentences = ['M67 is one of the most studied open clusters.',\n",
141
+ "'A solar twin is a star with atmospheric parameters and chemical composition very similar to our Sun.',\n",
142
+ "'The dynamical evolution of planets close to their star is affected by tidal effects',\n",
143
+ "'The Kepler satellite collected high-precision long-term and continuous light curves for more than 100,000 solar-type stars',\n",
144
+ "'The Local Group is composed of the Milky Way, the Andromeda Galaxy, and numerous smaller satellite galaxies.',\n",
145
+ "'Cepheid variables are used to determine the distances to galaxies in the local universe.',\n",
146
+ "'Jets are created and sustained by accretion of matter onto a compact massive object.',\n",
147
+ "'A single star of one solar mass will evolve into a white dwarf.',\n",
148
+ "'The Very Large Array observes the sky at radio wavelengths.',\n",
149
+ "'Elements heavier than iron are generated in supernovae explosions.',\n",
150
+ "'Spitzer was the first spacecraft to fly in an Earth-trailing orbit.',\n",
151
+ "'Galaxy mergers can occur when two (or more) galaxies collide',\n",
152
+ "'Dark matter is a hypothetical form of matter thought to account for approximately 85% of the matter in the universe.',\n",
153
+ "'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",
154
+ "'The Local Group of galaxies is pulled toward The Great Attractor.',\n",
155
+ "'The Moon is the only satellite of the Earth.',\n",
156
+ "'Galaxies are categorized according to their visual morphology as elliptical, spiral, or irregular.',\n",
157
+ "'Stars are made mostly of hydrogen.',\n",
158
+ "'Comet tails are created as comets approach the Sun.',\n",
159
+ "'Pluto is a dwarf planet in the Kuiper Belt.',\n",
160
+ "'The Large and Small Magellanic Clouds are irregular dwarf galaxies and are two satellite galaxies of the Milky Way.',\n",
161
+ "'The Milky Way has a supermassive black hole, Sagittarius A*, at its center.',\n",
162
+ "'Andromeda is the nearest large galaxy to the Milky Way and is roughly its equal in mass.',\n",
163
+ "'The interstellar medium is the gas and dust between stars.']"
164
+ ]
165
+ },
166
+ {
167
+ "cell_type": "code",
168
+ "execution_count": 11,
169
+ "id": "9f3a6fdc-182f-4edb-8ef4-7e4253c2d4db",
170
+ "metadata": {},
171
+ "outputs": [],
172
+ "source": [
173
+ "masked_sentences = ['M67 is one of the most studied [MASK] clusters.',\n",
174
+ "'A solar twin is a star with [MASK] parameters and chemical composition very similar to our Sun.',\n",
175
+ "'The dynamical evolution of planets close to their star is affected by [MASK] effects',\n",
176
+ "'The Kepler satellite collected high-precision long-term and continuous light [MASK] for more than 100,000 solar-type stars',\n",
177
+ "'The Local Group is composed of the Milky Way, the [MASK] Galaxy, and numerous smaller satellite galaxies.',\n",
178
+ "'Cepheid variables are used to determine the [MASK] to galaxies in the local universe.',\n",
179
+ "'Jets are created and sustained by [MASK] of matter onto a compact massive object.',\n",
180
+ "'A single star of one solar mass will evolve into a [MASK] dwarf.',\n",
181
+ "'The Very Large Array observes the sky at [MASK] wavelengths.',\n",
182
+ "'Elements heavier than [MASK] are generated in supernovae explosions.',\n",
183
+ "'Spitzer was the first [MASK] to fly in an Earth-trailing orbit.',\n",
184
+ "'Galaxy [MASK] can occur when two (or more) galaxies collide',\n",
185
+ "'Dark [MASK] is a hypothetical form of matter thought to account for approximately 85% of the matter in the universe.',\n",
186
+ "'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",
187
+ "'The Local Group of galaxies is pulled toward The Great [MASK].',\n",
188
+ "'The Moon is the only [MASK] of the Earth.',\n",
189
+ "'Galaxies are categorized according to their visual morphology as [MASK], spiral, or irregular.',\n",
190
+ "'Stars are made mostly of [MASK].',\n",
191
+ "'Comet tails are created as comets approach the [MASK].',\n",
192
+ "'Pluto is a dwarf [MASK] in the Kuiper Belt.',\n",
193
+ "'The Large and Small Magellanic Clouds are irregular [MASK] galaxies and are two satellite galaxies of the Milky Way.',\n",
194
+ "'The Milky Way has a [MASK] black hole, Sagittarius A*, at its center.',\n",
195
+ "'Andromeda is the nearest large [MASK] to the Milky Way and is roughly its equal in mass.',\n",
196
+ "'The [MASK] medium is the gas and dust between stars.']"
197
+ ]
198
+ },
199
+ {
200
+ "cell_type": "code",
201
+ "execution_count": 12,
202
+ "id": "d4c729ad-89f4-4e70-b433-a65b6035c10b",
203
+ "metadata": {},
204
+ "outputs": [],
205
+ "source": [
206
+ "masked_words = [x for s1,s2 in zip(clean_sentences, masked_sentences) \n",
207
+ " for x,y in zip(s1.split(), s2.split()) if y=='[MASK]']"
208
+ ]
209
+ },
210
+ {
211
+ "cell_type": "code",
212
+ "execution_count": 13,
213
+ "id": "2958cfea-f6cf-4d45-9e15-84a5fae7f3cb",
214
+ "metadata": {},
215
+ "outputs": [
216
+ {
217
+ "data": {
218
+ "text/plain": [
219
+ "['open',\n",
220
+ " 'atmospheric',\n",
221
+ " 'tidal',\n",
222
+ " 'curves',\n",
223
+ " 'Andromeda',\n",
224
+ " 'distances',\n",
225
+ " 'accretion',\n",
226
+ " 'white',\n",
227
+ " 'radio',\n",
228
+ " 'iron',\n",
229
+ " 'spacecraft',\n",
230
+ " 'mergers',\n",
231
+ " 'matter',\n",
232
+ " 'satellite',\n",
233
+ " 'planet',\n",
234
+ " 'dwarf',\n",
235
+ " 'supermassive',\n",
236
+ " 'galaxy',\n",
237
+ " 'interstellar']"
238
+ ]
239
+ },
240
+ "execution_count": 13,
241
+ "metadata": {},
242
+ "output_type": "execute_result"
243
+ }
244
+ ],
245
+ "source": [
246
+ "masked_words"
247
+ ]
248
+ },
249
+ {
250
+ "cell_type": "code",
251
+ "execution_count": 14,
252
+ "id": "2a07a641-61a7-42dd-b70e-62eb97ad4e4b",
253
+ "metadata": {},
254
+ "outputs": [],
255
+ "source": [
256
+ "results = astroBERT_pipeline(inputs=masked_sentences, \n",
257
+ " top_k=3\n",
258
+ " )"
259
+ ]
260
+ },
261
+ {
262
+ "cell_type": "code",
263
+ "execution_count": 15,
264
+ "id": "ec2880d9-a8ad-4919-ab5b-732f3bcc21ae",
265
+ "metadata": {},
266
+ "outputs": [
267
+ {
268
+ "name": "stdout",
269
+ "output_type": "stream",
270
+ "text": [
271
+ "M67 is one of the most studied [MASK] clusters.\n",
272
+ "original: open\n",
273
+ "\t open 0.87\n",
274
+ "\t globular 0.07\n",
275
+ "\t star 0.03\n",
276
+ "\n",
277
+ "A solar twin is a star with [MASK] parameters and chemical composition very similar to our Sun.\n",
278
+ "original: atmospheric\n",
279
+ "\t fundamental 0.56\n",
280
+ "\t physical 0.25\n",
281
+ "\t stellar 0.05\n",
282
+ "\n",
283
+ "The dynamical evolution of planets close to their star is affected by [MASK] effects\n",
284
+ "original: tidal\n",
285
+ "\t tidal 0.07\n",
286
+ "\t electromagnetic 0.05\n",
287
+ "\t electrostatic 0.04\n",
288
+ "\n",
289
+ "The Kepler satellite collected high-precision long-term and continuous light [MASK] for more than 100,000 solar-type stars\n",
290
+ "original: curves\n",
291
+ "\t curves 0.43\n",
292
+ "\t ##s 0.04\n",
293
+ "\t conditions 0.04\n",
294
+ "\n",
295
+ "The Local Group is composed of the Milky Way, the [MASK] Galaxy, and numerous smaller satellite galaxies.\n",
296
+ "original: Andromeda\n",
297
+ "\t Andromeda 0.99\n",
298
+ "\t M31 0.00\n",
299
+ "\t Sagittarius 0.00\n",
300
+ "\n",
301
+ "Cepheid variables are used to determine the [MASK] to galaxies in the local universe.\n",
302
+ "original: distances\n",
303
+ "\t distances 0.79\n",
304
+ "\t distance 0.21\n",
305
+ "\t redshifts 0.00\n",
306
+ "\n",
307
+ "Jets are created and sustained by [MASK] of matter onto a compact massive object.\n",
308
+ "original: accretion\n",
309
+ "\t accretion 0.79\n",
310
+ "\t infall 0.13\n",
311
+ "\t fall 0.02\n",
312
+ "\n",
313
+ "A single star of one solar mass will evolve into a [MASK] dwarf.\n",
314
+ "original: white\n",
315
+ "\t white 0.77\n",
316
+ "\t brown 0.19\n",
317
+ "\t red 0.02\n",
318
+ "\n",
319
+ "The Very Large Array observes the sky at [MASK] wavelengths.\n",
320
+ "original: radio\n",
321
+ "\t radio 0.29\n",
322
+ "\t centimeter 0.10\n",
323
+ "\t all 0.09\n",
324
+ "\n",
325
+ "Elements heavier than [MASK] are generated in supernovae explosions.\n",
326
+ "original: iron\n",
327
+ "\t iron 0.34\n",
328
+ "\t helium 0.16\n",
329
+ "\t oxygen 0.07\n",
330
+ "\n",
331
+ "Spitzer was the first [MASK] to fly in an Earth-trailing orbit.\n",
332
+ "original: spacecraft\n",
333
+ "\t satellite 0.42\n",
334
+ "\t spacecraft 0.20\n",
335
+ "\t observatory 0.16\n",
336
+ "\n",
337
+ "Galaxy [MASK] can occur when two (or more) galaxies collide\n",
338
+ "original: mergers\n",
339
+ "\t . 0.26\n",
340
+ "\t A 0.05\n",
341
+ "\t 1 0.04\n",
342
+ "\n",
343
+ "Dark [MASK] is a hypothetical form of matter thought to account for approximately 85% of the matter in the universe.\n",
344
+ "original: matter\n",
345
+ "\t energy 0.64\n",
346
+ "\t Energy 0.24\n",
347
+ "\t matter 0.10\n",
348
+ "\n",
349
+ "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",
350
+ "original: satellite\n",
351
+ "\t universe 0.45\n",
352
+ "\t Universe 0.26\n",
353
+ "\t expansion 0.09\n",
354
+ "\n",
355
+ "The Local Group of galaxies is pulled toward The Great [MASK].\n",
356
+ "original: planet\n",
357
+ "\t Wall 0.96\n",
358
+ "\t East 0.01\n",
359
+ "\t Planet 0.00\n",
360
+ "\n",
361
+ "The Moon is the only [MASK] of the Earth.\n",
362
+ "original: dwarf\n",
363
+ "\t satellite 0.38\n",
364
+ "\t moon 0.31\n",
365
+ "\t constituent 0.07\n",
366
+ "\n",
367
+ "Galaxies are categorized according to their visual morphology as [MASK], spiral, or irregular.\n",
368
+ "original: supermassive\n",
369
+ "\t elliptical 0.92\n",
370
+ "\t spheroidal 0.02\n",
371
+ "\t irregular 0.01\n",
372
+ "\n",
373
+ "Stars are made mostly of [MASK].\n",
374
+ "original: galaxy\n",
375
+ "\t hydrogen 0.20\n",
376
+ "\t helium 0.14\n",
377
+ "\t carbon 0.12\n",
378
+ "\n",
379
+ "Comet tails are created as comets approach the [MASK].\n",
380
+ "original: interstellar\n",
381
+ "\t Sun 0.45\n",
382
+ "\t sun 0.23\n",
383
+ "\t Earth 0.19\n",
384
+ "\n"
385
+ ]
386
+ }
387
+ ],
388
+ "source": [
389
+ "for w, s, rs in zip(masked_words, masked_sentences,results):\n",
390
+ " print(s)\n",
391
+ " print('original: {}'.format(w))\n",
392
+ " for r in rs:\n",
393
+ " print('\\t {} {:0.2f}'.format(r['token_str'], r['score']))\n",
394
+ " print()"
395
+ ]
396
+ },
397
+ {
398
+ "cell_type": "code",
399
+ "execution_count": null,
400
+ "id": "e03b19d6-66cd-4eba-8ddb-6924011037e7",
401
+ "metadata": {},
402
+ "outputs": [],
403
+ "source": []
404
+ }
405
+ ],
406
+ "metadata": {
407
+ "kernelspec": {
408
+ "display_name": "Python 3 (ipykernel)",
409
+ "language": "python",
410
+ "name": "python3"
411
+ },
412
+ "language_info": {
413
+ "codemirror_mode": {
414
+ "name": "ipython",
415
+ "version": 3
416
+ },
417
+ "file_extension": ".py",
418
+ "mimetype": "text/x-python",
419
+ "name": "python",
420
+ "nbconvert_exporter": "python",
421
+ "pygments_lexer": "ipython3",
422
+ "version": "3.8.5"
423
+ }
424
+ },
425
+ "nbformat": 4,
426
+ "nbformat_minor": 5
427
+ }