Combine the permutation/combination of two tensors in new tensor

username: phisku
cooked: <p>First up, apologies for the confusing title… as for lack of a better wording, ill explain in numbers:</p>
<p>I have given two tensors<br>
t1 = [[1,2,3], [2,3,4]]<br>
t2 = [[5,6],[6,7],[7,8]]</p>
<p>further i have a function f(x,y) that takes one element of each tensor (e.g. t1[0] and t2[0]) and outputs some value.</p>
<p>What I now aim for, is an efficient way of building a new tensor t3, such that t3[0,1] = f(t1[0], t2[1])</p>
<p>[[f([1,2,3],[5,6]), f([1,2,3],[6,7]), f([1,2,3],[7,8])],<br>
[f([2,3,4],[5,6]), f([2,3,4],[6,7]), f([2,3,4],[7,8])]]</p>
<p>Naturally I can solve this via a for loop but I have the feeling that there is a cool tensor magic way.</p>
<pre><code>t3 = torch.zeros((len(t1), len(t2)))
for i in range((len(t1)):
    for j in range(len(t2)):
        t3[i,j] = f(t1[i], t2[j])
</code></pre>
<p>I think it would already be a huge help to get to know a way to just have t3 containing the raw values of t1 and t2 at the appropriate position. (I guess the function might then be easily applicable later on)</p>
<p>Thanks a lot in advance!</p>

username: charan_Vjy
cooked: <p>Is the function arbitarary or is it fixed?</p>

username: phisku
cooked: <p>the function is fixed and returns a scalar value.</p>

username: charan_Vjy
cooked: <p>It is imperative that you first rewrite the function to take in t1 and t2 and give a tensor as an output rather than a scalar. Can you print out the function?</p>

username: phisku
cooked: <p>The actual function is rather complex but for the sake of this example we might assume it to be something like</p>
<pre><code class="lang-auto">def f(x,y):
    return torch.sum(x) + torch.sum(y)
</code></pre>
<p>where x in en element of t1 and y is an element of t2</p>

username: Santosh_Gupta
cooked: <p>Did you ever get an answer? In numpy something called np.meshgrid is used.</p>
<aside class="onebox stackexchange" data-onebox-src="https://stackoverflow.com/questions/1208118/using-numpy-to-build-an-array-of-all-combinations-of-two-arrays/35608701#35608701">
  <header class="source">

      <a href="https://stackoverflow.com/questions/1208118/using-numpy-to-build-an-array-of-all-combinations-of-two-arrays/35608701#35608701" target="_blank" rel="noopener nofollow ugc">stackoverflow.com</a>
  </header>

  <article class="onebox-body">
      <a href="https://stackoverflow.com/users/2487184/ct-zhu" target="_blank" rel="noopener nofollow ugc">
    <img alt="CT Zhu" src="https://www.gravatar.com/avatar/d8cd1dea84cfe8312c532088a0da08e8?s=256&amp;d=identicon&amp;r=PG" class="thumbnail onebox-avatar" width="256" height="256">
  </a>

<h4>
  <a href="https://stackoverflow.com/questions/1208118/using-numpy-to-build-an-array-of-all-combinations-of-two-arrays/35608701#35608701" target="_blank" rel="noopener nofollow ugc">Using numpy to build an array of all combinations of two arrays</a>
</h4>

<div class="tags">
  <strong>python, arrays, numpy, multidimensional-array</strong>
</div>

<div class="date">
  
  answered by
  <a href="https://stackoverflow.com/users/2487184/ct-zhu" target="_blank" rel="noopener nofollow ugc">
    CT Zhu
  </a>
  on <a href="https://stackoverflow.com/questions/1208118/using-numpy-to-build-an-array-of-all-combinations-of-two-arrays/35608701#35608701" target="_blank" rel="noopener nofollow ugc">05:14PM - 24 Feb 16 UTC</a>
</div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>So in pytorch, it would be</p>
<p>torch.stack(<br>
torch.meshgrid(x, y)<br>
).T.reshape(-1,2)</p>