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

Chapter 2: List object do not have anything .to(device) (something like this) #148

Open
usmanyaqoob49 opened this issue Sep 28, 2024 · 0 comments

Comments

@usmanyaqoob49
Copy link

this is code from book:
def extract_hidden_states(batch):
# Place model inputs on the GPU
inputs = {k:v.to(device) for k,v in batch.items()
if k in tokenizer.model_input_names}
# Extract last hidden states
with torch.no_grad():
last_hidden_state = model(**inputs).last_hidden_state
# Return vector for [CLS] token
return {"hidden_state": last_hidden_state[:,0].cpu().numpy()}

this might give you error about list feature only when your data is not correctly converted to tensors.
so you can use this:
def extract_hidden_state(batch):
inputs = {k: batch[k].to(device) for k in ['input_ids', 'attention_mask']}
with torch.no_grad():
outputs = distilbert(**inputs)
last_hidden_state = outputs.last_hidden_state[:, 0].cpu().numpy()
return {"hidden_state": last_hidden_state}

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