簡體   English   中英

塊狀(n,1,m)至(n,m)

[英]Numpy (n, 1, m) to (n,m)

我正在研究一個涉及19個令牌的批次,每個令牌具有400個功能。 將兩個大小為(1,200)的向量連接到最終特征向量時,得到的形狀為(19,1,400)。 如果我將1擠出,則剩下(19,),但我嘗試獲得(19,400)。 我嘗試過轉換為列表,壓縮和整理,但沒有任何效果。

有沒有辦法將此數組轉換為正確的形狀?

def attn_output_concat(sample):
  out_h, state_h = get_output_and_state_history(agent.model, sample)
  attns = get_attentions(state_h)
  inner_outputs = get_inner_outputs(state_h)
  if len(attns) != len(inner_outputs):
    print 'Length err'
  else:
    tokens = [np.zeros((400))] * largest
    print(tokens.shape)
    for j, (attns_token, inner_token) in enumerate(zip(attns, inner_outputs)):
      tokens[j] = np.concatenate([attns_token, inner_token], axis=1)
    print(np.array(tokens).shape)
    return tokens

最簡單的方法是將標記聲明為以numpy.shape =(19,400)開頭的數組。 這樣還可以提高內存/時間效率。 這是修改后的代碼的相關部分...

import numpy as np
attns_token = np.zeros(shape=(1,200))
inner_token = np.zeros(shape=(1,200))
largest = 19
tokens = np.zeros(shape=(largest,400))
for j in range(largest):
    tokens[j] = np.concatenate([attns_token, inner_token], axis=1)
print(tokens.shape)

順便說一句...如果您不包含獨立且可運行的代碼段,這將使人們難以為您提供幫助(這可能就是為什么您尚未對此做出回應的原因)。 最好使用上面的代碼片段,它可以幫助您獲得更好的答案,因為您對要完成的目標的猜測較少。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM