简体   繁体   中英

Numpy (n, 1, m) to (n,m)

I am working on a problem which involves a batch of 19 tokens each with 400 features. I get the shape (19,1,400) when concatenating two vectors of size (1, 200) into the final feature vector. If I squeeze the 1 out I am left with (19,) but I am trying to get (19,400). I have tried converting to list, squeezing and raveling but nothing has worked.

Is there a way to convert this array to the correct shape?

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

The easiest way would be to declare tokens to be a numpy.shape=(19,400) array to start with. That's also more memory/time efficient. Here's the relevant portion of your code revised...

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)

BTW... It makes it difficult for people to help you if you don't include a self-contained and runnable segment of code (which is probably why you haven't gotten a response on this yet). Something like the above snippet is preferred and will help you get better answers because there's less guessing at what your trying to accomplish.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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