PyTorch TypeError: new(): 将嵌套列表转换为张量时,数据类型 'str' 无效?

Shi*_*abK 5 python arrays numpy pandas pytorch

我尝试从嵌套列表创建 Pytorch 张量,但出现这个奇怪的错误

data = pd.read_csv('TrackDataNormalized.csv')

#Get needed columns from csv
X = data.iloc[:, 0]
y = data.iloc[:, 1]

#Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

#each element in X_train is a list, which is converted to a string when read from a csv, #so we convert it back

X_train = X_train.to_numpy(X_train)
y_train = y_train.to_numpy(y_train)
X_test = X_test.to_numpy(X_test)
y_test = y_test.to_numpy(y_test)

old_train = X_train
X_train = []
for i in range(len(old_train)):
    X_train.append(ast.literal_eval(old_train[i,]))


old_test = X_test
X_test = []
for i in range(len(old_test)):
    X_test.append(ast.literal_eval(old_test[i,]))

#Show input
print(X_train[:2])

tensor = tf.tensor(X_train)

Run Code Online (Sandbox Code Playgroud)

这给了我这个错误

[[26, 24, 22, 23, 25, 24, 22, 19, 20, 20, 22, 22, 19, 21, 22, 22, 20, 22, 20, 20, 21, 20, 20, 23, 26, 18, 19, 20, 18, 20, 20, 20, 19, 21, 20, 20, 19, 20, 19, 19, 27, 21, 19, 19, 19, 19, 19, 19, 22, 23, 21, 19, 19, 19, 19, 19, 19, 22, 19, 18, 18, 19, 19, 19, 23, 19, 18, 21, 19, 19, 20, 19, 20, 21, 18, 18, 17, 19, 18, 19, 21, 18, 19, 19, 19, 18, 19, 18, 19, 21, 18, 17, 19, 19, 19, 20, 18, 20, 19, 21, 20, 18, 19, 19, 19, 20, 18, 18, 16, 20, 21, 19, 31, 26, 36, 45, 52, 49, 63, 44, 43, 44, 38, 48, 44, 35, 46, 47, 51, 51, 48, 57, 47, 49, 46, 49, 44, 47, 52, 48, 48, 38, 44, 49, 48, 43, 48, 44, 44, 47, 51, 40, 39, 44, 44, 44, 41, 51, 43, 43, 48, 44, 42, 43, 51, 44, 39, 43, 47, 44, 36, 39, 36, 43, 54, 39, 43, 48, 43, 44, 37, 44, 48, 43, 41, 45, 49, 47, 43, 45, 48, 34, 43, 48, 44, 48, 45, 44, 44, 45, 43, 44, 43, 44, 35, 44, 39, 45, 34, 44, 51, 44, 44, 43, 38, 43, 37, 32, 39, 39, 49, 41, 48, 38, 1, 2, 3, 1, 1, 4, 0, 0, 1, 1, 2, 1, 0, 1, 2, 0, 1, 0, 0, 2, 0, 0, 1, 1, 1, 0, 0, 2, 1, 4, 3, 3, 2, 0, 1, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 3, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 2, 0, 0, 0, 0, 0, 1, 1, 2, 1, 0, 2, 1, 0, 0, 0, 0, 0, 2, 2, 0, 3, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], [18, 18, 18, 20, 23, 22, 19, 22, 22, 21, 19, 19, 20, 20, 19, 19, 20, 20, 19, 21, 18, 19, 19, 19, 19, 19, 20, 18, 19, 20, 19, 19, 19, 18, 18, 19, 20, 20, 18, 18, 18, 20, 18, 18, 18, 20, 20, 18, 20, 20, 18, 19, 19, 20, 18, 20, 19, 20, 19, 19, 19, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 43, 37, 37, 46, 58, 64, 46, 50, 51, 54, 45, 39, 42, 43, 50, 49, 46, 51, 47, 68, 44, 44, 38, 44, 41, 37, 49, 49, 39, 56, 45, 46, 53, 51, 38, 32, 44, 37, 50, 44, 45, 60, 42, 39, 45, 47, 38, 47, 38, 42, 33, 41, 40, 40, 33, 45, 52, 41, 38, 43, 50, 56, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 8, 2, 3, 1, 0, 0, 0, 1, 0, 0, 0, 1, 3, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 1, 1, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [107], in <cell line: 29>()
     21     X_test.append(ast.literal_eval(old_test[i,]))
     26 print(X_train[:2])
---> 29 tensor = tf.tensor(X_train)

TypeError: new(): invalid data type 'str'
Run Code Online (Sandbox Code Playgroud)

我很困惑为什么会发生这种情况,它只是一个嵌套列表,下面的代码工作正常

data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
data = tf.tensor(data)
print(data)

tensor([[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]])
Run Code Online (Sandbox Code Playgroud)

有人可以帮我吗?

更新:发现 X_train 中的一个数组包含一个字符串。因此,转换X_train.append(ast.literal_eval(old_train[i,]))将 X_train 中的数组之一保留为字符串,从而导致错误。