0

我正在尝试从“cls_encoding”对象(bert 输出)构建数据集。

print (cls_encoding.shape)
(1, 768)

我有 30 个 cls_encoding 所以我想制作一个形状为 (30,768) 的数组。

当我尝试时:

print(cls_encoding[0].shape)
(768,)

所以如果我尝试这样做:

DSlist = []
for x in DS:
  DSlist.append(x[0])
print(np.array(DSlist).shape)

我得到:

(30, 768, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)

我怎样才能从这个得到想要的形状(30,768)?

4

1 回答 1

1

如果您有一个数组列表(长度为 30),其中每个数组都具有形状(1,768),您可以通过以下方式将该列表转换为具有形状的数组(30,768)

# build a sytetic dataset
DS = []
for i in range(30):
    DS.append(np.array([np.random.rand(768)]))


# transform the list to a array

DS_arr = np.asarray(DS)

print(DS_arr.shape)

DS_arr = np.squeeze(DS_arr)

print(DS_arr.shape)
>>> (30, 1, 768)
>>> (30, 768)
于 2019-11-13T15:22:43.923 回答