This error occurred in Python.
At first, the reason is that I want to change the list with a tensor to a tensor type, that is, [tensor (), tensor ()] to a tensor, and then I write like this,
a = torch.randn(1,2) # tensor([[-0.4962, 0.6034]])
d = [a, a, a] # [tensor([[-0.4962, 0.6034]]), tensor([[-0.4962, 0.6034]]), tensor([[-0.4962, 0.6034]])]
d = torch.tensor(d)
It’s a mistake. ValueError: only one element tensors can be converted to Python scalars
I see a solution on the Internet,
val= torch.tensor([item.cpu().detach().numpy() for item in val]).cuda()
This method is very unsophisticated and concise.
Another way is to use torch. Cat, which is very concise. If you want to expand dimensions, you can use operations such as unsqueeze on this basis.
d = torch.cat(d, 0)
'''
Output: tensor([[-0.4962, 0.6034],
[-0.4962, 0.6034],
[-0.4962, 0.6034]])
'''
Read More:
- Pytorch directly creates a tensor on the GPU error [How to Solve]
- torch.nn.functional.normalize() Function Interpretation
- Here is the difference and connection of Torch. View (), Transpose (), and Permute ()
- Python: Torch.nn.functional.normalize() Function
- How to Solve Error: RuntimeError CUDA out of memory
- Python: RNN principle realized by numpy
- Pytorch: error message with chunks of 0 [How to Solve]
- [Solved] PyTorch Caught RuntimeError in DataLoader worker process 0和invalid argument 0: Sizes of tensors mus
- Autograd error in Python: runtimeerror: grad can be implicitly created only for scalar outputs
- The lenet model trained by Python failed to predict its own handwritten pictures
- [Solved] Pytorch-transformers Error: AttributeError: ‘str‘ object has no attribute ‘shape‘
- [Solved] RuntimeError: cuda runtime error (100) : no CUDA-capable device is detected at
- Apex install error: the environment is not compatible
- [Solved] Pytorch Tensor to numpy error: RuntimeError: Can‘t call numpy() on Tensor that requires grad.报错
- Error:output with shape [1, 224, 224] doesn‘t match the broadcast shape [3, 224, 224]
- [Solved] RuntimeError: function ALSQPlusBackward returned a gradient different than None at position 3, but t
- Pytorch CUDA Error: UserWarning: CUDA initialization: CUDA unknown error…
- [Solved] RuntimeError: CUDA error: CUBLAS_STATUS_EXECUTION_FAILED when calling `cubla…
- RuntimeError: CUDA error: an illegal memory access was encountered
- Pytorch: How to Handle error warning conda.gateways.disk.delete:unlink_or_rename_to_trash(140)