-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathprint_embeddings_hidden.py
More file actions
31 lines (23 loc) · 980 Bytes
/
Copy pathprint_embeddings_hidden.py
File metadata and controls
31 lines (23 loc) · 980 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from transformers import AutoModel, AutoTokenizer
from utilities.args_parser import parse_args
from utilities.embeddings_print import print_direct_embeddings
def main(tokenizer_name, model_name, prompt):
# Load the tokenizer
tokenizer = AutoTokenizer.from_pretrained(tokenizer_name)
tokenizer.add_special_tokens({'pad_token': '[PAD]'})
# Load the model configuration and model
model = AutoModel.from_pretrained(model_name)
# Tokenize the input text
tokens = tokenizer(prompt, return_tensors="pt")
input_ids = tokens['input_ids']
# make a forward pass
outputs = model(input_ids)
# Get embeddings from last hidden state layer
embeddings = outputs.last_hidden_state
# Use the utility function to print direct embeddings
print_direct_embeddings(tokenizer, embeddings, input_ids)
if __name__ == "__main__":
# parse the arguments
args = parse_args()
# execute
main(args.tokenizer, args.model, args.prompt)