import%20marimo%0A%0A__generated_with%20%3D%20%220.11.14%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%3Cdiv%20style%3D%22display%3A%20flex%3B%20align-items%3A%20center%3B%22%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cdiv%20style%3D%22flex%3A%201%3B%22%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Cspan%20style%3D%22font-size%3A%201.5em%3B%22%3EBidirectional%20Encoder%20Representations%20from%20Transformers%20(BERT)%3C%2Fspan%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Cp%3ESadamori%20Kojaku%3C%2Fp%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3C%2Fdiv%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cdiv%20style%3D%22flex%3A%201%3B%22%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Cimg%20src%3D%22https%3A%2F%2Fcdn.botpenguin.com%2Fassets%2Fwebsite%2FBERT_c35709b509.webp%22%20width%3D%22400%22%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3C%2Fdiv%3E%0A%20%20%20%20%20%20%20%20%3C%2Fdiv%3E%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%20BERT%3A%20A%20successor%20of%20ELMo%0A%0A%20%20%20%20%20%20%20%20ELMo%20addressed%20polysemy%20with%20**bidirectional%20LSTMs**.%0A%0A%0A%20%20%20%20%20%20%20%20!%5B%5D(https%3A%2F%2Fmiro.medium.com%2Fv2%2Fresize%3Afit%3A1400%2F1*rawUFwyB0hRnkNuKw3c-tA.png)%0A%0A%20%20%20%20%20%20%20%20BERT%20advances%20this%20by%20using%20**transformers**%20and%20a%20**self-attention%20mechanism**%2C%20significantly%20improving%20context%20understanding.%0A%0A%20%20%20%20%20%20%20%20!%5B%5D(https%3A%2F%2Ftrituenhantao.io%2Fwp-content%2Fuploads%2F2019%2F10%2Fbert-finetunning.png)%0A%0A%20%20%20%20%20%20%20%20Due%20to%20its%20powerful%20capabilities%20in%20tasks%20like%20**question%20answering**%20and%20**text%20classification**%2C%20BERT%20has%20become%20foundational%20in%20NLP%2C%20even%20enhancing%20Google's%20search%20engine.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%20Architecture%20of%20BERT%0A%0A%20%20%20%20%20%20%20%20**BERT**%20consists%20of%20stacked%2012%20**encoder%20transformer%20layers**.%20No%20decoder%20is%20used.%0A%0A%20%20%20%20%20%20%20%20Each%20layer%20progressively%20refines%20**token%20embeddings**%2C%20making%20them%20increasingly%20context-aware%20and%20effective%20for%20NLP%20tasks.%0A%0A%20%20%20%20%20%20%20%20!%5B%5D(https%3A%2F%2Fwww.researchgate.net%2Fpublication%2F372906672%2Ffigure%2Ffig2%2FAS%3A11431281179224913%401691164535766%2FBERT-model-architecture.ppm)%0A%0A%20%20%20%20%20%20%20%20%23%20%F0%9F%A7%A0%20Pre-training%20BERT%0A%0A%20%20%20%20%20%20%20%20-%20BERT%20is%20pre-trained%20on%20massive%20text%20datasets%20like%20Wikipedia%20and%20BooksCorpus.%0A%20%20%20%20%20%20%20%20-%20During%20this%20phase%2C%20BERT%20learns%20language%20patterns%2C%20context%2C%20and%20semantic%20relationships%20without%20human%20supervision.%0A%20%20%20%20%20%20%20%20-%20This%20foundational%20knowledge%20enables%20BERT%20to%20generalize%20effectively%20across%20various%20NLP%20tasks.%20%F0%9F%94%84%0A%0A%20%20%20%20%20%20%20%20BERT%20is%20pre-trained%20on%20two%20objectives%3A%0A%0A%20%20%20%20%20%20%20%20-%20**Masked%20Language%20Modeling%20(MLM)**%0A%20%20%20%20%20%20%20%20-%20**Next%20Sentence%20Prediction%20(NSP)**%0A%0A%20%20%20%20%20%20%20%20Let's%20see%20how%20these%20objectives%20work.%0A%0A%20%20%20%20%20%20%20%20%23%23%20Masked%20Language%20Modeling%20(MLM)%0A%0A%0A%20%20%20%20%20%20%20%20%3Cdiv%20style%3D%22display%3A%20flex%3B%20justify-content%3A%20center%3B%22%3E%0A%20%20%20%20%20%20%20%20%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20%20width%3D%22600%22%20viewBox%3D%220%200%20800%20500%22%3E%0A%0A%20%20%20%20%20%20%20%20%20%20%3C!--%20Title%20--%3E%0A%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22400%22%20y%3D%2240%22%20font-family%3D%22Arial%22%20font-size%3D%2222%22%20font-weight%3D%22bold%22%20text-anchor%3D%22middle%22%20fill%3D%22%23333%22%3EBERT%20Masking%20Process%20for%20MLM%20Training%3C%2Ftext%3E%0A%0A%20%20%20%20%20%20%20%20%20%20%3C!--%20Original%20sentence%20--%3E%0A%20%20%20%20%20%20%20%20%20%20%3Crect%20x%3D%2250%22%20y%3D%2280%22%20width%3D%22700%22%20height%3D%2260%22%20rx%3D%225%22%20ry%3D%225%22%20fill%3D%22%23e3f2fd%22%20stroke%3D%22%232196f3%22%20stroke-width%3D%222%22%20%2F%3E%0A%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22400%22%20y%3D%22100%22%20font-family%3D%22Arial%22%20font-size%3D%2218%22%20font-weight%3D%22bold%22%20text-anchor%3D%22middle%22%3EOriginal%20Sentence%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%3Cg%20transform%3D%22translate(80%2C%200)%22%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cg%20font-family%3D%22monospace%22%20font-size%3D%2216%22%20text-anchor%3D%22middle%22%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22100%22%20y%3D%22130%22%20fill%3D%22%23333%22%3E%22the%22%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22170%22%20y%3D%22130%22%20fill%3D%22%23333%22%3E%22cat%22%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22240%22%20y%3D%22130%22%20fill%3D%22%23333%22%3E%22sat%22%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22310%22%20y%3D%22130%22%20fill%3D%22%23333%22%3E%22on%22%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22380%22%20y%3D%22130%22%20fill%3D%22%23333%22%3E%22the%22%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22450%22%20y%3D%22130%22%20fill%3D%22%23333%22%3E%22mat%22%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22520%22%20y%3D%22130%22%20fill%3D%22%23333%22%3E%22.%22%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3C%2Fg%3E%0A%20%20%20%20%20%20%20%20%20%20%3C%2Fg%3E%0A%0A%20%20%20%20%20%20%20%20%20%20%3C!--%20Random%2015%25%20Selection%20--%3E%0A%20%20%20%20%20%20%20%20%20%20%3Crect%20x%3D%2250%22%20y%3D%22160%22%20width%3D%22700%22%20height%3D%2260%22%20rx%3D%225%22%20ry%3D%225%22%20fill%3D%22%23e8f5e9%22%20stroke%3D%22%234caf50%22%20stroke-width%3D%222%22%20%2F%3E%0A%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22400%22%20y%3D%22180%22%20font-family%3D%22Arial%22%20font-size%3D%2218%22%20font-weight%3D%22bold%22%20text-anchor%3D%22middle%22%3ESelect%2015%25%20of%20Tokens%20Randomly%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%3Cg%20transform%3D%22translate(80%2C%200)%22%3E%0A%20%20%20%20%20%20%20%20%20%20%3Cg%20font-family%3D%22monospace%22%20font-size%3D%2216%22%20text-anchor%3D%22middle%22%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22100%22%20y%3D%22210%22%20fill%3D%22%23333%22%3E%22the%22%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22170%22%20y%3D%22210%22%20fill%3D%22%23333%22%3E%22cat%22%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Crect%20x%3D%22225%22%20y%3D%22195%22%20width%3D%2230%22%20height%3D%2220%22%20fill%3D%22%23ffeb3b%22%20rx%3D%223%22%20ry%3D%223%22%20%2F%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22240%22%20y%3D%22210%22%20fill%3D%22%23333%22%3E%22sat%22%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22310%22%20y%3D%22210%22%20fill%3D%22%23333%22%3E%22on%22%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22380%22%20y%3D%22210%22%20fill%3D%22%23333%22%3E%22the%22%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22450%22%20y%3D%22210%22%20fill%3D%22%23333%22%3E%22mat%22%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22520%22%20y%3D%22210%22%20fill%3D%22%23333%22%3E%22.%22%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%3C%2Fg%3E%0A%20%20%20%20%20%20%20%20%20%20%3C%2Fg%3E%0A%0A%20%20%20%20%20%20%20%20%20%20%3C!--%20Three%20masking%20options%20--%3E%0A%20%20%20%20%20%20%20%20%20%20%3Cg%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3C!--%2080%25%20MASK%20--%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Crect%20x%3D%2250%22%20y%3D%22240%22%20width%3D%22220%22%20height%3D%22180%22%20rx%3D%225%22%20ry%3D%225%22%20fill%3D%22%23e0f7fa%22%20stroke%3D%22%2300bcd4%22%20stroke-width%3D%222%22%20%2F%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22160%22%20y%3D%22270%22%20font-family%3D%22Arial%22%20font-size%3D%2216%22%20font-weight%3D%22bold%22%20text-anchor%3D%22middle%22%3E80%25%20of%20time%3A%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22160%22%20y%3D%22290%22%20font-family%3D%22Arial%22%20font-size%3D%2216%22%20text-anchor%3D%22middle%22%3EReplace%20with%20%5BMASK%5D%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cg%20font-family%3D%22monospace%22%20font-size%3D%2214%22%20text-anchor%3D%22middle%22%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%2290%22%20y%3D%22335%22%20fill%3D%22%23333%22%3E...%20'cat'%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22160%22%20y%3D%22335%22%20fill%3D%22%23333%22%20font-weight%3D%22bold%22%3E%5BMASK%5D%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22240%22%20y%3D%22335%22%20fill%3D%22%23333%22%3E'on'%20...%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3C%2Fg%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22160%22%20y%3D%22370%22%20font-family%3D%22Arial%22%20font-size%3D%2213%22%20fill%3D%22%23444%22%20text-anchor%3D%22middle%22%3EPrevents%20model%20from%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22160%22%20y%3D%22385%22%20font-family%3D%22Arial%22%20font-size%3D%2213%22%20fill%3D%22%23444%22%20text-anchor%3D%22middle%22%3Erelying%20solely%20on%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22160%22%20y%3D%22400%22%20font-family%3D%22Arial%22%20font-size%3D%2213%22%20fill%3D%22%23444%22%20text-anchor%3D%22middle%22%3Ethe%20mask%20token%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%3C%2Fg%3E%0A%0A%20%20%20%20%20%20%20%20%20%20%3Cg%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3C!--%2010%25%20Random%20--%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Crect%20x%3D%22290%22%20y%3D%22240%22%20width%3D%22220%22%20height%3D%22180%22%20rx%3D%225%22%20ry%3D%225%22%20fill%3D%22%23fff3e0%22%20stroke%3D%22%23ff9800%22%20stroke-width%3D%222%22%20%2F%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22400%22%20y%3D%22270%22%20font-family%3D%22Arial%22%20font-size%3D%2216%22%20font-weight%3D%22bold%22%20text-anchor%3D%22middle%22%3E10%25%20of%20time%3A%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22400%22%20y%3D%22290%22%20font-family%3D%22Arial%22%20font-size%3D%2216%22%20text-anchor%3D%22middle%22%3EReplace%20with%20random%20word%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cg%20font-family%3D%22monospace%22%20font-size%3D%2214%22%20text-anchor%3D%22middle%22%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22340%22%20y%3D%22335%22%20fill%3D%22%23333%22%3E...%20'cat'%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22400%22%20y%3D%22335%22%20fill%3D%22%23333%22%20font-weight%3D%22bold%22%20fill%3D%22%23d32f2f%22%3E'dog'%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22460%22%20y%3D%22335%22%20fill%3D%22%23333%22%3E'on'%20...%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3C%2Fg%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22400%22%20y%3D%22370%22%20font-family%3D%22Arial%22%20font-size%3D%2213%22%20fill%3D%22%23444%22%20text-anchor%3D%22middle%22%3EAdapts%20model%20to%20handle%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22400%22%20y%3D%22385%22%20font-family%3D%22Arial%22%20font-size%3D%2213%22%20fill%3D%22%23444%22%20text-anchor%3D%22middle%22%3Enoise%20and%20incorrect%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22400%22%20y%3D%22400%22%20font-family%3D%22Arial%22%20font-size%3D%2213%22%20fill%3D%22%23444%22%20text-anchor%3D%22middle%22%3Epredictions%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%3C%2Fg%3E%0A%0A%20%20%20%20%20%20%20%20%20%20%3Cg%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3C!--%2010%25%20Same%20--%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Crect%20x%3D%22530%22%20y%3D%22240%22%20width%3D%22220%22%20height%3D%22180%22%20rx%3D%225%22%20ry%3D%225%22%20fill%3D%22%23f3e5f5%22%20stroke%3D%22%239c27b0%22%20stroke-width%3D%222%22%20%2F%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22640%22%20y%3D%22270%22%20font-family%3D%22Arial%22%20font-size%3D%2216%22%20font-weight%3D%22bold%22%20text-anchor%3D%22middle%22%3E10%25%20of%20time%3A%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22640%22%20y%3D%22290%22%20font-family%3D%22Arial%22%20font-size%3D%2216%22%20text-anchor%3D%22middle%22%3EKeep%20unchanged%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cg%20font-family%3D%22monospace%22%20font-size%3D%2216%22%20text-anchor%3D%22middle%22%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22580%22%20y%3D%22335%22%20fill%3D%22%23333%22%3E...%20'cat'%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22640%22%20y%3D%22335%22%20fill%3D%22%23333%22%20font-weight%3D%22bold%22%3E'sat'%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22700%22%20y%3D%22335%22%20fill%3D%22%23333%22%3E'on'%20...%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3C%2Fg%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22640%22%20y%3D%22370%22%20font-family%3D%22Arial%22%20font-size%3D%2213%22%20fill%3D%22%23444%22%20text-anchor%3D%22middle%22%3EForces%20model%20to%20make%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22640%22%20y%3D%22385%22%20font-family%3D%22Arial%22%20font-size%3D%2213%22%20fill%3D%22%23444%22%20text-anchor%3D%22middle%22%3Epredictions%20for%20all%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22640%22%20y%3D%22400%22%20font-family%3D%22Arial%22%20font-size%3D%2213%22%20fill%3D%22%23444%22%20text-anchor%3D%22middle%22%3Eselected%20positions%3C%2Ftext%3E%0A%20%20%20%20%20%20%20%20%20%20%3C%2Fg%3E%0A%0A%20%20%20%20%20%20%20%20%20%20%3C!--%20Prediction%20task%20--%3E%0A%20%20%20%20%20%20%20%20%20%20%3Crect%20x%3D%22150%22%20y%3D%22440%22%20width%3D%22500%22%20height%3D%2240%22%20rx%3D%225%22%20ry%3D%225%22%20fill%3D%22%23ede7f6%22%20stroke%3D%22%23673ab7%22%20stroke-width%3D%222%22%20%2F%3E%0A%20%20%20%20%20%20%20%20%20%20%3Ctext%20x%3D%22400%22%20y%3D%22465%22%20font-family%3D%22Arial%22%20font-size%3D%2216%22%20font-weight%3D%22bold%22%20text-anchor%3D%22middle%22%3EPrediction%20Task%3A%20Model%20must%20predict%20%22sat%22%20at%20masked%20position%3C%2Ftext%3E%0A%0A%20%20%20%20%20%20%20%20%20%20%3C!--%20Arrows%20--%3E%0A%20%20%20%20%20%20%20%20%20%20%3Cg%20stroke%3D%22%23666%22%20stroke-width%3D%222%22%20fill%3D%22none%22%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cpath%20d%3D%22M400%2C145%20L400%2C160%22%20marker-end%3D%22url(%23arrowhead)%22%20%2F%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cpath%20d%3D%22M160%2C225%20L160%2C240%22%20marker-end%3D%22url(%23arrowhead)%22%20%2F%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cpath%20d%3D%22M400%2C225%20L400%2C240%22%20marker-end%3D%22url(%23arrowhead)%22%20%2F%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cpath%20d%3D%22M640%2C225%20L640%2C240%22%20marker-end%3D%22url(%23arrowhead)%22%20%2F%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cpath%20d%3D%22M160%2C420%20L280%2C440%22%20marker-end%3D%22url(%23arrowhead)%22%20%2F%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cpath%20d%3D%22M400%2C420%20L400%2C440%22%20marker-end%3D%22url(%23arrowhead)%22%20%2F%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cpath%20d%3D%22M640%2C420%20L520%2C440%22%20marker-end%3D%22url(%23arrowhead)%22%20%2F%3E%0A%20%20%20%20%20%20%20%20%20%20%3C%2Fg%3E%0A%0A%20%20%20%20%20%20%20%20%20%20%3C!--%20Arrowhead%20marker%20--%3E%0A%20%20%20%20%20%20%20%20%20%20%3Cdefs%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cmarker%20id%3D%22arrowhead%22%20markerWidth%3D%2210%22%20markerHeight%3D%227%22%20refX%3D%229%22%20refY%3D%223.5%22%20orient%3D%22auto%22%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Cpolygon%20points%3D%220%200%2C%2010%203.5%2C%200%207%22%20fill%3D%22%23666%22%20%2F%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3C%2Fmarker%3E%0A%20%20%20%20%20%20%20%20%20%20%3C%2Fdefs%3E%0A%20%20%20%20%20%20%20%20%3C%2Fsvg%3E%0A%20%20%20%20%20%20%20%20%3C%2Fdiv%3E%0A%0A%20%20%20%20%20%20%20%20-%20In%20MLM%2C%20some%20tokens%20are%20masked%2C%20and%20the%20model%20must%20predict%20the%20masked%20tokens.%0A%20%20%20%20%20%20%20%20-%20Three%20ways%20to%20mask%20the%20tokens%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20Replace%20the%20token%20with%20%60%5BMASK%5D%60%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20Replace%20the%20token%20with%20a%20random%20word%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20Keep%20the%20token%20unchanged%0A%0A%20%20%20%20%20%20%20%20-%20Given%20a%20sentence%2C%20the%20model%20must%20recover%20the%20original%20tokens%20based%20on%20the%20context.%0A%0A%0A%20%20%20%20%20%20%20%20%23%23%20Next%20Sentence%20Prediction%20(NSP)%0A%0A%20%20%20%20%20%20%20%20!%5B%5D(https%3A%2F%2Famitness.com%2Fposts%2Fimages%2Fbert-nsp.png)%0A%0A%20%20%20%20%20%20%20%20-%20NSP%20is%20another%20objective%20for%20predicting%20whether%20two%20sentences%20are%20consecutive%20or%20not.%0A%20%20%20%20%20%20%20%20-%20This%20equips%20BERT%20with%20the%20ability%20to%20understand%20the%20relationships%20between%20sentences.%0A%20%20%20%20%20%20%20%20-%20Two%20special%20tokens%20are%20introduced%20to%20structure%20the%20sentence%20pairs%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20**%5BCLS%5D%20token**%20at%20the%20start%20of%20the%20first%20sentence%20(classification%20token)%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20**%5BSEP%5D%20token**%20at%20the%20end%20of%20the%20sentences%20(separator%20token)%0A%0A%20%20%20%20%20%20%20%20-%20Two%20sentences%20are%20then%20concatenated%20into%20one%20sequence%2C%20for%20example%2C%20as%20follows%3A%0A%0A%20%20%20%20%20%20%20%20%24%24%0A%20%20%20%20%20%20%20%20%5C%5Ctext%7B%60%60%5BCLS%5D%20%7D%5C%5Cunderbrace%7B%5C%5Ctext%7BI%20went%20to%20the%20store%7D%7D_%7B%5C%5Ctext%7BSentence%201%7D%7D%5C%5Ctext%7B%20%5BSEP%5D%20%7D%5C%5Cunderbrace%7B%5C%5Ctext%7BThey%20were%20out%20of%20milk%7D%7D_%7B%5C%5Ctext%7BSentence%202%7D%7D%5C%5Ctext%7B%20%5BSEP%5D%7D%22.%0A%20%20%20%20%20%20%20%20%24%24%0A%0A%20%20%20%20%20%20%20%20-%20The%20text%20is%20then%20fed%20into%20the%20BERT%20model%2C%20producing%20the%20embeddings%20of%20the%20tokens.%0A%20%20%20%20%20%20%20%20-%20The%20embedding%20of%20the%20%5BCLS%5D%20token%20is%20then%20used%20to%20predict%20whether%20the%20two%20sentences%20are%20consecutive%20or%20not.%0A%0A%20%20%20%20%20%20%20%20%3E%20*Note*%3A%20To%20inform%20BERT%20which%20tokens%20belong%20to%20which%20sentence%2C%20BERT%20additionally%20uses%20so-called%20%60segment%20embeddings%60.%20But%20recent%20transformer%20architectures%20(e.g.%2C%20GPT%20models%2C%20RoBERTa%2C%20LLaMA)%20typically%20omit%20explicit%20segment%20embeddings%2C%20opting%20instead%20for%20simplified%20positional%20encodings%20or%20relying%20purely%20on%20positional%20embeddings.%0A%20%20%20%20%20%20%20%20%3E%20%3Ccenter%3E%3Cimg%20src%3D%22https%3A%2F%2Ftowardsdatascience.com%2Fwp-content%2Fuploads%2F2024%2F05%2F1w4r6Hz2IF-Uvo4YHg2-bCw-1.png%22%20width%3D%22500%22%3E%3C%2Fcenter%3E%0A%0A%20%20%20%20%20%20%20%20%23%20Let's%20play%20with%20BERT%20%F0%9F%8E%81%0A%0A%20%20%20%20%20%20%20%20The%20best%20way%20to%20understand%20BERT%20is%20to%20play%20with%20it%20%F0%9F%98%89.%0A%0A%20%20%20%20%20%20%20%20We%20will%20use%20the%20%5Btransformers%5D(https%3A%2F%2Fhuggingface.co%2Fdocs%2Ftransformers%2Findex)%20library%20to%20load%20the%20model%20and%20the%20tokenizer.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Import%20transformers%0A%20%20%20%20from%20transformers%20import%20AutoTokenizer%2C%20AutoModel%0A%0A%20%20%20%20%23%20Load%20the%20tokenizer%20and%20model%0A%20%20%20%20tokenizer%20%3D%20AutoTokenizer.from_pretrained(%0A%20%20%20%20%20%20%20%20%22bert-base-uncased%22%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Load%20the%20model%0A%20%20%20%20model%20%3D%20AutoModel.from_pretrained(%0A%20%20%20%20%20%20%20%20%22bert-base-uncased%22%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Set%20the%20model%20to%20evaluation%20mode%0A%20%20%20%20model%20%3D%20model.eval()%0A%20%20%20%20return%20AutoModel%2C%20AutoTokenizer%2C%20model%2C%20tokenizer%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20-%20%60transformers%60%20library%20provides%20easy%20access%20to%20NLP%20models%0A%20%20%20%20%20%20%20%20-%20%60AutoTokenizer%60%20and%20%60AutoModel%60%20automatically%20load%20the%20right%20tokenizer%20and%20model%0A%20%20%20%20%20%20%20%20-%20%60model.eval()%60%20prepares%20the%20model%20for%20inference%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%23Tokenizer%0A%0A%20%20%20%20%20%20%20%20The%20tokenizer%20breaks%20text%20into%20tokens%20that%20BERT%20can%20understand.%0A%20%20%20%20%20%20%20%20Let's%20see%20what%20the%20tokenizer%20does%20with%20the%20following%20text.%0A%0A%20%20%20%20%20%20%20%20%60%60%60python%0A%20%20%20%20%20%20%20%20%3E%3E%3E%20text%20%3D%20%22Binghamton%20University%22%0A%20%20%20%20%20%20%20%20%60%60%60%0A%0A%20%20%20%20%20%20%20%20We%20will%20then%20break%20the%20text%20into%20the%20tokens%2C%20and%20then%20convert%20the%20tokens%20into%20token%20IDs%20(numbers)%20that%20BERT%20can%20understand.%0A%0A%20%20%20%20%20%20%20%20We%20will%20add%20special%20tokens%20to%20the%20text%20to%20help%20BERT%20understand%20the%20structure%20of%20the%20text.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(tokenizer)%3A%0A%20%20%20%20%23%20The%20text%20to%20tokenize%0A%20%20%20%20text%20%3D%20%22Binghamton%20University%22%0A%0A%20%20%20%20%23%20Tokenize%20the%20text.%20We%20add%20special%20tokens%20to%20the%20text.%0A%20%20%20%20tokens%20%3D%20tokenizer.tokenize(text%2C%20add_special_tokens%3DTrue)%0A%0A%20%20%20%20%23%20Convert%20the%20tokens%20into%20token%20IDs%0A%20%20%20%20token_ids%20%3D%20tokenizer.convert_tokens_to_ids(tokens)%0A%0A%20%20%20%20print(f%22Text%3A%20'%7Btext%7D'%22)%0A%20%20%20%20print(f%22Tokenized%3A%20%7Btokens%7D%22)%0A%20%20%20%20print(f%22Token%20IDs%3A%20%7Btoken_ids%7D%22)%0A%20%20%20%20return%20text%2C%20token_ids%2C%20tokens%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20Embedding%20tokens%0A%20%20%20%20%20%20%20%20Now%20the%20preparation%20is%20done.%20Let%20us%20feed%20the%20token%20IDs%20to%20the%20model%20and%20get%20the%20embeddings%20of%20the%20tokens.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(model%2C%20token_ids)%3A%0A%20%20%20%20import%20torch%0A%0A%20%20%20%20%23%20Convert%20the%20token%20IDs%20(numpy%20array)%20into%20a%20torch%20tensor%0A%20%20%20%20token_ids_tensor%20%3D%20torch.tensor(%5Btoken_ids%5D)%0A%0A%20%20%20%20%23%20Feed%20the%20token%20IDs%20to%20the%20model%0A%20%20%20%20outputs%20%3D%20model(token_ids_tensor%2C%20output_hidden_states%3DTrue)%0A%20%20%20%20return%20outputs%2C%20token_ids_tensor%2C%20torch%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20The%20%60outputs%60%20contains%3A%0A%0A%20%20%20%20%20%20%20%201.%20**Input%20token%20embeddings**%20before%20the%20first%20transformer%20module%0A%20%20%20%20%20%20%20%202.%20**Output%20token%20embeddings**%20after%20each%20transformer%20module%0A%20%20%20%20%20%20%20%203.%20**Attention%20scores**%20of%20the%20tokens%0A%0A%20%20%20%20%20%20%20%20Let's%20retrieve%20the%20token%20embeddings%20(1%20and%202)%20by%0A%0A%20%20%20%20%20%20%20%20%60%60%60python%0A%20%20%20%20%20%20%20%20%3E%3E%3E%20outputs.hidden_states%0A%20%20%20%20%20%20%20%20%60%60%60%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(outputs)%3A%0A%20%20%20%20outputs.hidden_states%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20-%20There%20are%2013%20tensors%20in%20%60outputs.hidden_states%60%20(1%20input%20token%20embedding%20before%20transformers%20%2B%2012%20transformer%20outputs).%0A%20%20%20%20%20%20%20%20-%20%60outputs.hidden_states%5Bi%5D%60%20is%20the%20token%20embeddings%20before%20the%20i-th%20transformer%20module.%0A%20%20%20%20%20%20%20%20-%20Each%20tensor%20has%20the%20shape%20%60(batch_size%2C%20sequence_length%2C%20hidden_size)%60.%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20%60batch_size%60%20is%20the%20number%20of%20sentences%20in%20the%20batch.%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20%60sequence_length%60%20is%20the%20number%20of%20tokens%20in%20the%20sentence.%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20%60hidden_size%60%20is%20the%20size%20of%20the%20hidden%20state%20of%20the%20transformer.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(outputs)%3A%0A%20%20%20%20%23%20Get%20the%20last%20hidden%20state%0A%20%20%20%20last_hidden_state%20%3D%20outputs.hidden_states%5B-1%5D%0A%0A%20%20%20%20%23%20Get%20the%20shape%20of%20the%20last%20hidden%20state%0A%20%20%20%20shape%20%3D%20last_hidden_state.shape%0A%20%20%20%20print(shape)%0A%20%20%20%20return%20last_hidden_state%2C%20shape%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22Each%20token%20embedding%20can%20be%20retrieved%20by%3A%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(last_hidden_state)%3A%0A%20%20%20%20%23%20The%20position%20of%20the%20token%20in%20the%20sentence%0A%20%20%20%20token_position%20%3D%203%0A%0A%20%20%20%20%23%20Get%20the%20embedding%20of%20the%20token%0A%20%20%20%20token_embedding%20%3D%20last_hidden_state%5B0%2C%20token_position%2C%20%3A%5D%0A%0A%20%20%20%20print(token_embedding%5B%3A10%5D)%20%23%20Truncated%20for%20brevity%0A%20%20%20%20return%20token_embedding%2C%20token_position%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20Batch%20processing%0A%0A%20%20%20%20%20%20%20%20-%20It%20is%20often%20the%20case%20that%20we%20have%20multiple%20sentences%20to%20process.%0A%20%20%20%20%20%20%20%20-%20Processing%20them%20one%20by%20one%20is%20not%20efficient%20(overhead%20of%20tokenization%2C%20model%20loading%2C%20etc.)%0A%20%20%20%20%20%20%20%20-%20We%20can%20process%20them%20in%20a%20batch%20and%20get%20the%20embeddings%20for%20all%20sentences%20at%20once.%0A%0A%20%20%20%20%20%20%20%20Sounds%20straightforward!%20But%20there%20is%20a%20challenge%20%F0%9F%A4%94%2C%20i.e.%2C%20sentences%20have%20different%20lengths%2C%20so%20that%20we%20may%20not%20be%20able%20to%20pack%20them%20into%20a%20single%20tensor.%0A%0A%20%20%20%20%20%20%20%20Let%20us%20consider%20the%20following%20two%20text%3A%0A%0A%20%20%20%20%20%20%20%20%60%60%60python%0A%20%20%20%20%20%20%20%20%3E%3E%3E%20text1%20%3D%20%22Binghamton%20University%22%0A%20%20%20%20%20%20%20%20%3E%3E%3E%20text2%20%3D%20%22State%20University%20of%20New%20York%22%0A%20%20%20%20%20%20%20%20%60%60%60%0A%0A%20%20%20%20%20%20%20%20As%20in%20the%20previous%20example%2C%20let's%20tokenize%20the%20sentences%20and%20convert%20them%20into%20token%20IDs.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(tokenizer)%3A%0A%20%20%20%20text1%20%3D%20%22Binghamton%20University%22%0A%20%20%20%20text2%20%3D%20%22State%20University%20of%20New%20York%22%0A%0A%20%20%20%20%23%20Tokenize%20the%20text.%20We%20add%20special%20tokens%20to%20the%20text.%0A%20%20%20%20tokens1%20%3D%20tokenizer.tokenize(text1%2C%20add_special_tokens%3DTrue)%0A%20%20%20%20tokens2%20%3D%20tokenizer.tokenize(text2%2C%20add_special_tokens%3DTrue)%0A%0A%20%20%20%20token_ids1%20%3D%20tokenizer.convert_tokens_to_ids(tokens1)%0A%20%20%20%20token_ids2%20%3D%20tokenizer.convert_tokens_to_ids(tokens2)%0A%0A%20%20%20%20print(f%22Token%20IDs%20of%20text1%3A%20%7Btoken_ids1%7D%22)%0A%20%20%20%20print(f%22Token%20IDs%20of%20text2%3A%20%7Btoken_ids2%7D%22)%0A%20%20%20%20return%20text1%2C%20text2%2C%20token_ids1%2C%20token_ids2%2C%20tokens1%2C%20tokens2%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20To%20feed%20them%20into%20the%20model%20in%20batch%2C%20we%20need%20to%20pack%20them%20into%20a%20single%20token%20id%20tensor%20as%20follows%3A%0A%0A%20%20%20%20%20%20%20%20%60%60%60python%0A%20%20%20%20%20%20%20%20%3E%3E%3E%20token_ids%20%3D%20torch.tensor(%5Btoken_ids1%2C%20token_ids2%5D)%0A%20%20%20%20%20%20%20%20%60%60%60%0A%0A%20%20%20%20%20%20%20%20which%20won't%20work%20because%20the%20sentences%20have%20different%20lengths!%0A%0A%20%20%20%20%20%20%20%20To%20address%20this%20issue%20of%20inconsistent%20sequence%20lengths%2C%20we%20can%20pad%20the%20sentences%20with%20special%20tokens%20(e.g.%2C%20%60%5BPAD%5D%60)%20so%20that%20they%20have%20the%20same%20length.%0A%0A%20%20%20%20%20%20%20%20The%20attention%20for%20the%20padded%20tokens%20should%20be%20masked%20out%20so%20that%20padding%20does%20not%20change%20the%20result.%0A%0A%20%20%20%20%20%20%20%20!%5B%5D(https%3A%2F%2Fmiro.medium.com%2Fv2%2Fresize%3Afit%3A1216%2F1*N6p-Oknq_QXV0vSX5KvwdQ.png)%0A%0A%20%20%20%20%20%20%20%20In%20%60transformers%60%2C%20we%20can%20use%20%60tokenizer%60%20to%20generate%20the%20padded%20tensor%20and%20the%20attention%20mask.%0A%0A%20%20%20%20%20%20%20%20%3E%20*Note*%3A%20We%20will%20use%20%60tokenizer%60%20not%20%60tokenizer.tokenize%60!%20The%20%60tokenizer.tokenize%60%20only%20breaks%20text%20into%20tokens%2C%20while%20%60tokenizer%60%20can%20generate%20all%20the%20input%20data%20needed%20to%20run%20BERT.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(text1%2C%20text2%2C%20tokenizer)%3A%0A%20%20%20%20%23%20Tokenize%20the%20text.%20We%20add%20special%20tokens%20to%20the%20text.%0A%20%20%20%20inputs%20%3D%20tokenizer(%5Btext1%2C%20text2%5D%2C%20add_special_tokens%3DTrue%2C%20padding%3DTrue%2C%20truncation%3DTrue%2C%20return_tensors%3D%22pt%22%2C%20return_attention_mask%20%3D%20True)%0A%0A%20%20%20%20print(inputs)%0A%20%20%20%20return%20(inputs%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20Now%20we%20can%20feed%20the%20padded%20sequences%20and%20the%20attention%20mask%20into%20the%20model.%20Don't%20forget%20to%20set%20%60output_hidden_states%3DTrue%60%20so%20that%20we%20can%20retrieve%20the%20token%20embeddings.%0A%0A%20%20%20%20%20%20%20%20Notice%20that%20the%20first%20dimension%20is%20now%20%602%60%20because%20we%20have%20two%20sentences.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(inputs%2C%20model)%3A%0A%20%20%20%20%23%20Process%20the%20multiple%20sentences%20in%20batch%0A%20%20%20%20outputs_batch%20%3D%20model(**inputs%2C%20output_hidden_states%3DTrue)%0A%0A%20%20%20%20last_hidden_state_batch%20%3D%20outputs_batch.hidden_states%5B-1%5D%0A%0A%20%20%20%20print(f%22Last%20hidden%20state%20batch%20shape%3A%20%7Blast_hidden_state_batch.shape%7D%22)%0A%20%20%20%20return%20last_hidden_state_batch%2C%20outputs_batch%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%20Case%20study%3A%20Polysemy%20resolution%0A%0A%20%20%20%20%20%20%20%20To%20illustrate%20the%20power%20of%20BERT%2C%20let%20us%20consider%20a%20case%20study%20of%20polysemy%20resolution.%0A%0A%20%20%20%20%20%20%20%20Consider%20the%20following%20two%20sentences%3A%0A%0A%20%20%20%20%20%20%20%201.%20%22The%20bank%20in%20the%20city%20is%20closed%22%0A%0A%20%20%20%20%20%20%20%202.%20%22The%20bank%20in%20the%20river%20is%20closed%22%0A%0A%20%20%20%20%20%20%20%20The%20word%20%22bank%22%20has%20two%20meanings%3A%0A%0A%20%20%20%20%20%20%20%20-%20%22bank%22%20as%20in%20%22financial%20institution%22%0A%20%20%20%20%20%20%20%20-%20%22bank%22%20as%20in%20%22side%20of%20a%20river%22%0A%0A%20%20%20%20%20%20%20%20To%20resolve%20the%20polysemy%2C%20we%20can%20use%20BERT%20to%20get%20the%20embeddings%20of%20the%20sentence%20and%20the%20word%20%22bank%22.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20sys%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20from%20tqdm%20import%20tqdm%20as%20tqdm%0A%20%20%20%20from%20sklearn.decomposition%20import%20PCA%0A%20%20%20%20return%20PCA%2C%20np%2C%20pd%2C%20plt%2C%20sys%2C%20tqdm%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22We%20will%20use%20%5BCoarseWSD-20%5D(https%3A%2F%2Fgithub.com%2Fdanlou%2Fbert-disambiguation%2Ftree%2Fmaster%2Fdata%2FCoarseWSD-20).%20The%20dataset%20contains%20sentences%20with%20polysemous%20words%20and%20their%20sense%20labels.%20We%20will%20see%20how%20to%20use%20BERT%20to%20disambiguate%20the%20word%20senses.%20Read%20the%20%5BREADME%5D(https%3A%2F%2Fgithub.com%2Fdanlou%2Fbert-disambiguation%2Fblob%2Fmaster%2Fdata%2FCoarseWSD-20%2FREADME.txt)%20for%20more%20details.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(pd)%3A%0A%20%20%20%20def%20load_data(focal_word%2C%20is_train%2C%20n_samples%3D100)%3A%0A%20%20%20%20%20%20%20%20data_type%20%3D%20%22train%22%20if%20is_train%20else%20%22test%22%0A%20%20%20%20%20%20%20%20data_file%20%3D%20f%22https%3A%2F%2Fraw.githubusercontent.com%2Fdanlou%2Fbert-disambiguation%2Fmaster%2Fdata%2FCoarseWSD-20%2F%7Bfocal_word%7D%2F%7Bdata_type%7D.data.txt%22%0A%20%20%20%20%20%20%20%20label_file%20%3D%20f%22https%3A%2F%2Fraw.githubusercontent.com%2Fdanlou%2Fbert-disambiguation%2Fmaster%2Fdata%2FCoarseWSD-20%2F%7Bfocal_word%7D%2F%7Bdata_type%7D.gold.txt%22%0A%0A%20%20%20%20%20%20%20%20data_table%20%3D%20pd.read_csv(%0A%20%20%20%20%20%20%20%20%20%20%20%20data_file%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20sep%3D%22%5Ct%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20header%3DNone%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20dtype%3D%7B%22word_pos%22%3A%20int%2C%20%22sentence%22%3A%20str%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20names%3D%5B%22word_pos%22%2C%20%22sentence%22%5D%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20label_table%20%3D%20pd.read_csv(%0A%20%20%20%20%20%20%20%20%20%20%20%20label_file%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20sep%3D%22%5Ct%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20header%3DNone%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20dtype%3D%7B%22label%22%3A%20int%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20names%3D%5B%22label%22%5D%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20combined_table%20%3D%20pd.concat(%5Bdata_table%2C%20label_table%5D%2C%20axis%3D1)%0A%20%20%20%20%20%20%20%20return%20combined_table.sample(n_samples)%0A%0A%0A%20%20%20%20focal_word%20%3D%20%22apple%22%0A%0A%20%20%20%20train_data%20%3D%20load_data(focal_word%2C%20is_train%3DTrue)%0A%20%20%20%20return%20focal_word%2C%20load_data%2C%20train_data%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(train_data)%3A%0A%20%20%20%20train_data.head(10)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20The%20data%20contains%20%60word_pos%60%2C%20%60label%60%2C%20and%20%60sentence%60.%20The%20%60word_pos%60%20indicates%20the%20token%20in%20question%20for%20polysemy%20resolution.%20The%20%60label%60%20indicates%20the%20meaning%20of%20the%20token%20in%20the%20sentence.%0A%0A%20%20%20%20%20%20%20%20Now%2C%20let's%20embed%20the%20tokens%20in%20the%20sentences%20in%20batch.%0A%0A%20%20%20%20%20%20%20%20Since%20we%20have%20multiple%20layers%20in%20the%20BERT%20model%2C%20we%20have%20freedom%20to%20choose%20which%20layer%20to%20use%20for%20the%20embeddings.%20Choose%20the%20layer%20for%20the%20embedding%2C%20and%20see%20how%20the%20embedding%20changes.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(model%2C%20tokenizer%2C%20torch%2C%20train_data)%3A%0A%20%20%20%20from%20collections%20import%20defaultdict%0A%0A%20%20%20%20%23%20Batch%20size%0A%20%20%20%20batch_size%20%3D%20128%0A%0A%20%20%20%20all_labels%20%3D%20%5B%5D%0A%20%20%20%20all_sentences%20%3D%20%5B%5D%0A%20%20%20%20all_embeddings%20%3D%20defaultdict(list)%0A%0A%20%20%20%20%23%20Process%20data%20in%20batches%0A%20%20%20%20for%20i%20in%20range(0%2C%20len(train_data)%2C%20batch_size)%3A%0A%20%20%20%20%20%20%20%20batch%20%3D%20train_data.iloc%5Bi%3Ai%2Bbatch_size%5D%0A%0A%20%20%20%20%20%20%20%20%23%20Prepare%20batch%20data%0A%20%20%20%20%20%20%20%20batch_sentences%20%3D%20batch%5B%22sentence%22%5D.tolist()%0A%20%20%20%20%20%20%20%20batch_focal_indices%20%3D%20batch%5B%22word_pos%22%5D.tolist()%0A%20%20%20%20%20%20%20%20batch_labels%20%3D%20batch%5B%22label%22%5D.tolist()%0A%0A%20%20%20%20%20%20%20%20%23%20Tokenize%20all%20sentences%20in%20the%20batch%0A%20%20%20%20%20%20%20%20encoded_inputs%20%3D%20tokenizer(%0A%20%20%20%20%20%20%20%20%20%20%20%20batch_sentences%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20padding%3DTrue%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20truncation%3DTrue%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20return_tensors%3D%22pt%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20add_special_tokens%3DTrue%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%20%20%20%20%23%20Get%20BERT%20embeddings%20for%20the%20batch%0A%20%20%20%20%20%20%20%20_outputs_batch%20%3D%20model(**encoded_inputs%2C%20output_hidden_states%3DTrue)%0A%0A%20%20%20%20%20%20%20%20%23%20Get%20the%20focal%20token%20embeddings%0A%20%20%20%20%20%20%20%20for%20layer_id%20in%20range(len(_outputs_batch.hidden_states))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20focal_token_embeddings%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_outputs_batch.hidden_states%5Blayer_id%5D%5Bi%2C%20batch_focal_indices%2C%20%3A%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20i%2C%20batch_focal_indices%20in%20enumerate(batch_focal_indices)%0A%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20all_embeddings%5Blayer_id%5D%2B%3Dfocal_token_embeddings%0A%0A%0A%20%20%20%20%20%20%20%20%23%20Stack%20the%20embeddings%20and%20labels%0A%20%20%20%20%20%20%20%20all_labels%20%3D%20all_labels%20%2B%20batch_labels%0A%20%20%20%20%20%20%20%20all_sentences%20%3D%20all_sentences%20%2B%20batch_sentences%0A%0A%20%20%20%20for%20layer_id%20in%20all_embeddings.keys()%3A%0A%20%20%20%20%20%20%20%20all_embeddings%5Blayer_id%5D%20%3D%20torch.vstack(all_embeddings%5Blayer_id%5D).detach().numpy()%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20all_embeddings%2C%0A%20%20%20%20%20%20%20%20all_labels%2C%0A%20%20%20%20%20%20%20%20all_sentences%2C%0A%20%20%20%20%20%20%20%20batch%2C%0A%20%20%20%20%20%20%20%20batch_focal_indices%2C%0A%20%20%20%20%20%20%20%20batch_labels%2C%0A%20%20%20%20%20%20%20%20batch_sentences%2C%0A%20%20%20%20%20%20%20%20batch_size%2C%0A%20%20%20%20%20%20%20%20defaultdict%2C%0A%20%20%20%20%20%20%20%20encoded_inputs%2C%0A%20%20%20%20%20%20%20%20focal_token_embeddings%2C%0A%20%20%20%20%20%20%20%20i%2C%0A%20%20%20%20%20%20%20%20layer_id%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo%2C%20slider_focal_layer_ids)%3A%0A%20%20%20%20_text%20%3D%20mo.md(%22Now%2C%20let's%20visualize%20the%20embeddings%20in%202D%20using%20PCA.%22)%0A%20%20%20%20mo.vstack(%5B_text%2C%20slider_focal_layer_ids%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(%0A%20%20%20%20PCA%2C%0A%20%20%20%20all_embeddings%2C%0A%20%20%20%20all_labels%2C%0A%20%20%20%20all_sentences%2C%0A%20%20%20%20pd%2C%0A%20%20%20%20slider_focal_layer_ids%2C%0A)%3A%0A%20%20%20%20import%20altair%20as%20alt%0A%20%20%20%20%23%20Apply%20PCA%20to%20reduce%20dimensions%20to%202D%0A%20%20%20%20pca%20%3D%20PCA(n_components%3D2%2C%20random_state%3D42)%0A%20%20%20%20xy%20%3D%20pca.fit_transform(all_embeddings%5Bslider_focal_layer_ids.value%5D)%0A%0A%20%20%20%20%23%20Create%20a%20DataFrame%20for%20Altair%0A%20%20%20%20df_chart%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20'x'%3A%20xy%5B%3A%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20'y'%3A%20xy%5B%3A%2C%201%5D%2C%0A%20%20%20%20%20%20%20%20'label'%3A%20all_labels%2C%0A%20%20%20%20%20%20%20%20'sentence'%3A%20all_sentences%0A%20%20%20%20%7D)%0A%0A%20%20%20%20chart%20%3D%20alt.Chart(df_chart).mark_circle(size%3D120).encode(%0A%20%20%20%20%20%20%20%20x%3Dalt.X('x%3AQ'%2C%20title%3D'PCA%201')%2C%0A%20%20%20%20%20%20%20%20y%3Dalt.Y('y%3AQ'%2C%20title%3D'PCA%202')%2C%0A%20%20%20%20%20%20%20%20color%3Dalt.Color('label%3AN'%2C%20legend%3Dalt.Legend(title%3D%22Label%22))%2C%0A%20%20%20%20%20%20%20%20tooltip%3D%5B'label'%2C%20'sentence'%5D%0A%20%20%20%20).properties(%0A%20%20%20%20%20%20%20%20width%3D700%2C%0A%20%20%20%20%20%20%20%20height%3D500%2C%0A%20%20%20%20%20%20%20%20title%3D'Word%20Embeddings%20Visualization'%0A%20%20%20%20).interactive()%0A%0A%20%20%20%20%23%20Display%20the%20chart%0A%20%20%20%20chart%0A%20%20%20%20return%20alt%2C%20chart%2C%20df_chart%2C%20pca%2C%20xy%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20Case%20Study%202%3A%20Implicit%20associations%20in%20language%0A%0A%20%20%20%20%20%20%20%20Human%20language%20contains%20implicit%20associations%20between%20concepts%20that%20reflect%20cultural%20norms%2C%20stereotypes%2C%20and%20common%20patterns.%20These%20associations%20can%20be%20captured%20by%20language%20models%20like%20BERT%2C%20which%20learn%20statistical%20patterns%20from%20large%20text%20corpora.%0A%0A%20%20%20%20%20%20%20%20Let's%20explore%20how%20we%20can%20use%20BERT%20to%20capture%20implicit%20associations%20between%20concepts%2C%20using%20the%20example%20of%20noun-color%20associations.%0A%0A%20%20%20%20%20%20%20%20We%20set%20up%20a%20MLM%20task%20using%20the%20following%20template%3A%0A%0A%20%20%20%20%20%20%20%20%22The%20most%20common%20color%20for%20%7Bobject%7D%20is%20%5BMASK%5D.%22%0A%0A%20%20%20%20%20%20%20%20-%20The%20%7Bobject%7D%20is%20any%20noun%2C%20e.g.%2C%20%22grass%22%2C%20%22sky%22%2C%20%22banana%22%2C%20%22blood%22%2C%20%22snow%22%2C%20etc.%0A%20%20%20%20%20%20%20%20-%20BERT%20will%20predict%20the%20masked%20tokens%20in%20the%20sentence.%0A%20%20%20%20%20%20%20%20-%20Since%20there%20is%20no%20words%20associated%20explicitly%20with%20the%20color%20to%20be%20predicted%2C%20the%20prediction%20is%20expected%20to%20reflect%20the%20BERT's%20understanding%20of%20the%20semantic%20meaning%20of%20the%20%7Bobject%7D.%0A%0A%20%20%20%20%20%20%20%20%23%23%23%20Implementation%0A%0A%0A%0A%20%20%20%20%20%20%20%20To%20run%20this%20task%2C%20we%20will%20use%20%60BertForMaskedLM%60%20an%20adapted%20version%20of%20%60AutoModel%60%20for%20masked%20language%20modeling.%0A%0A%20%20%20%20%20%20%20%20-%20**%60BertForMaskedLM%60**%20is%20specifically%20designed%20for%20BERT%20models%20with%20a%20language%20modeling%20head%20on%20top.%0A%20%20%20%20%20%20%20%20-%20This%20specialized%20model%20includes%20a%20linear%20layer%20that%20projects%20the%20hidden%20states%20to%20vocabulary-sized%20logits%2C%20enabling%20direct%20prediction%20of%20masked%20tokens.%0A%0A%20%20%20%20%20%20%20%20%3Cimg%20src%3D%22https%3A%2F%2Fwww.oreilly.com%2Fapi%2Fv2%2Fepubs%2F9781098136789%2Ffiles%2Fassets%2Fnlpt_0404.png%22%20style%3D%22display%3A%20block%3B%20margin-left%3A%20auto%3B%20margin-right%3A%20auto%3B%22%3E%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20from%20transformers%20import%20BertForMaskedLM%0A%0A%20%20%20%20model_masked_lm%20%3D%20BertForMaskedLM.from_pretrained('bert-base-uncased')%0A%20%20%20%20return%20BertForMaskedLM%2C%20model_masked_lm%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22Now%2C%20let's%20predict%20the%20masked%20word%20in%20the%20sentence.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(model_masked_lm%2C%20tokenizer%2C%20torch)%3A%0A%20%20%20%20def%20predict_masked_word(template%2C%20object_name%2C%20top_k%3D5)%3A%0A%20%20%20%20%20%20%20%20%23%20Fill%20in%20the%20template%20with%20the%20object%0A%20%20%20%20%20%20%20%20text%20%3D%20template.format(object%3Dobject_name)%0A%0A%20%20%20%20%20%20%20%20%23%20Tokenize%20input%0A%20%20%20%20%20%20%20%20inputs%20%3D%20tokenizer(text%2C%20return_tensors%3D%22pt%22)%0A%0A%20%20%20%20%20%20%20%20%23%20Get%20position%20of%20%5BMASK%5D%20token%0A%20%20%20%20%20%20%20%20mask_token_index%20%3D%20torch.where(inputs%5B%22input_ids%22%5D%20%3D%3D%20tokenizer.mask_token_id)%5B1%5D%0A%0A%20%20%20%20%20%20%20%20%23%20Forward%20pass%0A%20%20%20%20%20%20%20%20with%20torch.no_grad()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20outputs%20%3D%20model_masked_lm(**inputs)%0A%0A%20%20%20%20%20%20%20%20%23%20Get%20predictions%0A%20%20%20%20%20%20%20%20logits%20%3D%20outputs.logits%20%20%23%20This%20is%20the%20correct%20attribute%20for%20BertForMaskedLM%0A%20%20%20%20%20%20%20%20mask_token_logits%20%3D%20logits%5B0%2C%20mask_token_index%2C%20%3A%5D%0A%0A%20%20%20%20%20%20%20%20%23%20Get%20top-k%20predictions%0A%20%20%20%20%20%20%20%20top_k_tokens%20%3D%20torch.topk(mask_token_logits%2C%20top_k%2C%20dim%3D1).indices%5B0%5D.tolist()%0A%0A%20%20%20%20%20%20%20%20%23%20Convert%20token%20IDs%20to%20words%0A%20%20%20%20%20%20%20%20top_k_words%20%3D%20%5Btokenizer.convert_ids_to_tokens(token_id)%20for%20token_id%20in%20top_k_tokens%5D%0A%0A%20%20%20%20%20%20%20%20return%20top_k_words%0A%20%20%20%20return%20(predict_masked_word%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo%2C%20noun_placeholder%2C%20predict_masked_word)%3A%0A%20%20%20%20template%20%20%3D%20%22The%20most%20common%20color%20for%20%7Bobject%7D%20is%20%5BMASK%5D.%22%0A%20%20%20%20top_k%20%3D%208%0A%0A%20%20%20%20obj%20%3D%20noun_placeholder.value%0A%20%20%20%20predictions%20%3D%20predict_masked_word(template%2C%20obj%2C%20top_k)%0A%20%20%20%20results%20%3D%20f%22**%7Bobj%7D**%3A%20%7B'%2C%20'.join(predictions)%7D%22%0A%0A%20%20%20%20mo.vstack(%5Bnoun_placeholder%2Cresults%5D)%0A%20%20%20%20return%20obj%2C%20predictions%2C%20results%2C%20template%2C%20top_k%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20slider_focal_layer_ids%20%3D%20mo.ui.slider(%0A%20%20%20%20%20%20%20%200%2C%0A%20%20%20%20%20%20%20%2012%2C%0A%20%20%20%20%20%20%20%201%2C%0A%20%20%20%20%20%20%20%204%2C%0A%20%20%20%20%20%20%20%20label%3D%22Layer%20to%20use%22%2C%0A%20%20%20%20)%0A%20%20%20%20noun_placeholder%20%3D%20mo.ui.text(%0A%20%20%20%20%20%20%20%20value%3D%22banana%22%2C%0A%20%20%20%20%20%20%20%20label%3D%22When%20asked%20about%20its%20color%2C%20%7Bobject%7D%20is%20described%20as%20%5BMASK%5D.%22%2C%0A%20%20%20%20%20%20%20%20full_width%20%3D%20True%0A%20%20%20%20)%0A%20%20%20%20return%20noun_placeholder%2C%20slider_focal_layer_ids%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20inspect%0A%20%20%20%20import%20markdown%0A%20%20%20%20return%20inspect%2C%20markdown%2C%20mo%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
148ffe6c40cd2c3b715668d54c7004f10e4e7195d0db72696e0b8d12a50643b6