神经网络 - ValueError:无法将 NumPy 数组转换为 Tensor(不支持的对象类型列表)

Neural Network - ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type list)

提问人:ProgramminType 提问时间:1/5/2023 更新时间:1/5/2023 访问量:128

问:

我正在做一个 tensorflow 项目,其中包括一个神经网络,它应该能够学习如何从输入报价中获取鼓舞人心的报价。但是当我去运行我的代码时,我收到这个错误:

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type list).

我尝试了很多方法,但只是在寻找它发生的原因。我认为这是因为我在数组中使用了一个嵌套列表,如下所示:

[list([7, 8]) list([33, 34, 34]) list([24, 4, 0, 18, 18])]

但我不确定为什么这会导致错误,因为我在其他神经网络中非常频繁地使用列表和列表列表。

这是我的完整代码:

alpha = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", " ", ".", ",", "!"]
packing = []
listing = []
try:
  while True:
    temp = input(":")
    for elements in temp:
      count = 0
      for i in range(56):
        if elements == alpha[count]:
          listing.append(count)
          break
        count = count + 1
  
    packing.append(listing)
    listing = []

except KeyboardInterrupt:
  print("Dataset Created.")
  print("This is your Data: " + str(packing))

import tensorflow as tf
import numpy as np
from tensorflow import keras
from tensorflow.keras.activations import relu
from tensorflow.keras import regularizers

packing = np.array(packing)
shapepack = packing.shape[0]
inputs = np.zeros((shapepack,))
print(packing)
print("--------------")
print(inputs)

model = keras.Sequential([
    keras.layers.Dense(6, input_shape=(shapepack,), activation='relu',
                       kernel_regularizer=regularizers.l1(0.01)),
    #                                                     0.01 is the reg rate
    keras.layers.Dense(8, input_shape=(shapepack,), activation='relu',
                       kernel_regularizer=regularizers.l1(0.01)),
    #                                                     0.01 is the reg rate
    keras.layers.Dense(1, input_shape=(shapepack,), activation='relu',
                       kernel_regularizer=regularizers.l1(0.01))
    #                                                     0.01 is the reg rate
])

model.compile(optimizer='sgd', loss='mse')

network = model.fit(inputs, packing, epochs=35000)

predictions = model.predict(packing)

del tf
del np
del keras
del relu
del regularizers

我试图通过将列表转换为float64来修复错误,但我无法更改内部列表,因此它们仍保留为列表类型,我希望代码可以工作并开始运行网络,但只是以相同的错误结束。

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type list).

感谢所有帮助!

Python 数组 TensorFlow 神经网络 嵌套列表

评论

0赞 Corralien 1/5/2023
嵌套列表的长度不同。在转换为张量之前,您必须填充它们。
0赞 Hiran Hasanka 1/5/2023
在此处查看填充序列。

答: 暂无答案