'RunTime error only when adding Button: "Boolean value of Tensor with more than one valie is ambiguous"

after studied a few posts here in the forum I didn't figure out, why this error eccours when adding a Button class in which the procedure shall be executed.

I have this part of code:

Tensor_1 = torch.from_numpy(Input1)
Tensor_2 = torch.from_numpy(Input2)
Tensor_3 = torch.from_numpy(Input3)
Tensor_4 = torch.from_numpy(Input4)
Tensor_5 = torch.from_numpy(Input5)
Tensor_6 = torch.from_numpy(Input6)
Tensor_Out = torch.from_numpy(coordinates.transpose())

Input_list = [Tensor_1, ..., Tensor_6]

for eingabe in Input_list:
   Input, Output = Variable(eingabe).float(), Variable(Tensor_Output).float()

If I execute it alone, works totally fine. If I place it in a function like:

def calc_tensors():
   all of the above

it also works fine.

But if I add a Button from tkinter and try to execute this code with the button command, even before the button is pressed the RuntimeError occurs.

class Calc_proj(Button):
   def calc_tensors():
       all of the above

I am pretty new to Python but I cannot understand with my actual knowledge, what makes the difference to create this Error.

  Input, Output = Variable(eingabe).float(), Variable(Tensor_Output).float()
File "...tkinter\__init__.py", line 345, in __init__ if not master:
RuntimeError: Boolean value of Tensor with more than one value is ambiguous

Can someone please help me out?

Thanks in advance!!



Solution 1:[1]

I was told, that you have to convert the tensor into a cariable that pytorch can work with.

When I place that command within the Button class, somehow it doesn't work. Delete the Variable command and everything is fine.

So change the line:

Input, Output = Variable(eingabe).float(), Variable(Tensor_Output).float()

to

Input, Output = eingabe.float(), Tensor_Output.float()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 William Baker Morrison