array([ 0.0123291 , 0.20410156, -0.28515625, 0.21679688, 0.11816406,
0.08300781, 0.04980469, -0.00952148, 0.22070312, -0.12597656],
dtype=float32)
Words most similar to 'cat':
----------------------------------------
cats | similarity: 0.8099
dog | similarity: 0.7609
kitten | similarity: 0.7465
feline | similarity: 0.7326
beagle | similarity: 0.7151
puppy | similarity: 0.7075
pup | similarity: 0.6934
pet | similarity: 0.6892
felines | similarity: 0.6756
chihuahua | similarity: 0.6710
Words most similar to 'dog':
----------------------------------------
dogs | similarity: 0.8680
puppy | similarity: 0.8106
pit_bull | similarity: 0.7804
pooch | similarity: 0.7627
cat | similarity: 0.7609
golden_retriever | similarity: 0.7501
German_shepherd | similarity: 0.7465
Rottweiler | similarity: 0.7438
beagle | similarity: 0.7419
pup | similarity: 0.7407
Words most similar to 'pizza':
----------------------------------------
pizzas | similarity: 0.7863
Domino_pizza | similarity: 0.7343
Pizza | similarity: 0.6988
pepperoni_pizza | similarity: 0.6903
sandwich | similarity: 0.6840
burger | similarity: 0.6570
sandwiches | similarity: 0.6495
takeout_pizza | similarity: 0.6492
gourmet_pizza | similarity: 0.6401
meatball_sandwich | similarity: 0.6377
Similarity between 'cat' and 'dog': 0.7609
Similarity between 'cat' and 'kitten': 0.7465
Similarity between 'cat' and 'car': 0.2153
Similarity between 'doctor' and 'hospital': 0.5143
Similarity between 'king' and 'queen': 0.6511
king + woman - man:
--------------------------------------------------
queen | similarity: 0.7118
monarch | similarity: 0.6190
princess | similarity: 0.5902
crown_prince | similarity: 0.5499
prince | similarity: 0.5377
Paris + Italy - France:
--------------------------------------------------
Milan | similarity: 0.7222
Rome | similarity: 0.7028
Palermo_Sicily | similarity: 0.5968
Italian | similarity: 0.5911
Tuscany | similarity: 0.5633
walking + swim - walk:
--------------------------------------------------
swimming | similarity: 0.8246
swam | similarity: 0.6807
swims | similarity: 0.6538
swimmers | similarity: 0.6495
paddling | similarity: 0.6424

x = 42 (integer)name = "Alice" (string)pi = 3.14 (float)numbers = [1, 2, 3, 4, 5]words = ["cat", "dog", "bird"]numbers[0] returns 1print("Hello"), len([1, 2, 3])def greet(name): return f"Hello {name}"import math - mathematical functionsfrom transformers import AutoModel - import specific componentsmath.sqrt(16)pip - standard package installer (similar to NuGet for C#)uv - modern, faster alternative to pipHello World and Word2Vec notebooks in Colab and VS Code
Setup Colab, get the two notebooks up and running (hello-world, word2vec)
graph LR
Input["Input: 'Bonjour, comment allez-vous?'"]
Transformer[Transformer]
Output["Output: 'Hello, how are you?'"]
Input --> Tokenize --> Transformer --> Decode --> Output
tensor([8703, 2, 1027, 5682, 21, 682, 54, 0])
Tokens: ['▁Bonjour', ',', '▁comment', '▁allez', '-', 'vous', '?', '</s>']
graph LR
Input["Input: 'Bonjour, comment allez-vous?'"]
Transformer[Transformer]
Output["Output: 'Hello, how are you?'"]
Input --> Tokenize --> Transformer --> Decode --> Output
graph LR
Input["Input: 'Bonjour, comment allez-vous?'"]
subgraph Transformer
Encoder[Encoder]
Decoder[Decoder]
Encoder --> Decoder
end
Output["Output: 'Hello, how are you?'"]
Input --> Tokenize --> Encoder
Decoder --> Decode --> Output
graph LR
Input["Input: 'Bonjour, comment allez-vous?'"]
subgraph Transformer
direction LR
subgraph "Encoder Stack (N layers)"
E[Encoder<br/>Layers<br/>1...N]
end
subgraph "Decoder Stack (N layers)"
D[Decoder<br/>Layers<br/>1...N]
end
E -.->|Context| D
end
Output["Output: 'Hello, how are you?'"]
Input --> Tokenize --> E
D --> Decode --> Output
output_ids = model.generate(input_ids)
graph LR
Input["Input: 'Bonjour, comment allez-vous?'"]
subgraph Transformer
direction TB
subgraph "Encoder Layer"
direction TB
E_SelfAttn[Multi-Head<br/>Self-Attention]
E_AddNorm1[Add & Norm]
E_FFN[Feed-Forward<br/>Network]
E_AddNorm2[Add & Norm]
E_SelfAttn --> E_AddNorm1 --> E_FFN --> E_AddNorm2
end
subgraph "Decoder Layer"
direction TB
D_SelfAttn[Masked Multi-Head<br/>Self-Attention]
D_AddNorm1[Add & Norm]
D_CrossAttn[Multi-Head<br/>Cross-Attention]
D_AddNorm2[Add & Norm]
D_FFN[Feed-Forward<br/>Network]
D_AddNorm3[Add & Norm]
D_SelfAttn --> D_AddNorm1 --> D_CrossAttn --> D_AddNorm2 --> D_FFN --> D_AddNorm3
end
E_AddNorm2 -.->|Encoder<br/>Output| D_CrossAttn
end
Output["Output: 'Hello, how are you?'"]
Input --> E_SelfAttn
D_AddNorm3 --> Output
Translation Transformer in Colab
Experiment with your own phrases in the translation-transformer.ipynb notebook
import torch
def autocomplete(prompt, max_length=50, temperature=0.7, top_k=50, top_p=0.9):
# Encode the prompt with attention mask
inputs = tokenizer(prompt, return_tensors="pt")
# Generate continuation
with torch.no_grad():
output = model.generate(
inputs['input_ids'],
attention_mask=inputs['attention_mask'],
max_length=max_length,
temperature=temperature,
top_k=top_k,
top_p=top_p,
do_sample=True,
pad_token_id=tokenizer.eos_token_id
)
# Decode and return the generated text
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
return generated_textMary had a little lamb, and the young woman asked her for a little lamb, and they gave it to her.
"Oh, my child, it is good to have a little lamb," said he, "but it is not to be bought, for it is hard to make, and it is much more difficult to make.
"When you have a little lamb, it
GPT-2 notebook in Colab
Experiment with your own phrases in the GPT-2.ipynb notebook
