简体   繁体   中英

ValueError: too many dimensions 'str' python in pytorch

I keep getting the error:

ValueError: too many dimensions 'str'

I attach my colab notebook to have a look at it. Have not found anything online yet that helps me solve the problem.

link: https://colab.research.google.com/drive/1ikol2D8mmiIPKhNHbcFlTfVpuU_Gf9BZ?usp=sharing

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 (ie 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 check your notebook yet but I just solved mine with 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 training loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM