BertTokenizer: ValueError: Input is not valid. Should be a string, a list/tuple of strings or a list/tuple of integers.
Author: mariusjohan
Pull request: no

# 🐛 Bug

## Information

Tokenizer I am using is BertTokenizer and I've also tried using AlbertTokenizer, but it does not have any effect. So I'm thinking that the bug is in the base tokenizer

Language I am using the model on is English, but I don't believe that's the issue.

The problem arises when using:
* [ ] the official example scripts: (give details below)
* [x] my own modified scripts: (give details below)

The tasks I am working on is:
* [ ] an official GLUE/SQUaD task: (give the name)
* [x] my own task or dataset: (give details below)

## To reproduce

Steps to reproduce the behavior:

1. Version: ```transformers==2.11.0```
2. Run this code
```python
from transformers import BertModel, BertTokenizer
text = 'A quick brown fox jumps over' # Just a dummy text
BertTokenizer.encode_plus(
    text.split(' '),
    None,
    add_special_tokens = True,
    max_length = 512)
```
3. This should be the error
```
Traceback (most recent call last):
  File "classification.py", line 23, in <module>
    max_length = 512)
  File "D:\Programmering\Python\lib\site-packages\transformers\tokenization_utils.py", line 1576, in encode_plus
    first_ids = get_input_ids(text)
  File "D:\Programmering\Python\lib\site-packages\transformers\tokenization_utils.py", line 1556, in get_input_ids
    "Input is not valid. Should be a string, a list/tuple of strings or a list/tuple of integers."
ValueError: Input is not valid. Should be a string, a list/tuple of strings or a list/tuple of integers.
```

And yes, I've tried just inputting a string, and I still got the same error.

<!-- If you have code snippets, error messages, stack traces please provide them here as well.
     Important! Use code tags to correctly format your code. See https://help.github.com/en/github/writing-on-github/creating-and-highlighting-code-blocks#syntax-highlighting
     Do not use screenshots, as they are hard to read and (more importantly) don't allow others to copy-and-paste your code.-->

## Expected behavior

I want the encoder_plus function to return an encoded version of the input sequence.

## Environment info
<!-- You can run the command `transformers-cli env` and copy-and-paste its output below.
     Don't forget to fill out the missing fields in that output! -->
     
- `transformers` version: 2.11.0
- Platform: Windows
- Python version: 3.7.4
- PyTorch version (GPU?): 1.5.0+cpu
- Tensorflow version (GPU?): (Not used)
- Using GPU in script?: Nope
- Using distributed or parallel set-up in script?: No


Comments:
The mistake is on me. I forgot to download the tokenizer😂

I am getting the same error. What exactly do you mean by download the tokenizer? Doesn't it come with the transformers package?

I think what he meant was that use used the class, and not the instance, to encode text. You should always initialize the class:

```py
from transformers import BertTokenizer

tokenizer = BertTokenizer.from_pretrained("bert-base-cased")
# or
tokenizer = BertTokenizer(vocabfile)

# now you can encode
text = 'A quick brown fox jumps over' # Just a dummy text
model_inputs = tokenizer.encode_plus(text)
```

@LysandreJik, while I have you. I know this aint the right place to ask you, but.

I’ve seen that you’re about to release the Electra modeling for question answering, and I’ve written a small script for training the electra discriminator for question answering, and I’m about to train the model. 
so Would it be useful for you if I trained the model, or are you already doing that?

Hi @mariusjohan, we welcome all models here :) The [hub](https://huggingface.co/models) is a very easy way to share models. The way you're training it will surely be different to other trainings, so sharing it on the hub with details of how you trained it is always welcome!

Ok, this is still not working for me. I am running the run_squad.py script and I keep getting the error.

Traceback (most recent call last):
  File "/home/ubuntu/anaconda3/envs/pytorch_p36/lib/python3.6/multiprocessing/pool.py", line 119, in worker
    result = (True, func(*args, **kwds))
  File "/home/ubuntu/anaconda3/envs/pytorch_p36/lib/python3.6/multiprocessing/pool.py", line 44, in mapstar
    return list(map(*args))
  File "/kriviv-10T/transformers/transformers/src/transformers/data/processors/squad.py", line 142, in squad_convert_example_to_features
    return_token_type_ids=True,
  File "/kriviv-10T/transformers/transformers/src/transformers/tokenization_utils_base.py", line 1521, in encode_plus
    **kwargs,
  File "/kriviv-10T/transformers/transformers/src/transformers/tokenization_utils.py", line 356, in _encode_plus
    second_ids = get_input_ids(text_pair) if text_pair is not None else None
  File "/kriviv-10T/transformers/transformers/src/transformers/tokenization_utils.py", line 343, in get_input_ids
    f"Input {text} is not valid. Should be a string, a list/tuple of strings or a list/tuple of integers."
ValueError: Input [] is not valid. Should be a string, a list/tuple of strings or a list/tuple of integers.

The reason I got the error, was because I forgot to initialize the tokenize module, and therefore it thinks the self argument is the input_ids and then you’re not giving it the real input_ids argument. And ofc, the system was way complex than the example I gave, so maybe try to check how the tokenization module is giving. Maybe also check your inputs and so on if you haven’t already. Sadly I can first fix it in a few hours.

@vkrishnamurthy11 Did it help?

I'm still facing the same issue:
ValueError: Input [] is not valid. Should be a string, a list/tuple of strings or a list/tuple of integers.
While trying to run run_squad.py. I'm trying to train and test it with:
https://rajpurkar.github.io/SQuAD-explorer/dataset/train-v1.1.json
https://rajpurkar.github.io/SQuAD-explorer/dataset/dev-v1.1.json 
 

Facing the same issue as Sarang when training on Squad using run_squad.py . Is this a known bug?

Same issue here when running squad_convert_examples_to_features in my own code. 

I don't know if it helps, but the reason was because I failed to use _**.from_pretrained**_ function. Maybe check for that. So maybe print out the **_self_** argument

I resolved this issue by updating all packages I was using for training to the newest version. In my experience you need to have:
1.9.0 - torch
0.10.0 - torchtext
4.11.3 - transformers
or newer...

PS: You can check the version you are currently using with:
print(torch.__version__)
print(torchtext.__version__)
print(transformers.__version__)

> I resolved this issue by updating all packages I was using for training to the newest version. In my experience you need to have: 1.9.0 - torch 0.10.0 - torchtext 4.11.3 - transformers or newer...
> 
> PS: You can check the version you are currently using with: print(torch.**version**) print(torchtext.**version**) print(transformers.**version**)

import torch
import torchtext
import transformers
import numpy as np
import os
import collections

os.makedirs('./data', exist_ok=True)
train_dataset, test_dataset = torchtext.datasets.AG_NEWS(root='./data')
classes = ['World', 'Sports', 'Business', 'Sci/Tech']

train_dataset = list(train_dataset)
test_dataset = list(test_dataset)

bert_model = 'bert-base-uncased'

tokenizer = transformers.BertTokenizer.from_pretrained(bert_model)

MAX_SEQ_LEN = 128
PAD_INDEX = tokenizer.convert_tokens_to_ids(tokenizer.pad_token)
UNK_INDEX = tokenizer.convert_tokens_to_ids(tokenizer.unk_token)


def pad_bert(b):
    # b is the list of tuples of length batch_size
    #   - first element of a tuple = label,
    #   - second = feature (text sequence)
    # build vectorized sequence
    v = [tokenizer.encode(x[1]) for x in b]
    # compute max length of a sequence in this minibatch
    l = max(map(len, v))
    return (  # tuple of two tensors - labels and features
        torch.LongTensor([t[0] for t in b]),
        torch.stack([torch.nn.functional.pad(torch.tensor(t), (0, l - len(t)), mode='constant', value=0) for t in v])
    )


train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=8, collate_fn=pad_bert, shuffle=True)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=8, collate_fn=pad_bert)

model = transformers.BertForSequenceClassification.from_pretrained(bert_model, num_labels=4)

optimizer = torch.optim.Adam(model.parameters(), lr=2e-5)

report_freq = 50
iterations = 500  # make this larger to train for longer time!

model.train()

i, c = 0, 0
acc_loss = 0
acc_acc = 0

for labels, texts in train_loader:
    labels = labels - 1  # get labels in the range 0-3
    texts = texts
    loss, out = model(texts, labels=labels)[:2]
    labs = out.argmax(dim=1)
    acc = torch.mean((labs == labels).type(torch.float32))
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    acc_loss += loss
    acc_acc += acc
    i += 1
    c += 1
    if i % report_freq == 0:
        print(f"Loss = {acc_loss.item() / c}, Accuracy = {acc_acc.item() / c}")
        c = 0
        acc_loss = 0
        acc_acc = 0
    iterations -= 1
    if not iterations:
        break

model.eval()
iterations = 100
acc = 0
i = 0
for labels, texts in test_loader:
    labels = labels - 1
    texts = texts
    _, out = model(texts, labels=labels)[:2]
    labs = out.argmax(dim=1)
    acc += torch.mean((labs == labels).type(torch.float32))
    i += 1
    if i > iterations: break

print(f"Final accuracy: {acc.item() / i}")

> Ok, this is still not working for me. I am running the run_squad.py script and I keep getting the error.
> 
> Traceback (most recent call last): File "/home/ubuntu/anaconda3/envs/pytorch_p36/lib/python3.6/multiprocessing/pool.py", line 119, in worker result = (True, func(*args, **kwds)) File "/home/ubuntu/anaconda3/envs/pytorch_p36/lib/python3.6/multiprocessing/pool.py", line 44, in mapstar return list(map(*args)) File "/kriviv-10T/transformers/transformers/src/transformers/data/processors/squad.py", line 142, in squad_convert_example_to_features return_token_type_ids=True, File "/kriviv-10T/transformers/transformers/src/transformers/tokenization_utils_base.py", line 1521, in encode_plus **kwargs, File "/kriviv-10T/transformers/transformers/src/transformers/tokenization_utils.py", line 356, in _encode_plus second_ids = get_input_ids(text_pair) if text_pair is not None else None File "/kriviv-10T/transformers/transformers/src/transformers/tokenization_utils.py", line 343, in get_input_ids f"Input {text} is not valid. Should be a string, a list/tuple of strings or a list/tuple of integers." ValueError: Input [] is not valid. Should be a string, a list/tuple of strings or a list/tuple of integers.

Check whether your input is empty. I got similar errors and found when passing empty string to tokenizer, it will get this error.

hi, @mariusjohan, where this file? `vocabfile`

IS THERE A RESOLVE??!


## One Possible Reason for 'ValueError: Input is not valid' (caused by pandas)

In my case, I got this problem, because in the dataset, one sample row is 'None' (Type string).
However when I load dataset, **the pandas will automatically transform the str 'None' into Nonetype 'nan',**
and cause the ValueError when I doing tokenization. 
**Same problem will occur when you use huggingface load_dataset or dataset.from_csv function**, because they seems to actually use pandas for reading files.

Here is my code meet problem:
```
from datasets import Dataset
import pandas as pd
testset = pd.read_csv('./rotate_tomato/test.tsv', sep='\t')  
testset = Dataset.from_pandas(testset)
model = "distilbert-base-uncased"
tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')

def tokz(x):
    return tokenizer(x['Phrase'], padding=True, truncation=True, return_tensors="pt")

testset_tokz = testset.map(tokz, batched=True)
```

`ValueError: Input is not valid. Should be a string, a list/tuple of strings or a list/tuple of integers.`

I recommend to check if your dataset has special string like 'None', 'NA' etc....

## My solution

`testset = pd.read_csv('./rotate_tomato/test.tsv', sep='\t', keep_default_na=False)`

I simply set keep_default_na=False to prevent pandas to detect na values and transform then into Nonetype...
And then everything goes well.

> > Ok, this is still not working for me. I am running the run_squad.py script and I keep getting the error.
> > Traceback (most recent call last): File "/home/ubuntu/anaconda3/envs/pytorch_p36/lib/python3.6/multiprocessing/pool.py", line 119, in worker result = (True, func(*args, **kwds)) File "/home/ubuntu/anaconda3/envs/pytorch_p36/lib/python3.6/multiprocessing/pool.py", line 44, in mapstar return list(map(*args)) File "/kriviv-10T/transformers/transformers/src/transformers/data/processors/squad.py", line 142, in squad_convert_example_to_features return_token_type_ids=True, File "/kriviv-10T/transformers/transformers/src/transformers/tokenization_utils_base.py", line 1521, in encode_plus **kwargs, File "/kriviv-10T/transformers/transformers/src/transformers/tokenization_utils.py", line 356, in _encode_plus second_ids = get_input_ids(text_pair) if text_pair is not None else None File "/kriviv-10T/transformers/transformers/src/transformers/tokenization_utils.py", line 343, in get_input_ids f"Input {text} is not valid. Should be a string, a list/tuple of strings or a list/tuple of integers." ValueError: Input [] is not valid. Should be a string, a list/tuple of strings or a list/tuple of integers.
> 
> Check whether your input is empty. I got similar errors and found when passing empty string to tokenizer, it will get this error.

Yeah dropping the rows with empty str and nan values resolving this error🧑‍💻