-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathembedding.py
More file actions
66 lines (56 loc) · 1.97 KB
/
Copy pathembedding.py
File metadata and controls
66 lines (56 loc) · 1.97 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import gc
import os
import torch
from tqdm import tqdm
from datasets import load_dataset
from transformers import AutoTokenizer, AutoModel
emb_model = "BAAI/bge-m3"
tokenizer = AutoTokenizer.from_pretrained(emb_model)
model = AutoModel.from_pretrained(
emb_model,
device_map="auto",
)
def process(sample):
user_id = sample['user_id']
data = sample['data']
text = data['text']
profile = sample['profile']
profile = sorted(profile, key=lambda x: x["timestamp"], reverse=False)
prof_inps = [f"{prof['text']}" for prof in profile]
return user_id, prof_inps, text
@torch.no_grad()
def get_embeddings(all_txts):
batch_size = 512
embeddings = []
for i in range(0, len(all_txts), batch_size):
batch_txts = all_txts[i:i+batch_size]
batch_tokens = tokenizer(
batch_txts, truncation=True, padding=True, return_tensors="pt")
for key in batch_tokens:
batch_tokens[key] = batch_tokens[key].to(model.device)
batch_embeddings = model(**batch_tokens)
batch_embeddings = torch.nn.functional.normalize(
batch_embeddings[0][:, 0], p=2, dim=1).detach().cpu()
embeddings.append(batch_embeddings)
del batch_tokens, batch_embeddings
torch.cuda.empty_cache()
gc.collect()
embeddings = torch.cat(embeddings, dim=0)
return embeddings
categories = ["Books", "Movies_and_TV", "CDs_and_Vinyl"]
split = "test"
output_dir = "embeddings"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for category in categories:
os.makedirs(f"{output_dir}/{category}", exist_ok=True)
main_dataset = load_dataset(
"SnowCharmQ/DPL-main",
category,
split=split
)
for sample in tqdm(main_dataset, desc=f"Embedding {category} {split}"):
user_id, prof_inps, text = process(sample)
all_txts = prof_inps + [text]
embeddings = get_embeddings(all_txts)
torch.save(embeddings, f"{output_dir}/{category}/{user_id}.emb")