使用Tensorflow 2.0 alpha时,我在尝试使用以下数据创建tf.data.Dataset时收到了错误信息“ValueError: Can't convert Python sequence with mixed types to Tensor”。
(当
现在我想知道是否有什么我错过了,因为Tensorflow的教程(如上所述)也使用混合类型的数据框,但是当我尝试使用完全相同的
显然,这里有混合的数据类型。 性别
是一个字符串,年龄
是一个浮点/双精度数,SibSp
和 Parch
都是整数等等。
我(使用 Python 3)编写的将此 Pandas 数据框架 转换为 tf.data.Dataset
的代码基于 Tensorflow 的 如何对结构化数据进行分类 教程,并且如下所示:
def df_to_dataset(dataframe, shuffle=True, batch_size=32):
dataframe = dataframe.copy()
# the 'Survived' column is the label (not shown in the image of the Dataframe but exists in the Dataframe)
label = dataframe.pop('Survived')
# create the dataset from the dataframe
ds = tf.data.Dataset.from_tensor_slices((dict(dataframe), label))
# if shuffle == true, randomize the entries
if shuffle:
ds = ds.shuffle(buffer_size=len(dataframe))
ds = ds.batch(batch_size)
return ds
如上所述,当使用混合类型的 Python 序列执行此函数时,将抛出错误ValueError: Can't convert Python sequence with mixed types to Tensor
。
train_ds = df_to_dataset(df_train, batch_size=32)
(当
df_train
是图像中可见的Pandas数据帧时)现在我想知道是否有什么我错过了,因为Tensorflow的教程(如上所述)也使用混合类型的数据框,但是当我尝试使用完全相同的
df_to_dataset
函数来运行此示例时,却没有遇到任何错误。
df_to_dataset
时发生,而不是在训练模型时。 - Eyk Rehbeintrain_ds
的机会,因为在创建train_ds
时它就会抛出一个错误@Sharky - Eyk Rehbein