IceCube
IceCube

Reputation: 425

Torch/Lua element wise multiplication of 2D and 1D tensors

I'm trying to preform element wise multiplication between 2D batch tensor(128x512) and 1D tensor(512).

Currently, I'm doing it in this why:

   nbatch = input:size(1)
   for i = 1 , nbatch , 1 do
      self.output[i]:cmul(self.noise)
   end

It works and I get expected results, but I think it is not the best efficient why to do it.

Can it be done more efficiently?

How can I extend it for nD tensors element wise multiplied with (n-1)D tensors ?

Thanks!

Upvotes: 1

Views: 464

Answers (1)

deltheil
deltheil

Reputation: 16121

self.output:cmul(self.noise:view(1, self.output:size(2)):expandAs(self.output))

Upvotes: 1

Related Questions