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

I am a newbe and i am stumped … probably simply because i am missing something obvious.

Trying to show images of the kernels in a cnn layer.
The following works fine:

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
  col += 1

Now if i uncomment the “scaling” lines in the code above. My Jupiter notebook tosses the following:

TypeError                                 Traceback (most recent call last)
 in ()
     18 row = -1
---> 19 for j in range(16):
     20   if j%4 == 0:
     21     row += 1

TypeError: 'Tensor' object is not callable

Confusing as j there are no tensors on line 19.
printing kernels prior to the loop shows are “scaled” just fine.

Appreciate any assistance to shed light upon my ignorance.

Hi Marc!

This is really a python issue – not specific to pytorch.

range = max - min

This line (when uncommented) sets range to (refer to)
max - min, which, in this case, is a pytorch tensor.

Then when you try to execute

for j in range(16):

range no longer refers to the built-in python “range” function,
and the thing it does refer to (a pytorch tensor) is, indeed, not

Easy fix: Change the name of your max - min variable to
something else. Perhaps my_max_min_range_not_pythons?

The following pure python script shows this general behavior:

range = 'Marc'
print (range)
for  i in range (5):
    print (i)

(I consider this kind of thing to be a weakness in python. Who
has time to worry about tripping over such things?)

Best regards.

K. Frank

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 :blush: