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 plot bert model? (Transfomer models) #99

Open
indramal opened this issue Dec 11, 2022 · 1 comment
Open

How to plot bert model? (Transfomer models) #99

indramal opened this issue Dec 11, 2022 · 1 comment

Comments

@indramal
Copy link

I try to plot bert model using this package. But I unable to do it.

Code:

from transformers import AutoModel, AutoTokenizer
model = AutoModel.from_pretrained("bert-base-uncased")
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
inputs = tokenizer("Hello world!", return_tensors="pt")

After that how to plot it?

import hiddenlayer as hl
hl.build_graph(model, inputs[0])
@Foxglove144
Copy link

Foxglove144 commented Jan 25, 2024

You need to extract input_ids and attention_mask from the tokenizers.Encoding object and pass them as a tuple to the hl.build_graph function.

import hiddenlayer as hl
from transformers import AutoModel, AutoTokenizer

model = AutoModel.from_pretrained("bert-base-uncased")
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
inputs = tokenizer("Hello world!", return_tensors="pt")

input_ids = inputs["input_ids"]
attention_mask = inputs["attention_mask"]

hl.build_graph(model, (input_ids, attention_mask))

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