AttributeError: 'Tensor' object has no attribute 'ndim'
# ❓ Questions & Help

Hello,

when I run the run_generation.py file I got this error:

Traceback (most recent call last):
  File "C:\Users\imen.benamor\AppData\Local\Programs\Python\Python36\lib\site-packages\transformers\tokenization_utils_base.py", line 507, in convert_to_tensors
    if tensor.ndim > 2:
AttributeError: 'Tensor' object has no attribute 'ndim'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "run_generation.py", line 274, in <module>
    main()
  File "run_generation.py", line 227, in main
    encoded_prompt = tokenizer.encode(prompt_text, add_special_tokens=False, return_tensors="pt")
  File "C:\Users\imen.benamor\AppData\Local\Programs\Python\Python36\lib\site-packages\transformers\tokenization_utils_base.py", line 1425, in encode
    **kwargs,
  File "C:\Users\imen.benamor\AppData\Local\Programs\Python\Python36\lib\site-packages\transformers\tokenization_utils_base.py", line 1737, in encode_plus
    **kwargs,
  File "C:\Users\imen.benamor\AppData\Local\Programs\Python\Python36\lib\site-packages\transformers\tokenization_utils.py", line 473, in _encode_plus
    verbose=verbose,
  File "C:\Users\imen.benamor\AppData\Local\Programs\Python\Python36\lib\site-packages\transformers\tokenization_utils_base.py", line 2098, in prepare_for_model
    encoded_inputs, tensor_type=return_tensors, prepend_batch_axis=prepend_batch_axis
  File "C:\Users\imen.benamor\AppData\Local\Programs\Python\Python36\lib\site-packages\transformers\tokenization_utils_base.py", line 159, in __init__
    self.convert_to_tensors(tensor_type=tensor_type, prepend_batch_axis=prepend_batch_axis)
  File "C:\Users\imen.benamor\AppData\Local\Programs\Python\Python36\lib\site-packages\transformers\tokenization_utils_base.py", line 515, in convert_to_tensors
    "Unable to create tensor, you should probably activate truncation and/or padding "
ValueError: Unable to create tensor, you should probably activate truncation and/or padding with 'padding=True' 'truncation=True' to have batched tensors with the same length.

Any suggestions?
Thank you
Hi! Do you mind pasting the command you used to run the script? Thank you!<|||||>Hi @LysandreJik ,
Thank you for your quick reply.
First, I want to cheer you for your amazing work in HuggingFaces Transformers.
For that error I used the command "python run.generation.py --model_type=gpt2 --model_name_or_path=gpt2".
I doubt that the problem originates from my Pytorch version and installation. What do you think?

<|||||>Thank you :) 

This doesn't fail in my environment. Are you running on an older `transformers` version? Do you mind pasting your environment information here?

Just running `transformers-cli env` in your environment should return something similar to this:

```
- `transformers` version: 3.0.2
- Platform: Linux-5.6.16-1-MANJARO-x86_64-with-arch-Manjaro-Linux
- Python version: 3.6.10
- PyTorch version (GPU?): 1.5.0 (True)
- Tensorflow version (GPU?): 2.2.0 (False)
- Using GPU in script?: <fill in>
- Using distributed or parallel set-up in script?: <fill in>
```<|||||>I ran into the same issue when using pytorch19.01-py3 Nvidia container but don't get the error when using pytorch20.06-py3 Nvidia container.  I can't use the latest version of Pytorch because it doesn't support Cuda driver 10.0 - only Cuda driver version 11.0.  The Cuda driver I have is 10.0 version and there are limitations on Nvidia DGX to upgrade to Cuda driver 11.0.  

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/opt/conda/lib/python3.6/site-packages/transformers/tokenization_utils_base.py in convert_to_tensors(self, tensor_type, prepend_batch_axis)
    506                 # at-least2d
--> 507                 if tensor.ndim > 2:
    508                     tensor = tensor.squeeze(0)

AttributeError: 'Tensor' object has no attribute 'ndim'

During handling of the above exception, another exception occurred:<|||||>This is the command I am using that is triggering the error:
encoding = self.tokenizer.encode_plus(
          note,
          add_special_tokens=True,
          max_length=self.max_len,
          return_token_type_ids=True,
          truncation=True,
          pad_to_max_length=True,
          return_attention_mask=True,
          return_tensors='pt',
        )    <|||||>The error went away when I switched off the "return_tensors='pt' " argument<|||||>I'm also running into the same error @LysandreJik . Here's my `transformers-cli env`. 
`- transformers version: 3.0.2`
`- Platform: Darwin-19.5.0-x86_64-i386-64bit`
`- Python version: 3.7.3`
`- PyTorch version (GPU?): 1.0.1 (False)`
`- Tensorflow version (GPU?): not installed (NA)`
`- Using GPU in script?: <fill in>`
`- Using distributed or parallel set-up in script?: <fill in>`
Running something trivial like `python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('I hate you'))"` reproduces the error which leads me to believe I'm missing something obvious here. Here's the last error:
`"Unable to create tensor, you should probably activate truncation and/or padding "
ValueError: Unable to create tensor, you should probably activate truncation and/or padding with 'padding=True' 'truncation=True' to have batched tensors with the same length.`
<|||||>I don't know if you are willing to upgrade, but your issue is that `Tensor.ndim` was introduced in a later version. If you uninstall pytorch and follow these instructions https://pytorch.org/get-started/locally/ you should be all set.<|||||>hi guys i have the same problem i'm trying to load a model and integrate it in django
any solution ?<|||||>same error ! but when i run it on kaggle it works<|||||>Easiest solution is upgrading torch.