UNQ_C6:get_disc_loss:zeros_like() received an invalid combination of arguments - got (tuple)

This is my code for question6,

noise=get_noise(num_images, z_dim, device)
fimage=gen(noise),
pred=disc(fimage),

def get_noise(n_samples, z_dim, device=‘cpu’):
return torch.randn(n_samples,z_dim).to(device)

And I got this error, could anyone help me with it?

33 noise=get_noise(num_images, z_dim, device),
—> 34 fimage=gen(noise),
TypeError: zeros_like() received an invalid combination of arguments - got (tuple), but expected one of:

  • (Tensor input, torch.memory_format memory_format, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)
  • (Tensor input, torch.memory_format memory_format, bool requires_grad)

Hi there @Wenhan !
I see a couple of things, I called the torch.randn() function, but passed the device to the function, instead of using the .to(device) that you did.

Also I noticed that i do a fimage.detach() as an argument to the discriminator function.

Hope that helps, let me know!

Hi @ gautamaltman!
I think I have revised it according to what you said, but I still get the same error

Hello @gautamaltman!
I think I can solve it. Just delete the comma outside the bracket. Can you explain it?

from

fimage=gen(noise),

to

fimage=gen(noise)

1 Like

Hi @Wenhan, great find!

Yes, you were unintentionally creating a Python tuple structure because of the commas at the end of the line. To explain this try the following sample code:

correctly assigning arrays to variables t1,t2

t1,t2 = [1,2,3],[2,3,4]

print(‘showing correct multiple variable assignment’)
print(t1)
print(t2)

generate tuples

fimage = [1,2,3],
pred = [2,3,4],

print(‘display tuples data structs’)
print(fimage)
print(pred)

print(‘indexing tuple to first element’)
print(fimage[0])
print(pred[0])

fimage = [1,2,3]
pred = [2,3,4]

print(‘display array data structs’)
print(fimage)
print(pred)

print(‘indexing array to first element’)
print(fimage[0])
print(pred[0])

Here is the output:

showing correct multiple variable assignment
[1, 2, 3]
[2, 3, 4]

display tuples data structs
([1, 2, 3],)
([2, 3, 4],)

indexing tuple to first element
[1, 2, 3]
[2, 3, 4]

display array data structs
[1, 2, 3]
[2, 3, 4]

indexing array to first element
1
2

Remember that Python does not require and line ending symbol like in C/C++. And be careful with your use of the comma!