Home > Net >  Call row from numpy ndarray
Call row from numpy ndarray

Time:11-23

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.

CodePudding user response:

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 ]
  • Related