Trying to understand the code from https://www.analyticsvidhya.com/blog/2021/05/measuring-text-similarity-using-bert/

I am looking at understanding the syntax on these two lines:

token['input_ids'] = torch.stack(token['input_ids'])
token['attention_mask'] = torch.stack(token['attention_mask'])
output = model(**token)

What does **tokens do? I can't seem to print it, or debug its value. I get a Syntax Error exception

I am familiar with the role of **arg in function calls, where it changes an expression like (a=1, b=2) to a dictionary like {'a':1, 'b':2}, but what does it do when the expression is already a dictionary? Or am I misunderstanding something here?


1 Answer 1


When **dict is used in a function call like that, the dictionary is expanded into keyword arguments, so the dictionary keys become the keywords and the dictionary values become the argument values. So in your case:

output = model(**token)

is expanded to:

output = model(input_ids=token['input_ids'], attention_mask=token['attention_mask'])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.