I am trying to apply a function to 3d torch tensor while the function is applied to 2d tensor which is read through the axis 1 of the 3d torch tensor.
For example, I have a torch tensor of the shape (51, 128, 20100) (a variable with name autoencode_logprob) and the function(rawid2sentence) runs on the input of the shape (51, 20100).
Right now I wrote the code to run with naive for loop, looping one by one with range(128).
However, it’s too slow. Following is the code part that matters.
autoencode_logprobs is the 3d tensor and I need to apply rawids2sentence function along its second axis. Any help to vectorize it?
for i in range(128):
output_sent = self.dictionary.rawids2sentence(
autoencode_logprobs[:, i].max(1)[
1].data.cpu().numpy(),
oov_dicts[i],
)
output_sent_encoding = ifst_model.encode([output_sent])