Tensorflow: ValueError: Expected non-integer, got <dtype: 'int32'>

Tensorflow: ValueError: Expected non-integer, got <dtype: 'int32'>

我刚开始使用 Tensorflow,当我调用 m.fit(input_fn=lambda: self.input_fn(train_data), steps=train_steps) 时,我收到以下错误。

File "/Library/Python/2.7/site-packages/tensorflow/contrib/layers/python/layers/feature_column_ops.py", line 161, in _input_from_feature_columns
    transformed_tensor = transformer.transform(column)
File "/Library/Python/2.7/site-packages/tensorflow/contrib/layers/python/layers/feature_column_ops.py", line 882, in transform
    feature_column.insert_transformed_feature(self._columns_to_tensors)
File "/Library/Python/2.7/site-packages/tensorflow/contrib/layers/python/layers/feature_column.py", line 991, in insert_transformed_feature
    self.sparse_id_column.insert_transformed_feature(columns_to_tensors)
File "/Library/Python/2.7/site-packages/tensorflow/contrib/layers/python/layers/feature_column.py", line 572, in insert_transformed_feature
    name="lookup")
File "/Library/Python/2.7/site-packages/tensorflow/contrib/lookup/lookup_ops.py", line 1018, in index_table_from_tensor
    "integer" if dtype.is_integer else "non-integer", keys.dtype))
ValueError: Expected non-integer, got <dtype: 'int32'>.

在我传递给fit()的特征栏中,只有int32int64,但这应该不是问题吧?

我认为您可能会在 tf.SparseTensor 中使用分类特征,但您的特征列包含 int32

在这种情况下,只需将整数列转换为字符串,例如:

# using Pandas
for f in categorical_features:
    df_train[f] = df_train[f].astype(str)   
    df_test[f] = df_test[f].astype(str)