When using dcgan for network training, the following errors occur:
RuntimeError: Found dtype Long but expected Float
The code snippet for this error is as follows:
label = torch.full((b_size,), real_label, device=device)
# Input the batch with positive samples into the discriminant network for forward computation and put the result into the variable output
output = netD(real_cpu).view(-1)
# Calculate the loss
errD_real = criterion(output, label)
The reason is that the data type of the input output data and tag value into the loss function does not match the required data type. What is required is float type data, and what is passed in is long type data
therefore, we need to convert the incoming data to float type
the modified code is as follows:
label = torch.full((b_size,), real_label, device=device)
# Input the batch with positive samples into the discriminant network for forward computation and put the result into the variable output
output = netD(real_cpu).view(-1)
# Convert the incoming data to float type
output = output.to(torch.float32)
label = label.to(torch.float32)
# Calculate the loss
errD_real = criterion(output, label)
Problem solved!
Read More:
- [Solved] RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and
- Libtorch Error: Expected object of type Variable but found type CUDALongType for argument #2 ‘index’
- [Solved] pytorch CrossEntropyLoss Error: RuntimeError: 1D target tensor expected, multi-target not supported
- [Solved] RuntimeError: 1only batches of spatial targets supported (non-empty 3D tensors) but got targets of s
- [Solved] TFrecords Create Datas Error: Number of int64 values != expected. Values size: 1 but output shape: [3]
- Numpy.exp Function Error ‘Float’ object has no attribute ‘exp’
- [Solved] Pytorch Error: RuntimeError: Trying to backward through the graph a second time
- RuntimeError: CUDNN_STATUS_EXECUTION_FAILED [How to Solve]
- error: expected ‘;‘ at end of member declaration and expected ‘)‘ before ‘&‘ toke Errors
- [Solved] OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.
- [Solved] RuntimeError: each element in list of batch should be of equal size
- error: library dfftpack has Fortran sources but no Fortran compiler found
- [Solved] RuntimeError: CUDA error: out of memory
- [Error] invalid operands to binary ^ (have ‘double‘ and ‘float‘)
- ValueError: Input 0 of node import/save/Assign was passed float from import/beta1_power:0 incompatib
- [Solved] Cmake compile opencv open-source project Error: but it set OpenCV_FOUND to FALSE so package “OpenCV” is considered to be…
- [Solved] MindSpore Error: “RuntimeError: Unable to data from Generator..”
- [Solved] OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.
- [Solved] torch Do Targer Detection Error: RuntimeError: CUDA error: device-side assert triggered
- [Solved] RuntimeError: cuDNN error: CUDNN_STATUS_INTERNAL_ERROR