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:
- Pandas Error: ValueError: setting an array element with a sequence.
- Autograd error in Python: runtimeerror: grad can be implicitly created only for scalar outputs
- [Solved] Python-selenium locates an element cannot be clicked error: ElementClickInterceptedException
- Pytorch Error: RuntimeError: value cannot be converted to type float without overflow: (0.00655336,-0.00
- How to Solve RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu
- Python time tuples are converted to timestamps, strings
- Python TypeError: not all arguments converted during string formatting [Solved]
- Pychar can’t connect to Python console, but it can run. Py file, and Anaconda’s command line can run Python command
- How to Solve Python Xlwt ValueError: More than 4094 XFs (styles)
- Python Valueerror: cannot index with vector containing Na / Nan values
- [Solved] python Error: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.
- [Solved] PyTorch Caught RuntimeError in DataLoader worker process 0和invalid argument 0: Sizes of tensors mus
- Python Selenium: element is not attached to the page document error
- [Solved] Pandas rename Error: ValueError: operands could not be broadcast together with shapes (1,2) (3,) (1,2)
- Python errors: valueerror: if using all scalar values, you must pass an index (four solutions)
- [Solved] TypeError: not all arguments converted during string formatting
- RuntimeWarning: overflow encountered in ubyte_Scalars pixel addition and subtraction overflow exception
- [Solved] Python Selenium Error: AttributeError: ‘WebDriver‘ object has no attribute ‘find_element_by_xpath‘
- Python synthesizes multiple pictures into one video
- Python 3.X error: valueerror: data type must provide an itemsize