2

I have the following numpy.ndarray:

array([[[-0.34772965, -0.08028811, -0.02384451, ..., -0.14809863,
          0.34251794,  0.38363418],
        [-0.10257925, -0.17833571, -0.09449598, ...,  0.06461751,
          0.6166984 ,  0.5700328 ],
        [ 0.9105252 ,  0.19411758, -0.4067452 , ...,  0.09065486,
         -0.539338  , -0.04183678]]], dtype=float32)

Which I got from the following code:

from transformers import DistilBertTokenizer, DistilBertModel

tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')
model = DistilBertModel.from_pretrained("distilbert-base-uncased")

text = "cat"
encoded_input = tokenizer(text, return_tensors='pt')
output = model(**encoded_input)

vec = output.last_hidden_state.detach().numpy()

print(vec)

How can I select the second row? I would do vec[1] but this doesn't work, apparently.

1
  • Check the shape of the array. It looks like (1,3,n). You need to take that initial size 1 dimension into account when indexing. Commented Nov 23, 2021 at 17:28

1 Answer 1

1

There are some nested array in your case.

vec = [[[-0.34772965, -0.08028811, -0.02384451, ..., -0.14809863,
          0.34251794,  0.38363418],
        [-0.10257925, -0.17833571, -0.09449598, ...,  0.06461751,
          0.6166984 ,  0.5700328 ],
        [ 0.9105252 ,  0.19411758, -0.4067452 , ...,  0.09065486,
         -0.539338  , -0.04183678]]]

vec[0] = [[-0.34772965, -0.08028811, -0.02384451, ..., -0.14809863,
          0.34251794,  0.38363418],
        [-0.10257925, -0.17833571, -0.09449598, ...,  0.06461751,
          0.6166984 ,  0.5700328 ],
        [ 0.9105252 ,  0.19411758, -0.4067452 , ...,  0.09065486,
         -0.539338  , -0.04183678]]

vec[0][1] = [-0.10257925, -0.17833571, -0.09449598, ...,  0.06461751,
          0.6166984 ,  0.5700328 ]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.