ValueError: too many dimensions 'str' python in pytorch

Viewed 2587
2 Answers

I've seen this error as well in my Jupyter notebook. I can reproduce it with the following simple code:

Input:
tensor(['a'])

Output: 
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-55-3bdb0dbfafc2> in <module>
----> 1 tensor(['a'])
...(stacktrace)...

ValueError: too many dimensions 'str'

Apparently PyTorch tensors are different from Numpy arrays in that they don't work with strings, only integers, floats, and booleans.

The above error indicates that there are too many strings passed to the tensor (i.e. even one string is too many). When I change the code to the following, it works fine:

Input:
tensor([1])

Output: 
tensor([1])

I haven't checked your notebook yet, but I just solved mine with the same error. Just double check if all training datasets and labels are converted to numeric values or tensors. If you have multiple columns in your dataframe, remove the one that does not need to feed into the training loop.

Related