PyTorch - RuntimeError: bool value of Tensor with more than one value is ambiguous
I trained a GAN on the MNIST dataset, and I'm trying to make a very simple UI that has a button to generate and display new images. When I press a button I make a call to the generator and pass a new latent vector to the forward method and keep getting this error message.

def update_picture():
    print('press')
    _, img = netG.forward(create_noise(1))
    img = img.detach().cpu().numpy()[0][0]
    img = ((img - img.min()) * (1 / img.max() - img.min()) * 255)
    photo = ImageTk.PhotoImage(image=Image.fromarray(img))
    label = Label(image=photo).grid(row=0, column=0)


tk = Tk()

photo = ImageTk.PhotoImage(image=Image.fromarray(img))
label = Label(image=photo).grid(row=0, column=0)

create = Button(text="update", command=update_picture).grid(row=1, column=0)

tk.mainloop()


And when I press the button to generate a new picture I keep getting this error:

Traceback (most recent call last):
  File "C:\Users\daman\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
  File "C:/Users/daman/PycharmProjects/untitled4/DCGAN_MNIST.py", line 243, in update_picture
_, img = netG.forward(create_noise(1))
  File "C:/Users/daman/PycharmProjects/untitled4/DCGAN_MNIST.py", line 104, in create_noise
return Variable(torch.zeros(b, feature_space, 1, 1).normal_(0, 1))
  File "C:\Users\daman\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 315, in __init__
if not master:
RuntimeError: bool value of Tensor with more than one value is ambiguous


The error traces back to my create noise function:

def create_noise(b):
    return Variable(torch.zeros(b, feature_space, 1, 1).normal_(0, 1))


Any ideas why this is happening and what does that error actually mean? I can post more code if needed.

I think I got the problem.

Variable is a name reserved in torch and tkinter. If you are doing from ... import * you may get Variable from tkinter. Since the error is comming from this line, the Variable in your code is from tkinter. However, since you are calling it with a Tensor inside, I'm guessing that you wanted the deprecated version of torch's Variable.

Simply removing Variable in create_noise should do the work.

def create_noise(b):
    return torch.zeros(b, feature_space, 1, 1).normal_(0, 1)

