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

Ask For the problem of Transformer-XL to get text embedding #6

Closed
eddy60506 opened this issue May 22, 2022 · 2 comments
Closed

Ask For the problem of Transformer-XL to get text embedding #6

eddy60506 opened this issue May 22, 2022 · 2 comments

Comments

@eddy60506
Copy link

Dear Author:

Very appreciate for the sample code of RGTN, I try to use Hugging Face transformer-XL to get semantic embedding of the node text.
Here is the official code in the Hugging Face transformer-XL doc:

===
`from transformers import TransfoXLTokenizer, TransfoXLModel
import torch

tokenizer = TransfoXLTokenizer.from_pretrained("transfo-xl-wt103")
model = TransfoXLModel.from_pretrained("transfo-xl-wt103")

inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")
outputs = model(**inputs)

last_hidden_states = outputs.last_hidden_state`

===

But the last_hidden_states was the embedding vector of the word not the embedding of text, can you please give me the way to get the text embedding or release the part of sample code?

@GRAPH-0
Copy link
Owner

GRAPH-0 commented May 23, 2022

An example code

tokenizer = XLNetTokenizer.from_pretrained('xlnet-base-cased')
model = XLNetModel.from_pretrained('xlnet-base-cased',
                                   output_hidden_states=True,
                                   output_attentions=True).to(device)

lang_features = dict()
with open(args.input_file) as fin:
    # read your text file as fin; node_id + text
    lines = fin.readlines()
    for line in tqdm(lines[1:]):
        l = line.split('\t')
        node_id = l[0]
        input_ids = torch.tensor([tokenizer.encode(l[1])]).to(device)
        input_ids = input_ids[:, :args.max_word]
        all_hidden_states, all_attentions = model(input_ids)[-2:]
        rep = (all_hidden_states[-2][0] * all_attentions[-2][0].mean(dim=0).mean(dim=0).view(-1, 1)).sum(dim=0)
        lang_features[node_id] = rep.detach().cpu().numpy()

@eddy60506
Copy link
Author

thx! I will try this code.

@GRAPH-0 GRAPH-0 closed this as completed Jun 23, 2022
@GRAPH-0 GRAPH-0 mentioned this issue Jun 4, 2023
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