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

Erlangshen embedding模型推理使用 #431

Open
Congcong-Song opened this issue Oct 19, 2023 · 0 comments
Open

Erlangshen embedding模型推理使用 #431

Congcong-Song opened this issue Oct 19, 2023 · 0 comments

Comments

@Congcong-Song
Copy link

Congcong-Song commented Oct 19, 2023

tokenizer = BertTokenizer.from_pretrained("/home/inspur/nas_data/pretrain/Erlangshen-TCBert-330M-Sentence-Embedding-Chinese")   #.cuda().eval()  # text长度512
model = BertForMaskedLM.from_pretrained("/home/inspur/nas_data/pretrain/Erlangshen-TCBert-330M-Sentence-Embedding-Chinese").cuda().eval()

cos = torch.nn.CosineSimilarity(dim=0, eps=1e-8)

with torch.no_grad():
    # To extract sentence representations for training data
    training_input = tokenizer("怎样的房子才算户型方正?", return_tensors="pt")
    print(f"training_input {training_input}")
    training_output = BertForMaskedLM(**token_text, output_hidden_states=True)
    training_representation = torch.mean(training_outputs.hidden_states[-1].squeeze(), dim=0)

    # To extract sentence representations for training data
    test_input = tokenizer("下面是一则关于[MASK][MASK]的新闻:股票放量下趺,大资金出逃谁在接盘?", return_tensors="pt")
    test_output = BertForMaskedLM(**token_text, output_hidden_states=True)
    test_representation = torch.mean(training_output.hidden_states[-1].squeeze(), dim=0)

similarity_score = cos(training_representation, test_representation)

这个是huggingface的代码,但是里面的token_text和training_outputs没有定义

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

1 participant