I am following the quick start guide here. The problem is they have provided code for GPU machine and I am running the code on CPU based Ubuntu machine. I have commented the lines that put everything in CUDA. The code is now showing error and I don't know how to resolve it. The question is "How can I make this work?"
I have checked this answer and this is not what I'm looking for.
The full code is here
1. Using BertModel to encode inputs in hidden-states:#Load pre-trained model (weights)
model = BertModel.from_pretrained('bert-base-uncased')
#Set the model in evaluation mode to desactivate the DropOut modules
# This is IMPORTANT to have reproducible results during evaluation!
model.eval()
#***I have commented these 3 lines***
# If you have a GPU, put everything on cuda
#tokens_tensor = tokens_tensor.to('cuda')
#segments_tensors = segments_tensors.to('cuda')
#model.to('cuda')
#Rest all is untouched
# *** -----------------***---------------***
# Predict hidden states features for each layer
with torch.no_grad():
# See the models docstrings for the detail of the inputs
outputs = model(tokens_tensor, token_type_ids=segments_tensors)
# PyTorch-Transformers models always output tuples.
# See the models docstrings for the detail of all the outputs
# In our case, the first element is the hidden state of the last layer of the Bert model
encoded_layers = outputs[0]
# We have encoded our input sequence in a FloatTensor of shape (batch size, sequence length, model hidden dimension)
assert tuple(encoded_layers.shape) == (1, len(indexed_tokens), model.config.hidden_size)
Error for 1:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-40-a86e9643e7f3> in <module>
11
12 # We have encoded our input sequence in a FloatTensor of shape (batch size, sequence length, model hidden dimension)
---> 13 assert tuple(encoded_layers).shape == (1, len(indexed_tokens), model.config.hidden_size)
AttributeError: 'tuple' object has no attribute 'shape'
2. Using BertForMaskedLM to predict a masked token:
# Load pre-trained model (weights)
model = BertForMaskedLM.from_pretrained('bert-base-uncased')
model.eval()
#***---------------Commented--------------------------
# If you have a GPU, put everything on cuda
#tokens_tensor = tokens_tensor.to('cuda')
#segments_tensors = segments_tensors.to('cuda')
#model.to('cuda')
#***---------------------------------------------
# Predict all tokens
with torch.no_grad():
outputs = model(tokens_tensor, token_type_ids=segments_tensors)
predictions = outputs[0]
# confirm we were able to predict 'henson'
predicted_index = torch.argmax(predictions[0, masked_index]).item()
predicted_token = tokenizer.convert_ids_to_tokens([predicted_index])[0]
assert predicted_token == 'henson'
Error for 2:
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-42-9b965490d278> in <module>
17 predicted_index = torch.argmax(predictions[0, masked_index]).item()
18 predicted_token = tokenizer.convert_ids_to_tokens([predicted_index])[0]
---> 19 assert predicted_token == 'henson'
AssertionError: