For j in range(16) >>> 'Tensor' object is not callable

username: inquest
cooked: <p>I am a newbe and i am stumped … probably simply because i am missing something obvious.</p>
<h2>Trying to show images of the kernels in a cnn layer.<br>
The following works fine:</h2>
<pre>
kernels = model.cnn1.weights.detach().clone().cpu().view(-1, 3, 3)

#max = torch.max(kernels)
#min = torch.min(kernels)
#range = max - min
#kernels = (kernels - min) / range

kernels = kernels.numpy()

fig, ax = plt.subplots(4, 4)


row = -1
for j in range(16):
  if j%4 == 0:
    row += 1
    col = 0
  ax[row][col].imshow(kernels[j]).set_cmap('Greys')
  ax[row][col].axis('off')
  col += 1

</pre>
<p>Now if i uncomment the “scaling” lines in the code above. My Jupiter notebook tosses the following:</p>
<pre>
TypeError                                 Traceback (most recent call last)
 in ()
     17 
     18 row = -1
---&gt; 19 for j in range(16):
     20   if j%4 == 0:
     21     row += 1

TypeError: 'Tensor' object is not callable
</pre>
<p>Confusing as j there are no tensors on line 19.<br>
printing kernels prior to the loop shows are “scaled” just fine.</p>
<p>Appreciate any assistance to shed light upon my ignorance.</p>

username: KFrank
cooked: <p>Hi Marc!</p>
<aside class="quote no-group quote-modified" data-username="inquest" data-post="1" data-topic="44744" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/inquest/48/11916_2.png" class="avatar"> inquest:</div>
<blockquote>
<p>I am a newbe and i am stumped … probably simply because i am missing something obvious.<br>
…</p>
<pre>...
#range = max - min
...
for j in range(16):
...
</pre>
<p>Now if i uncomment the “scaling” lines in the code above. My Jupiter notebook tosses the following:</p>
<pre>TypeError                                 Traceback (most recent call last)
 in ()
...
---&gt; 19 for j in range(16):
...
TypeError: 'Tensor' object is not callable
</pre>
<p>Confusing as j there are no tensors on line 19.<br>
printing kernels prior to the loop shows are “scaled” just fine.</p>
<p>Appreciate any assistance to shed light upon my ignorance.</p>
</blockquote>
</aside>
<p>This is really a python issue – not specific to pytorch.</p>
<pre><code class="lang-auto">range = max - min
</code></pre>
<p>This line (when uncommented) sets <code>range</code> to (refer to)<br>
<code>max - min</code>, which, in this case, is a pytorch tensor.</p>
<p>Then when you try to execute</p>
<pre><code class="lang-auto">for j in range(16):
</code></pre>
<p><code>range</code> no longer refers to the built-in python “range” function,<br>
and the thing it does refer to (a pytorch tensor) is, indeed, not<br>
callable.</p>
<p>Easy fix:  Change the name of your <code>max - min</code> variable to<br>
something else.  Perhaps <code>my_max_min_range_not_pythons</code>?</p>
<p>The following pure python script shows this general behavior:</p>
<pre><code class="lang-auto">range = 'Marc'
print (range)
for  i in range (5):
    print (i)
</code></pre>
<p>(I consider this kind of thing to be a weakness in python.  Who<br>
has time to worry about tripping over such things?)</p>
<p>Best regards.</p>
<p>K. Frank</p>

username: inquest
cooked: <p>thanks Frank! … python is a new language to me … really appreciate the assist … not a typical behavior of all the other languages i have encountered <img src="https://discuss.pytorch.org/images/emoji/apple/blush.png?v=9" title=":blush:" class="emoji" alt=":blush:"></p>