How to use transformers to  convert  batch sentences into word vectors???
Author: duan348733684
Pull request: no

## ❓ Questions & Help

<!-- A clear and concise description of the question. -->

How to use transformers to  convert  batch sentences into word vectors???


Comments:
I'm sorry but this is not how you should ask questions to begin with, second it is very general. There are tons of tutorials about this kind of stuff. You can have a look at a notebook that I made. It shows you how to get a feature vector for your input sentence. https://github.com/BramVanroy/bert-for-inference/blob/master/introduction-to-bert.ipynb

If that doesn't help you, post a question on a website like Stack Overflow or Google.

> I'm sorry but this is not how you should ask questions to begin with, second it is very general. There are tons of tutorials about this kind of stuff. You can have a look at a notebook that I made. It shows you how to get a feature vector for your input sentence. https://github.com/BramVanroy/bert-for-inference/blob/master/introduction-to-bert.ipynb
> 
> If that doesn't help you, post a question on a website like Stack Overflow or Google.

thank you! I understand what you mean.  I  can  use transformers to convert  a  single sentence into feature vector. Following:

string = "I like the girl"
tokens = tokenizer.tokenize(string)
ids = tokenizer.convert_tokens_to_ids(tokens)
tokens_tensor = torch.tensor([ids])
with torch.no_grad():
    outputs = model(tokens_tensor )

But  if   I  want   to  get   feature vector  about  ["I like the girl", "post a quesetion on a website", "I often use facebook"]  at once. Here are three sentences. How to use transformer? This is my main question.


You are not hearing what I am saying. You should ask this type of question on Stack Overflow and tag it with huggingface-transformers because it is a _general_ question. Below you can find a basic approach. Please close this question and direct your future questions like this to SO.

```python
import torch
from transformers import BertModel, BertTokenizer

tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertModel.from_pretrained('bert-base-uncased')
model.eval()

text = ['I like cookies.', 'Do you like cookies?']
encoded = tokenizer.batch_encode_plus(text, return_tensors='pt', add_special_tokens=True)
print(encoded)
# {'input_ids': tensor([[  101,  1045,  2066, 16324,  1012,   102,     0],
        [  101,  2079,  2017,  2066, 16324,  1029,   102]]), 'token_type_ids': tensor([[0, 0, 0, 0, 0, 0, 1],
        [0, 0, 0, 0, 0, 0, 0]]), 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1, 1]])}

with torch.no_grad():
    out = model(**encoded)

print(out[0].size())
# torch.Size([2, 7, 768])

```

> You are not hearing what I am saying. You should ask this type of question on Stack Overflow and tag it with huggingface-transformers because it is a _general_ question. Below you can find a basic approach. Please close this question and direct your future questions like this to SO.
> 
> ```python
> import torch
> from transformers import BertModel, BertTokenizer
> 
> tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
> model = BertModel.from_pretrained('bert-base-uncased')
> model.eval()
> 
> text = ['I like cookies.', 'Do you like cookies?']
> encoded = tokenizer.batch_encode_plus(text, return_tensors='pt', add_special_tokens=True)
> print(encoded)
> # {'input_ids': tensor([[  101,  1045,  2066, 16324,  1012,   102,     0],
>         [  101,  2079,  2017,  2066, 16324,  1029,   102]]), 'token_type_ids': tensor([[0, 0, 0, 0, 0, 0, 1],
>         [0, 0, 0, 0, 0, 0, 0]]), 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1],
>         [1, 1, 1, 1, 1, 1, 1]])}
> 
> with torch.no_grad():
>     out = model(**encoded)
> 
> print(out[0].size())
> # torch.Size([2, 7, 768])
> ```

thank you for your guidance!!!!   sorry, I'm just a high school student from Zimbabwe..  I'll close it  right now