-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference_example.py
70 lines (50 loc) · 1.96 KB
/
inference_example.py
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
67
68
69
70
from model import MatryoshkaEmbedder
from utils import compute_similarity
from sentence_transformers import SentenceTransformer
def test_matryoshka_model():
"""Test the base MatryoshkaEmbedder implementation"""
# Initialize the model
embedder = MatryoshkaEmbedder()
# Example texts
texts = [
"The weather is beautiful today",
"It's a sunny and pleasant day",
"This is a completely different topic",
]
# Compare embeddings at different dimensions
dimensions = [768, 512, 256, 128, 64]
for dim in dimensions:
print(f"\nTesting MatryoshkaEmbedder with dimension: {dim}")
embeddings = embedder.encode(texts, dimension=dim)
# Compare similarities
sim1 = compute_similarity(embeddings[0], embeddings[1])
sim2 = compute_similarity(embeddings[0], embeddings[2])
print(f"Similarity between similar sentences: {sim1:.4f}")
print(f"Similarity between different sentences: {sim2:.4f}")
def test_trained_model():
"""Test the custom trained model"""
print("\nTesting trained model performance:")
# Load the trained model
model = SentenceTransformer("trained-matryoshka-model")
# Test texts
texts = ["The weather is nice", "It's a beautiful day"]
# Test different dimensions
dimensions = [768, 512, 256, 128, 64]
for dim in dimensions:
print(f"\nTesting trained model with dimension: {dim}")
# Set the desired dimension
model.truncate_dim = dim
# Get embeddings
embeddings = model.encode(texts)
# Compare similarity
sim = compute_similarity(embeddings[0], embeddings[1])
print(f"Similarity between sentences: {sim:.4f}")
def main():
# Test original MatryoshkaEmbedder
print("Testing original MatryoshkaEmbedder implementation:")
test_matryoshka_model()
# Test trained model
print("\nTesting trained model:")
test_trained_model()
if __name__ == "__main__":
main()