<p dir="auto">Test case (the order is important):</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="print(type(numpy.exp(1) + Variable(torch.zeros(2, 5))))
# &lt;class 'numpy.ndarray'&gt; (of really strange shape)"><pre class="notranslate"><code class="notranslate">print(type(numpy.exp(1) + Variable(torch.zeros(2, 5))))
# &lt;class 'numpy.ndarray'&gt; (of really strange shape)
</code></pre></div>
<p dir="auto">I understand why this is happening but I'd prefer an error to be thrown here if possible when the "expected" behaviour cannot be achieved.</p>
<p dir="auto">Pytorch shows the following inconsistent behaviour.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
import torch
from torch.autograd import Variable

1.0 + Variable(torch.ones(1)) 
# returns as expected
# Variable containing:
#  2
# [torch.FloatTensor of size 1]

np.sum(1.0) + Variable(torch.ones(1)) 
# returns an unexpected (depth of array is 32)
# array([[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[Variable containing:
#  2
# [torch.FloatTensor of size 1]
# ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]], dtype=object)

# Switching their order 
Variable(torch.ones(1)) + np.sum(1.0)
# returns the expected
# Variable containing:
#  2
# [torch.FloatTensor of size 1]"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-k">import</span> <span class="pl-s1">torch</span>
<span class="pl-k">from</span> <span class="pl-s1">torch</span>.<span class="pl-s1">autograd</span> <span class="pl-k">import</span> <span class="pl-v">Variable</span>

<span class="pl-c1">1.0</span> <span class="pl-c1">+</span> <span class="pl-v">Variable</span>(<span class="pl-s1">torch</span>.<span class="pl-en">ones</span>(<span class="pl-c1">1</span>)) 
<span class="pl-c"># returns as expected</span>
<span class="pl-c"># Variable containing:</span>
<span class="pl-c">#  2</span>
<span class="pl-c"># [torch.FloatTensor of size 1]</span>

<span class="pl-s1">np</span>.<span class="pl-en">sum</span>(<span class="pl-c1">1.0</span>) <span class="pl-c1">+</span> <span class="pl-v">Variable</span>(<span class="pl-s1">torch</span>.<span class="pl-en">ones</span>(<span class="pl-c1">1</span>)) 
<span class="pl-c"># returns an unexpected (depth of array is 32)</span>
<span class="pl-c"># array([[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[Variable containing:</span>
<span class="pl-c">#  2</span>
<span class="pl-c"># [torch.FloatTensor of size 1]</span>
<span class="pl-c"># ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]], dtype=object)</span>

<span class="pl-c"># Switching their order </span>
<span class="pl-v">Variable</span>(<span class="pl-s1">torch</span>.<span class="pl-en">ones</span>(<span class="pl-c1">1</span>)) <span class="pl-c1">+</span> <span class="pl-s1">np</span>.<span class="pl-en">sum</span>(<span class="pl-c1">1.0</span>)
<span class="pl-c"># returns the expected</span>
<span class="pl-c"># Variable containing:</span>
<span class="pl-c">#  2</span>
<span class="pl-c"># [torch.FloatTensor of size 1]</span></pre></div>