title: Speed up sentence processing by BERT in Transformers
body: <p>I have this code:</p>
  <pre class="lang-py prettyprint-override"><code>import torch
  import tensorflow as tf
  import numpy as np
  from transformers import AutoTokenizer, AutoModel
  
  model = 'bert-base-uncased' 
  tokenizer = AutoTokenizer.from_pretrained(model)
  model = AutoModel.from_pretrained(model)
  Sentence_vectorList = []
  for sent in x_train:
  
    input_sentence = torch.tensor(tokenizer.encode(sent)).unsqueeze(0)
    out = model(input_sentence)
    embeddings_of_last_layer = out[0]
    cls_embeddings = embeddings_of_last_layer[0]
  
    cls_layer = cls_embeddings.detach().numpy()
  
    sent_emd = np.average(cls_layer,axis=0)
  </code></pre>
  <p>The task is to take the sentence vectors and detach them in [n x 768] then I save them as sent2vec. This process is taking a lot of time. Is there a more efficient way to do it?</p>