1

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:
Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239
AbhisheK
  • 33
  • 5
  • Your code is working perfectly for me. I am running pytorch version 1.1.0 on a Windows machine with python 3.6. May be upgrading to the latest version of `pytorch_transformers` would do the trick. – asymptote Sep 12 '19 at 03:17
  • I installed it just before this quick start guide that I followed, print(torch.__version__) gives me this: 1.2.0+cu92 – AbhisheK Sep 12 '19 at 07:01
  • both the errors have nothing to do with CUDA/CPU. – Wasi Ahmad Sep 14 '19 at 20:33
  • I agree. It was to give context to the problem. What I want to know is "why this error?" and "how can I resolve it?". – AbhisheK Sep 16 '19 at 10:33
  • Any luck here? Face the same problem here – Angus Mar 21 '20 at 12:44

0 Answers0