Search code examples
pythondeep-learningneural-networkpytorchartificial-intelligence

How to fix "Boolean value of Tensor with more than one value is ambiguous"?


I am trying to create a Model, but i'm getting a RuntimeError "Boolean value of Tensor with more than one value is ambiguous". I've searched already on other posts about this but could find a working solution so here is my own try.

I am following a tutorial, which is working with the tutorial set of Data. The code looks as follows:

#%% import file

df = pd.read_csv('coordinates.csv', sep='\;', engine='python')
df

#%% cleanup

df=df[[ 'CATEGORY','LEFT_ANGLE']]
print(df)
df.apply(pd.to_numeric)

#%%

from tkinter import Variable
import numpy as np
from sklearn.preprocessing import MinMaxScaler


def sliding_windows(data, seq_length):
    x = []
    y = []

    for i in range(len(data)-seq_length-1):
        _x = data[i:(i+seq_length)]
        _y = data[i+seq_length]
        x.append(_x)
        y.append(_y)

    return np.array(x),np.array(y)

#%%

sc = MinMaxScaler()
training_data = sc.fit_transform(df)

seq_length = 4
x, y = sliding_windows(training_data, seq_length)

train_size = int(len(y) * 0.67)
test_size = len(y) - train_size

dataX = Variable(torch.Tensor(np.array(x)))
dataY = Variable(torch.Tensor(np.array(y)))

trainX = Variable(torch.Tensor(np.array(x[0:train_size])))
trainY = Variable(torch.Tensor(np.array(y[0:train_size])))

testX = Variable(torch.Tensor(np.array(x[train_size:len(x)])))
testY = Variable(torch.Tensor(np.array(y[train_size:len(y)])))

This is the DataFrame i want to use for training and the error:

[300 rows x 2 columns]
6
CATEGORY    LEFT_ANGLE
0   0   160
1   0   162
2   0   160
3   0   157
4   0   146
... ... ...
295 4   163
296 4   176
297 4   132
298 4   150
299 4   176
300 rows × 2 columns

Cleanup done

---------------------------------------------------------------------------

RuntimeError                              Traceback (most recent call last)

<ipython-input-12-2bfe2346f838> in <module>
      8 test_size = len(y) - train_size
      9 
---> 10 dataX = Variable(torch.Tensor(np.array(x)))
     11 dataY = Variable(torch.Tensor(np.array(y)))
     12 


    331             raise TypeError("name must be a string")
    332         global _varnum
--> 333         if not master:
    334             master = _default_root
    335         self._root = master._root()

RuntimeError: Boolean value of Tensor with more than one value is ambiguous

In the first place i dont understand what the class Variable is for and why the Tensor does not accept my array. I hope someone can help me. Thanks in advance and sorry if my englisch is not the best.


Solution

  • Short answer:
    Remove the Variable()

    Explanation:
    I believe your answer is in you question. If you don't know what Variable is, why are you using it? But more importantly you are importing it from tkinter which is an interface package and I'm pretty sure that's not what you want.

    What you want is to use the one from torch. I was looking for it in the doc but it is actually deprecated, the Variable() API from torch now returns a Tensor so it is not useful anymore. see here