【发布时间】:2022-01-01 20:18:02
【问题描述】:
我有以下 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)
我从以下代码中得到:
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)
如何选择第二行?我会做 vec[1] 但这显然行不通。
【问题讨论】:
-
检查数组的形状。它看起来像
(1,3,n)。索引时需要考虑初始大小 1 维度。
标签: python numpy numpy-ndarray