How to use embedding_column with BoostedTreesClassifier

Viewed 153

I am learning the GBDT example on the tensorflow official website:https://www.tensorflow.org/tutorials/estimator/boosted_trees?hl=en The example on the official website is one-hot processing of classification features. But I want to convert discrete features into dense vectors through embedding. In the process, I encountered an unsolvable problem. The following code is an example from the official website.

CATEGORICAL_COLUMNS = ['sex', 'n_siblings_spouses', 'parch', 'class', 'deck',
                       'embark_town', 'alone']
NUMERIC_COLUMNS = ['age', 'fare']

def one_hot_cat_column(feature_name, vocab):
return tf.feature_column.indicator_column(tf.feature_column.categorical_column_with_vocabulary_list(feature_name,vocab))

for feature_name in CATEGORICAL_COLUMNS:
    vocabulary = dftrain[feature_name].unique()
    feature_columns.append(one_hot_cat_column(feature_name, vocabulary))

for feature_name in NUMERIC_COLUMNS:
    feature_columns.append(tf.feature_column.numeric_column(feature_name,dtype=tf.float32))

But when I change the'indicator_column' to'embedding_column' and run the'boostedtreeclassifier', an error is reported.

def embedding_column(feature_name,vocab):
return tf.feature_column.embedding_column(categorical_column=tf.feature_column.categorical_column_with_vocabulary_list(
                                          feature_name,vocab),dimension=4,combiner='sum')
feature_columns = []
for feature_name in CATEGORICAL_COLUMNS:
    vocabulary = dftrain[feature_name].unique()
    feature_columns.append(embedding_column(feature_name,vocabulary))
 
for feature_name in NUMERIC_COLUMNS:
    feature_columns.append(tf.feature_column.numeric_column(feature_name,
                                           dtype=tf.float32))

Next, I have not made any changes to the data input method of the official website.

NUM_EXAMPLES = len(y_train)
def make_input_fn(X,y,n_epochs=None,shuffle=True):
    def input_fn():
        dataset = tf.data.Dataset.from_tensor_slices((dict(X),y))
        if shuffle:
            dataset = dataset.shuffle(NUM_EXAMPLES)
        dataset = dataset.repeat(n_epochs)
        dataset = dataset.batch(NUM_EXAMPLES)
        return dataset
    return input_fn
    

Then I start to initialize the model and train, and an error will be reported here.

n_batches = 1
est = tf.estimator.BoostedTreesClassifier(feature_columns,n_batches_per_layer=n_batches)
train_input_fn = make_input_fn(dftrain,y_train)

est.train(train_input_fn,max_steps=100)

The above says: '# NOTE: GBDT requires that all DenseColumns expose a dtype attribute.' 'AttributeError:'EmbeddingColumn' object has no attribute'dtype''. I use 'estimator.LinearClassifier' and there is no problem, but when I use 'tf.estimator.BoostedTreesClassifier', this problem occurs. enter image description here

0 Answers
Related