【问题标题】:Keras - cannot convert numpy array to tensor objectKeras - 无法将 numpy 数组转换为张量对象
【发布时间】:2021-05-31 15:53:51
【问题描述】:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from tensorflow import keras

dftrain = pd.read_csv('https://storage.googleapis.com/tf-datasets/titanic/train.csv') # training data
dfeval = pd.read_csv('https://storage.googleapis.com/tf-datasets/titanic/eval.csv') # testing data
y_train = dftrain.pop('survived')
y_eval = dfeval.pop('survived')

model = keras.Sequential([
    keras.layers.Flatten(input_shape=[9]),  # input layer (1)
    keras.layers.Dense(128, activation='relu'),  # hidden layer (2)
    keras.layers.Dense(10, activation='softmax') # output layer (3)
])
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(dftrain, y_train, epochs=10)
TypeError: Could not build a TypeSpec for         sex   age  n_siblings_spouses  ...     deck  embark_town alone
0      male  22.0                   1  ...  unknown  Southampton     n
1    female  38.0                   1  ...        C    Cherbourg     n
2    female  26.0                   0  ...  unknown  Southampton     y
3    female  35.0                   1  ...        C  Southampton     n
4      male  28.0                   0  ...  unknown   Queenstown     y
..      ...   ...                 ...  ...      ...          ...   ...
622    male  28.0                   0  ...  unknown  Southampton     y
623    male  25.0                   0  ...  unknown  Southampton     y
624  female  19.0                   0  ...        B  Southampton     y
625  female  28.0                   1  ...  unknown  Southampton     n
626    male  32.0                   0  ...  unknown   Queenstown     y

[627 rows x 9 columns] with type DataFrame

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
     96       dtype = dtypes.as_dtype(dtype).as_datatype_enum
     97   ctx.ensure_initialized()
---> 98   return ops.EagerTensor(value, ctx.device_name, dtype)
     99 
    100 

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

我收到此错误! 这是一个预测一个人是否幸存的数据框。 谁能告诉我如何解决这个问题? 提前谢谢! 单击此处此链接以查看我尝试作为火车特征上传的数据框。 enter image description here

model.summary() =enter image description here

【问题讨论】:

  • 可以发model.summary()
  • 另外,为什么要将分类特征作为文本传递给神经网络?您必须将这些功能转换为编码(一个热门或其他)。
  • 是的,我编辑了我的问题并包含了 model.summary()
  • 嘿 akshay,谢谢你的帮助,你的意思是我应该先将这些数据帧转换为 tensorflow 数据集对象,然后再将其传递到 model.train 中?
  • 它与转换数据类型无关。神经网络无法处理文本数据。您必须使用 One-hot 编码或类似方法将分类列转换为数字。

标签: python pandas numpy tensorflow keras


【解决方案1】:

神经网络无法处理文本特征。您必须使用One-hot encoder 或其他形式的编码将分类特征转换为数字。

同样,对于序数特征,您想使用Label encoder

请阅读this guide 以了解为什么以及如何为神经网络处理这些输入中的每一个,以及您是否可能想要遵循有关如何使用神经网络对泰坦尼克数据集进行分类的指南。一个这样的链接是this one

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-19
    • 2021-08-25
    • 2020-01-23
    • 2021-11-26
    • 2020-05-04
    • 2021-10-19
    • 1970-01-01
    相关资源
    最近更新 更多