Last active
April 4, 2025 07:12
-
-
Save wolfecameron/82db74244e4c46206f5d7c1336d7f4cd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
from transformers import AutoTokenizer | |
# load the llama-3.2 tokenizer | |
tokenizer = AutoTokenizer.from_pretrained('meta-llama/Llama-3.1-8B') | |
# raw text | |
text = "This raw text will be tokenized" | |
# create tokens using tokenizer | |
tokens = tokenizer.tokenize(text) | |
token_ids = tokenizer.convert_tokens_to_ids(tokens) | |
# token_ids = tokenizer.encode(text) # directly create token ids | |
# view the results | |
print("Original Text:", text) | |
print("Tokens:", tokens) | |
print("Token IDs:", token_ids) | |
# create token embedding layer | |
VOCABULARY_SIZE: int = 128000 | |
EMBEDDING_DIM: int = 768 | |
token_embedding_layer = torch.nn.Embedding( | |
num_embeddings=VOCABULARY_SIZE, | |
embedding_dim=EMBEDDING_DIM, | |
) | |
# get token embeddings (IDs must be passed as a tensor, not a list) | |
token_emb = token_embedding_layer(torch.tensor(token_ids)) | |
print(f'Token Embeddings Shape: {token_emb.shape}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I get an error as:
What am I missing?
Note: I reviewed https://huggingface.co/meta-llama/Llama-3.1-8B?library=transformers but that does not help much. Same error continues.