无法将numpy数组转换为model.fit的Tensor

ie3xauqp  于 2023-08-05  发布在  其他
关注(0)|答案(1)|浏览(88)

我是ML的初学者,在拟合模型时,我很难将numpy数组转换为Tensor。
当我尝试时得到的错误

test_audio_class(x_train.values, y_train.values, x_test.values, y_test.values)

字符串

Traceback (most recent call last):
  File "classifier.py", line 51, in <module>
    test_audio_class(x_train.values, y_train.values, x_test.values, y_test.values)
  File "classifier.py", line 28, in test_audio_class
    history = model.fit(x_train, y_train, epochs = 10, validation_data = (x_test, y_test))

line 102, in convert_to_eager_tensor
    return ops.EagerTensor(value, ctx.device_name, dtype)
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type tensorflow.python.framework.ops.EagerTensor).


我尝试了一些print语句来找出错误。

type of x_train = <class 'numpy.ndarray'>, values in x_train = <class 'tensorflow.python.framework.ops.EagerTensor'>
type of y_train = <class 'numpy.ndarray'>, values in y_train = <class 'str'>


我知道这与我正在做的EagerTensor有关,但不知道如何修复它。

o3imoua4

o3imoua41#

model.fit期望输入数据可以是:

  • 一个Numpy数组(或类似数组),或数组列表(如果模型有多个输入)。
  • TensorFlowTensor或Tensor列表(如果模型有多个输入)。
  • 如果模型具有命名输入,则将输入名称Map到相应的数组/Tensor的dict。

请参考下面的代码片段,这将解决您的问题。

x_train = np.asarray(x_train).astype(np.float32)
y_train = np.asarray(y_train).astype(np.float32)

字符串
或者,您也可以这样做:

tf.convert_to_tensor(X_train, dtype=tf.float32)


如果需要进一步干预,请留下评论。

相关问题