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 transformer model? (BERT Model) #77

Open
indramal opened this issue Dec 11, 2022 · 3 comments
Open

How to plot transformer model? (BERT Model) #77

indramal opened this issue Dec 11, 2022 · 3 comments

Comments

@indramal
Copy link

I try to plot bert model using this library but I unable to do it.

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")
from torchviz import make_dot, make_dot_from_trace
make_dot(model(inputs), params=dict(model.named_parameters()))

Error:


KeyError Traceback (most recent call last)
/usr/local/lib/python3.8/dist-packages/transformers/tokenization_utils_base.py in getattr(self, item)
247 try:
--> 248 return self.data[item]
249 except KeyError:

KeyError: 'size'

During handling of the above exception, another exception occurred:

AttributeError Traceback (most recent call last)
3 frames
/usr/local/lib/python3.8/dist-packages/transformers/tokenization_utils_base.py in getattr(self, item)
248 return self.data[item]
249 except KeyError:
--> 250 raise AttributeError
251
252 def getstate(self):

AttributeError:

@tanujjain
Copy link

Try replacing the following line:
make_dot(model(inputs), params=dict(model.named_parameters()))

with:

make_dot(model(inputs).mean(), params=dict(model.named_parameters()))
You could probably use some aggregation other than mean too.

@indramal
Copy link
Author

I got this error for make_dot(model(inputs).mean(), params=dict(model.named_parameters()))

image

@Contributorrandom
Copy link

Use torchview package

from transformers import AutoModel, AutoTokenizer
from torchview import draw_graph

model = AutoModel.from_pretrained("bert-base-uncased")
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")

inputs = tokenizer("Hello world!", return_tensors="pt")
model_graph = draw_graph(model, input_data=inputs)
model_graph.visual_graph

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

3 participants