简体   繁体   English

从 numpy ndarray 调用行

[英]Call row from numpy ndarray

I have the following numpy.ndarray:我有以下 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?我怎样才能 select 第二排? I would do vec[1] but this doesn't work, apparently.我会做 vec[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 ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM