Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use PolyFuzz with T5 model? #58

Open
bhishanpdl opened this issue Feb 16, 2023 · 1 comment
Open

How to use PolyFuzz with T5 model? #58

bhishanpdl opened this issue Feb 16, 2023 · 1 comment

Comments

@bhishanpdl
Copy link

I am learning the use case of polyfuzz with T5 embedding.
I am getting error when using following code:

polyfuzz: 0.4.0
transformers: 4.26.1
torch: 1.13.1+cu117
tensorflow: 2.11.0
tensorflow_hub: 0.12.0

MWE

import torch
import numpy as np
import polyfuzz
from transformers import T5Tokenizer, T5ForConditionalGeneration

# Load the T5 model and tokenizer
model_name = 't5-small'
model = T5ForConditionalGeneration.from_pretrained(model_name)
tokenizer = model.from_pretrained(model_name)

# Define your target and candidate strings
target_strings = ['The quick brown fox jumps over the lazy dog', 'The sky is blue']
candidate_strings = ['The fox is quick and the dog is lazy', 'The ocean is blue']

# Tokenize the strings and convert them to T5 embeddings
target_tokens = tokenizer.batch_encode_plus(target_strings, padding=True, truncation=True, return_tensors='pt')
candidate_tokens = tokenizer.batch_encode_plus(candidate_strings, padding=True, truncation=True, return_tensors='pt')
with torch.no_grad():
    target_embeddings = model.encoder(input_ids=target_tokens['input_ids']).last_hidden_state.detach().numpy()
    candidate_embeddings = model.encoder(input_ids=candidate_tokens['input_ids']).last_hidden_state.detach().numpy()

# Create a PolyFuzz object with default settings
model = polyfuzz.PolyFuzz()

# Fit the model with the T5 embeddings
model.fit(target_embeddings, candidate_embeddings)

# Get the matches between the target and candidate strings
matches = model.get_matches()
@MaartenGr
Copy link
Owner

It is not possible within PolyFuzz to only supply the model with embeddings, you would have to pass the raw strings yourself and create either a custom model yourself or use something like Flair to load in the model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants