MobileBert embedding vectors values
Author: datapaintings
Pull request: no
Labels: name: wontfix

# 🐛 Bug

## Information

Model I am using (Bert, XLNet ...): MobileBert

Language I am using the model on (English, Chinese ...): English

The problem arises when using:
* [ ] the official example scripts: (give details below) 
* [ ] my own modified scripts: (give details below)

```py
def BERT_embeddings(data, max_seq_len):
    tokenizer = MobileBertTokenizer.from_pretrained('google/mobilebert-uncased')
    model = MobileBertModel.from_pretrained('google/mobilebert-uncased')
    batches = np.array_split(data, 5)  
    encoded = None
    for batch in batches:
        encoded_text = tokenizer.batch_encode_plus(batch.values, max_length=max_seq_len, pad_to_max_length=True, truncation=True)
        input_ids = tf.constant(encoded_text['input_ids'])#[None, :]
        #attention_masks = encoded_data_train['attention_mask']
        outputs = model(input_ids)
        if encoded == None:
            encoded = outputs[1]
        else:
            encoded = np.concatenate((encoded, outputs[1]), axis=0)

    encoded_test_x = pd.DataFrame(data=encoded, index=test_x.index)
    return encoded_test_x

print(BERT_embeddings(data, max_seq_len=18)
```

The tasks I am working on is:
* [ ] an official GLUE/SQUaD task: (give the name)
* [ ] my own task or dataset: (give details below)
I would like to create sentence embeddings with MobileBert
## To reproduce

Steps to reproduce the behavior:

1. 'text' is one column pandas object 
2. run above code 
3.

<!-- 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.-->
Embeddings have a form of vectors with large values like [-24888514.0  222398.593750...]

## Expected behavior
Should have [-1, 1] range

<!-- A clear and concise description of what you would expect to happen. -->
I would expect that elements of a vector will not be in range hundred of thousands but [-1, 1]

## 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: 3.0.1
- Platform:
- Python version: 3.7.3
- PyTorch version (GPU?): -
- Tensorflow version (GPU?): 2.0.0
- Using GPU in script?: No
- Using distributed or parallel set-up in script?: No


Comments:
Hi! Firstly, in your code you're using a `MobileBertModel` with tensorflow inputs; does that work? You should either use torch inputs or use `TFMobileBertModel` instead.

The model outputs have no reason to be bound between [-1, 1], as they're logits. You can pass them through a softmax layer to get the total sum to be 1 and have each result bound between -1 and 1!

Hello, thank you for your answer. I modified the function according to your suggestion, applying softmax on outputs to get hidden states values:

```
def softmax(x):
    e_x = np.exp((x - np.max(x))/2000000)
    return e_x / e_x.sum(axis=0)

def BERT_embeddings2(test_x, max_seq_len):
    tokenizer = MobileBertTokenizer.from_pretrained('google/mobilebert-uncased')
    model = TFMobileBertModel.from_pretrained('google/mobilebert-uncased')
    batches = np.array_split(test_x, 100)  
    encoded = None
    for batch in batches:
        encoded_text = tokenizer.batch_encode_plus(batch.values, max_length=max_seq_len, pad_to_max_length=True, truncation=True)
        input_ids = tf.constant(encoded_text['input_ids'])#[None, :]
        outputs = model(input_ids)
        hidden_states = softmax(outputs[1])
        if encoded is None:
            encoded = hidden_states
        else:
            encoded = np.concatenate((encoded, hidden_states), axis=0)
```
Then I used encoded data to run experiment on multiple neural network architectures. What I observed, is that the loss function does not converge and is similar for various network shapes. This is not appearing while training on (not mobile) Bert Model. Do you know what can be the reason?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.