title: How to generate a new Tensor with different vectors in PyTorch?
body: <p>I want to generate new a○b vector with <code>a</code> and <code>b</code> (○ means element wise multiply). My code is below, but the performance looks bad because of <code>for</code>. Are there any efficient way?</p>
  
  <pre><code>a = torch.rand(batch_size, a_len, hid_dim)
  b = torch.rand(batch_size, b_len, hid_dim)
  # a_elmwise_mul_b = torch.zeros(batch_size, a_len, b_len, hid_dim)
  for sample in range(batch_size):
      for ai in range(a_len):
          for bi in range(b_len):
              a_elmwise_mul_b[sample, ai, bi] = torch.mul(a[sample, ai], b[sample, bi])
  </code></pre>
  
  <h2>Update</h2>
  
  <p>I updated my code refer to Ahmad! Thank you.</p>
  
  <pre><code>N = 16
  hid_dim = 50
  a_seq_len = 10
  b_seq_len = 20
  a = torch.randn(N, a_seq_len, hid_dim)
  b = torch.randn(N, b_seq_len, hid_dim)
  shape = (N, a_seq_len, b_seq_len, hid_dim)
  
  a_dash = a.unsqueeze(2) # (N, a_len, 1,     hid_dim)
  b_dash = b.unsqueeze(1) # (N, 1,     b_len, hid_dim)
  a_dash = a_dash.expand(shape)
  b_dash = b_dash.expand(shape)
  print(a_dash.size(), b_dash.size())
  mul = a_dash * b_dash
  print(mul.size())
  ----------
  torch.Size([16, 10, 20, 50]) torch.Size([16, 10, 20, 50])
  torch.Size([16, 10, 20, 50])
  </code></pre>