Spaces:
Runtime error
Runtime error
im
commited on
Commit
·
120ad45
1
Parent(s):
085ad06
refine formatting
Browse files
app.py
CHANGED
|
@@ -20,51 +20,53 @@ st.title("Transformers: Tokenisers and Embeddings")
|
|
| 20 |
preface_image, preface_text, = st.columns(2)
|
| 21 |
# preface_image.image("https://static.streamlit.io/examples/dice.jpg")
|
| 22 |
# preface_image.image("""https://assets.digitalocean.com/articles/alligator/boo.svg""")
|
| 23 |
-
preface_text.write("""
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
| 28 |
""")
|
| 29 |
|
| 30 |
divider()
|
| 31 |
|
| 32 |
-
st.write("""
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
-
|
| 38 |
-
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
| 52 |
""")
|
| 53 |
|
| 54 |
with st.expander("Copernicus Museum in Warsaw"):
|
| 55 |
-
st.write("""
|
| 56 |
-
Have you ever visited the Copernicus Museum in Warsaw? It's an engaging interactive hub that allows
|
| 57 |
-
you to familiarize yourself with various scientific topics. The experience is both entertaining and educational,
|
| 58 |
-
providing the opportunity to explore different concepts firsthand. **They even feature a small neural network that
|
| 59 |
-
illustrates the neuron activation process during the recognition of handwritten digits!**
|
| 60 |
-
|
| 61 |
-
Taking inspiration from this approach, we'll embark on our journey into the world of Transformer models by first
|
| 62 |
-
establishing a firm understanding of tokenisation and embeddings. This foundation will equip us with the knowledge
|
| 63 |
-
needed to delve into the more complex aspects of these models later on.
|
| 64 |
-
|
| 65 |
-
I encourage you not to hesitate in modifying parameters or experimenting with different models in the provided
|
| 66 |
-
examples. This hands-on exploration can significantly enhance your learning experience. So, let's begin our journey
|
| 67 |
-
through this virtual, interactive museum of AI. Enjoy the exploration!
|
| 68 |
""")
|
| 69 |
st.image("https://i.pinimg.com/originals/04/11/2c/04112c791a859d07a01001ac4f436e59.jpg")
|
| 70 |
|
|
@@ -73,11 +75,12 @@ divider()
|
|
| 73 |
|
| 74 |
st.header("Tokenisers and Tokenisation")
|
| 75 |
|
| 76 |
-
st.write("""
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
| 81 |
""")
|
| 82 |
|
| 83 |
from transformers import AutoTokenizer
|
|
@@ -90,7 +93,7 @@ sentence_encode_bert = tokenizer.encode(sentence)
|
|
| 90 |
sentence_encode_bert = list(zip(sentence_tokenise_bert, sentence_encode_bert))
|
| 91 |
|
| 92 |
st.write(f"""\
|
| 93 |
-
A basic word-level tokenisation, which splits a text by spaces, would produce next tokens:
|
| 94 |
""")
|
| 95 |
st.code(f"""
|
| 96 |
{sentence_split}
|
|
@@ -98,25 +101,26 @@ st.code(f"""
|
|
| 98 |
|
| 99 |
|
| 100 |
st.write(f"""\
|
| 101 |
-
However, we notice that the punctuation may attached to the words. It is disadvantageous, how the tokenization dealt with the word "Don't".
|
| 102 |
-
"Don't" stands for "do not", so it would be better tokenized as ["Do", "n't"]. (Hint: try another sentence: "I musn't tell lies. Don't do this.") This is where things start getting complicated,
|
| 103 |
-
and part of the reason each model has its own tokenizer type. Depending on the rules we apply for tokenizing a text,
|
| 104 |
-
a different tokenized output is generated for the same text.
|
| 105 |
-
A more sophisticated algorithm, with several optimizations, might generate a different set of tokens:
|
|
|
|
| 106 |
st.code(f"""
|
| 107 |
{sentence_tokenise_bert}
|
| 108 |
""")
|
| 109 |
|
| 110 |
with st.expander("click here to look at the Python code:"):
|
| 111 |
st.code(f"""\
|
| 112 |
-
from transformers import AutoTokenizer
|
| 113 |
-
|
| 114 |
-
sentence = "{sentence}"
|
| 115 |
-
sentence_split = sentence.split()
|
| 116 |
-
tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
|
| 117 |
-
sentence_tokenise_bert = tokenizer.tokenize(sentence)
|
| 118 |
-
sentence_encode_bert = tokenizer.encode(sentence)
|
| 119 |
-
sentence_encode_bert = list(zip(sentence_tokenise_bert, sentence_encode_bert))
|
| 120 |
""", language='python')
|
| 121 |
|
| 122 |
|
|
|
|
| 20 |
preface_image, preface_text, = st.columns(2)
|
| 21 |
# preface_image.image("https://static.streamlit.io/examples/dice.jpg")
|
| 22 |
# preface_image.image("""https://assets.digitalocean.com/articles/alligator/boo.svg""")
|
| 23 |
+
preface_text.write("""\
|
| 24 |
+
*Transformers represent a revolutionary class of machine learning architectures that have sparked
|
| 25 |
+
immense interest. While numerous insightful tutorials are available, the evolution of transformer architectures over
|
| 26 |
+
the last few years has led to significant simplifications. These advancements have made it increasingly
|
| 27 |
+
straightforward to understand their inner workings. In this series of articles, I aim to provide a direct, clear explanation of
|
| 28 |
+
how and why modern transformers function, unburdened by the historical complexities associated with their inception.*
|
| 29 |
""")
|
| 30 |
|
| 31 |
divider()
|
| 32 |
|
| 33 |
+
st.write("""\
|
| 34 |
+
In order to understand the recent success in AI we need to understand the Transformer architecture. Its
|
| 35 |
+
rise in the field of Natural Language Processing (NLP) is largely attributed to a combination of several key
|
| 36 |
+
advancements:
|
| 37 |
+
|
| 38 |
+
- Tokenisers and Embeddings
|
| 39 |
+
- Attention and Self-Attention
|
| 40 |
+
- Encoder-Decoder architecture
|
| 41 |
+
|
| 42 |
+
Understanding these foundational concepts is crucial to comprehending the overall structure and function of the
|
| 43 |
+
Transformer model. They are the building blocks from which the rest of the model is constructed, and their roles
|
| 44 |
+
within the architecture are essential to the model's ability to process and generate language. In my view,
|
| 45 |
+
a comprehensive and simple explanation may give a reader a significant advantage in using LLMs. Feynman once said:
|
| 46 |
+
"*I think I can safely say that nobody understands quantum mechanics.*". Because he couldn't explain it to a freshman.
|
| 47 |
+
|
| 48 |
+
Given the importance and complexity of these concepts, I have chosen to dedicate the first article in this series
|
| 49 |
+
solely to Tokenisation and embeddings. The decision to separate the topics into individual articles is driven by a
|
| 50 |
+
desire to provide a thorough and in-depth understanding of each component of the Transformer model.
|
| 51 |
+
|
| 52 |
+
Note: *HuggingFace provides an exceptional [tutorial on Transformer models](https://huggingface.co/docs/transformers/index).
|
| 53 |
+
That tutorial is particularly beneficial for readers willing to dive into advanced topics.*
|
| 54 |
""")
|
| 55 |
|
| 56 |
with st.expander("Copernicus Museum in Warsaw"):
|
| 57 |
+
st.write("""\
|
| 58 |
+
Have you ever visited the Copernicus Museum in Warsaw? It's an engaging interactive hub that allows
|
| 59 |
+
you to familiarize yourself with various scientific topics. The experience is both entertaining and educational,
|
| 60 |
+
providing the opportunity to explore different concepts firsthand. **They even feature a small neural network that
|
| 61 |
+
illustrates the neuron activation process during the recognition of handwritten digits!**
|
| 62 |
+
|
| 63 |
+
Taking inspiration from this approach, we'll embark on our journey into the world of Transformer models by first
|
| 64 |
+
establishing a firm understanding of tokenisation and embeddings. This foundation will equip us with the knowledge
|
| 65 |
+
needed to delve into the more complex aspects of these models later on.
|
| 66 |
+
|
| 67 |
+
I encourage you not to hesitate in modifying parameters or experimenting with different models in the provided
|
| 68 |
+
examples. This hands-on exploration can significantly enhance your learning experience. So, let's begin our journey
|
| 69 |
+
through this virtual, interactive museum of AI. Enjoy the exploration!
|
| 70 |
""")
|
| 71 |
st.image("https://i.pinimg.com/originals/04/11/2c/04112c791a859d07a01001ac4f436e59.jpg")
|
| 72 |
|
|
|
|
| 75 |
|
| 76 |
st.header("Tokenisers and Tokenisation")
|
| 77 |
|
| 78 |
+
st.write("""\
|
| 79 |
+
Tokenisation is the initial step in the data preprocessing pipeline for natural language processing (NLP)
|
| 80 |
+
models. It involves breaking down a piece of text—whether a sentence, paragraph, or document—into smaller units,
|
| 81 |
+
known as "tokens". In English and many other languages, a token often corresponds to a word, but it can also be a
|
| 82 |
+
subword, character, or n-gram. The choice of token size depends on various factors, including the task at hand and
|
| 83 |
+
the language of the text.
|
| 84 |
""")
|
| 85 |
|
| 86 |
from transformers import AutoTokenizer
|
|
|
|
| 93 |
sentence_encode_bert = list(zip(sentence_tokenise_bert, sentence_encode_bert))
|
| 94 |
|
| 95 |
st.write(f"""\
|
| 96 |
+
A basic word-level tokenisation, which splits a text by spaces, would produce next tokens:
|
| 97 |
""")
|
| 98 |
st.code(f"""
|
| 99 |
{sentence_split}
|
|
|
|
| 101 |
|
| 102 |
|
| 103 |
st.write(f"""\
|
| 104 |
+
However, we notice that the punctuation may attached to the words. It is disadvantageous, how the tokenization dealt with the word "Don't".
|
| 105 |
+
"Don't" stands for "do not", so it would be better tokenized as ["Do", "n't"]. (Hint: try another sentence: "I musn't tell lies. Don't do this.") This is where things start getting complicated,
|
| 106 |
+
and part of the reason each model has its own tokenizer type. Depending on the rules we apply for tokenizing a text,
|
| 107 |
+
a different tokenized output is generated for the same text.
|
| 108 |
+
A more sophisticated algorithm, with several optimizations, might generate a different set of tokens:
|
| 109 |
+
""")
|
| 110 |
st.code(f"""
|
| 111 |
{sentence_tokenise_bert}
|
| 112 |
""")
|
| 113 |
|
| 114 |
with st.expander("click here to look at the Python code:"):
|
| 115 |
st.code(f"""\
|
| 116 |
+
from transformers import AutoTokenizer
|
| 117 |
+
|
| 118 |
+
sentence = "{sentence}"
|
| 119 |
+
sentence_split = sentence.split()
|
| 120 |
+
tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
|
| 121 |
+
sentence_tokenise_bert = tokenizer.tokenize(sentence)
|
| 122 |
+
sentence_encode_bert = tokenizer.encode(sentence)
|
| 123 |
+
sentence_encode_bert = list(zip(sentence_tokenise_bert, sentence_encode_bert))
|
| 124 |
""", language='python')
|
| 125 |
|
| 126 |
|