Reputation: 536
I'm a newbie to Tensorflow and am trying to train a model. The model includes a feature cross of two variables. One of the variables contains data that has been "Z-scored" / normalized. The other is what I would call a "dummy variable" that takes the value of either a 1 or a 0.
When I attempt to run the script, I get the following error message:
ValueError: Unsupported key type. All keys must be either string, or categorical column except HashedCategoricalColumn.
I ran the dtypes, and the 'variable one_or_zero_col' is an int64, while the "normalized_col" is a float64.
Below is a sample of the code that should show the error when run.
from __future__ import absolute_import, division, print_function, unicode_literals
import numpy as np
import pandas as pd
import tensorflow as tf
# Create train_df.
train_df = pd.DataFrame({'one_or_zero_col': [1, 0, 0, 1, 1, 0, 0],
'normalized_col': [0, 1.25, -0.5, 0, 0, -.15, 0.1]})
print(train_df.dtypes)
# Feature column list
feature_columns = []
# Create feature columns.
one_or_zero_col = tf.feature_column.numeric_column('one_or_zero_col')
normalized_col = tf.feature_column.numeric_column('normalized_col')
feature_columns.append(normalized_col)
# Create a feature cross of normalized_col and one_or_zero_col.
normalized_col_x_one_or_zero_col = tf.feature_column.crossed_column([normalized_col, one_or_zero_col], hash_bucket_size=100)
crossed_feature = tf.feature_column.indicator_column(normalized_col_x_one_or_zero_col)
feature_columns.append(normalized_col_x_one_or_zero_col)
Error Message Here is the full error message, in case it is helpful.
one_or_zero_col int64
normalized_col float64
dtype: object
Traceback (most recent call last):
File "Z:\ML\testML.py", line 19, in <module>
normalized_col_x_one_or_zero_col = tf.feature_column.crossed_column([normalized_col, one_or_zero_col], hash_bucket_size=100)
File "Z:\Python\lib\site-packages\tensorflow\python\feature_column\feature_column_v2.py", line 2170, in crossed_column
raise ValueError(
ValueError: Unsupported key type. All keys must be either string, or categorical column except HashedCategoricalColumn. Given: NumericColumn(key='normalized_col', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None)
[Finished in 2.713s]
This is my first attempt to use a feature cross in tensorflow, and so I am trying to crib from the Google Crash Course (and obviously not doing a very good job!). Any help / advice on why this feature cross does not work would be greatly appreciated. Thanks!
Upvotes: 0
Views: 325
Reputation: 1550
I had this same error, I solved passing the key as string (but also an instance of a feature from tf.feature_column.bucketized_column)
In your case this may be enough
normalized_col_x_one_or_zero_col = tf.feature_column.crossed_column(["normalized_col", "one_or_zero_col"], hash_bucket_size=100)
Upvotes: 1