Both real_A and label_A are the images and labels that I loaded using ImageFolder. They should be in the form of tensors. So do I no longer need to use Variables to convert to variables?But I see that there are steps to convert variables in many similar codes. If I remove it, will pred or label_A still be able to enter the built network smoothly?
Can you tell me how to modify the code? Thank you!
I originally wanted to use squeeze_ to convert the label into a one-dimensional tensor, but now I find that this is not necessary. So I removed squeeze_ and did not report the above error again.This is the label_A that I printed after removing squeeze_. Can it be used directly without the need to transform the form?
nn.MSELoss warns you, if your output and target shapes are different, as broadcasting will be applied, which might yield wrong results. If your output has the size [batch_size, 1], make sure to pass the target is the same shape. Currently dim1 seems to be missing, so you might want to call unsqueeze(1) on the target.
The input tensor seems to be on the CPU, while a CUDATensor is expected, so you would need to push the tensor to the device.
PS: It’s better to post code snippets by wrapping them into three backticks ```, as it makes debugging easier.
2. In other words, if CUDA is available, then I need to push the input tensor from the CPU to the GPU. If CUDA is not used, the input tensor on the CPU can directly enter the network without the need to do Variable (tensor) processing, because the effect is the same, right?
PS: In addition to the code snippets, should the result of the operation also be wrapped into three backticks ```? To replace those pictures?
Yes, exactly. Your target has the shape , while the output uses [1, 1], which will raise this warning. Just to make sure to avoid future issues, use label_real = label_real.unsqueeze(1) before passing it to criterion_GAN.
You have to push the model as well as all inputs to the right device. If you are using the GPU, you would thus have to transfer the data via x = x.to('cuda') or x = x.cuda(). To transfer it back, use x = x.to('cpu'). Variables are not needed anymore at all, neither for CUDA operations nor for CPU ops.
Yes, please post all code snippets and the outputs as formatted code, as it’s easier to debug and the forum search can index it. Pictures are not always easy to read, cannot be used to copy the source code for debugging, and the search won’t index it.
Thank you very much for your patience and detailed help! I understand most of what you said and solved the problem 1.
For question 2, I want to confirm whether the inputs form CPU is tensor or Variable(tensor) before using cuda? Or the two forms are the same, because I understand at what version it seems that Variable and tensor are merged?