Flatten 3D tensor

Viewed 134

I have a tensor of the shape T x B x N (training data for a RNN, T is max seq length, B is number of batches, and N number of features) and I'd like to flatten all the features across timesteps, such that I get a tensor of the shape B x TN. Haven't been able to figure out how to do this..

1 Answers

You need to permute your axes before flattening, like so:

t = t.swapdims(0,1) # (T,B,N) -> (B,T,N)
t = t.view(B,-1)    # (B,T,N) -> (B,T*N) (equivalent to `t.view(B,T*N)`)
Related