Harmonic mean of two logits batches

Majid_Shirazi: <p>Hi, I have two batches, containing logits produced by classifiers. I like to produce another batch with the same size, but with harmonic mean of each pair of values.</p>
<pre><code class="lang-auto">The batch_A = tensor([[[[-0.39453]]],
        [[[-0.39018]]],
        [[[ 0.01991]]],
        [[[-0.40854]]],
        [[[-0.39593]]],
        [[[ 0.21211]]]], device='cuda:0', grad_fn=&lt;CudnnConvolutionBackward&gt;)

The batch_B = tensor([[-0.00552],
        [ 0.26541],
        [ 1.66024],
        [-0.33280],
        [ 0.35407],
        [ 0.55062]], device='cuda:0', grad_fn=&lt;AddmmBackward&gt;)

I like to have a new batch with harmonic mean of batch A and B, but the grad_fn=&lt;AddmmBackward&gt; or identical to batch_B
</code></pre>
<p>I first tried to just sum up two batches, it kinda worked, but I got grad_fn=CopySlices. by below code:</p>
<pre><code class="lang-auto">for ele in range(len(batch_B)): batch_B[ele] =  batch_B[ele] + batch_A [ele]
</code></pre>
<p>I tried to do similar but do harmoni mean of each element pairs</p>
<pre><code class="lang-auto">for ele in range(len(batch_B)): batch_B[ele] = (( batch_B[ele]*batch_A [ele])*2)/( batch_B[ele]+batch_A [ele])
</code></pre>
<p>But I got the error below:<br>
<strong>RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [1]], which is output 0 of SelectBackward, is at version 32; expected version 31 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).</strong></p>
<p>Really appreciate any help.</p>

mMagmer: <p>hi,</p>
<ol>
<li>c = concant(a,b) → torch.cat(a,b,dim=0)</li>
<li>((1/c).mean(dim=0))^(-1)</li>
</ol>
<p>also</p><aside class="onebox wikipedia" data-onebox-src="https://en.wikipedia.org/wiki/Harmonic_mean">
  <header class="source">

      <a href="https://en.wikipedia.org/wiki/Harmonic_mean" target="_blank" rel="noopener nofollow ugc">en.wikipedia.org</a>
  </header>

  <article class="onebox-body">
    

<h3><a href="https://en.wikipedia.org/wiki/Harmonic_mean" target="_blank" rel="noopener nofollow ugc">Harmonic mean</a></h3>

<p>In mathematics, the harmonic mean is one of several kinds of average, and in particular, one of the Pythagorean means. It is sometimes appropriate for situations when the average rate is desired.
 The harmonic mean can be expressed as the reciprocal of the arithmetic mean of the reciprocals of the given set of observations. As a simple example, the harmonic mean of 1, 4, and 4 is
 The harmonic mean H of the positive real numbers

  
    
      
        
          x
          
            1
      ...</p>

  </article>

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

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

<blockquote>
<p>The harmonic mean is also concave, which is an even stronger property than Schur-concavity. One has to take care to only use positive numbers though, since the mean fails to be concave <strong>if negative values are used</strong>.</p>
</blockquote>

Majid_Shirazi: <p>Many thanks for your reply. I did</p>
<pre><code class="lang-auto">Hmean = torch.cat(batch_A ,batch_B,dim=0) 
But got error below:

TypeError: cat() received an invalid combination of arguments - got (Tensor, Tensor, dim=int), but expected one of:
 * (tuple of Tensors tensors, int dim, *, Tensor out)
 * (tuple of Tensors tensors, name dim, *, Tensor out)
</code></pre>
<p>Regarding negative values, I will define a function to take abs values, and return the sign after Harmonic mean operation is done. However, the error above pops up, even if I make all values positive.<br>
Appreciate your support again.</p>

mMagmer: <p>hi, it was pseudo code you’re not suppose to run it as is.</p>
<pre><code class="lang-auto">c = torch.stack([batch_A,batch_B])
((1/c).mean(dim=0))**(-1)
</code></pre>
<p>this shoud work .</p>