如何tf.估计量具有连续列和分类列

2024-04-25 20:09:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个tf.估计量它适用于连续变量,我想扩展它来使用分类变量。你知道吗

考虑一个如下所示的数据帧:

label           |  con_col          |  cat_col
(float 0 or 1)  |  (float -1 to 1)  |  (int 0-3)
----------------+-------------------+---------------
0               |   0.123           |  2
0               |   0.456           |  1
1               |  -0.123           |  3
1               |  -0.123           |  3
0               |   0.123           |  2

为了只为标签和连续变量列(con\u col)构建估计器,我构建了以下特征列变量。你知道吗

feature_cols = [
                   tf.feature_column.numeric_column('con_col')
               ]

然后我像这样把它传给DNN分类员。你知道吗

tf.estimator.DNNClassifier(feature_columns=feature_cols ...)

后来我构建了一个服务输入\ u fn()。在这个函数中,我还指定了列。此例程非常小,如下所示:

def serving_input_fn(): 
    feat_placeholders['con_col'] = tf.placeholder(tf.float32, [None])

    return tf.estimator.export.ServingInputReceiver(feat_placeholders.copy(), feat_placeholders)

这很管用。但是,当我尝试使用分类列时,我遇到了一个问题。你知道吗

因此,使用分类列,这一部分似乎是可行的。你知道吗

feature_cols = [
    tf.feature_column.sequence_categorical_column_with_identity('cat_col', num_buckets=4))
               ]
tf.estimator.DNNClassifier(feature_columns=feature_cols ...)

对于服务\u input \u fn(),我从堆栈跟踪中得到建议,但两个建议都失败了:

def serving_input_fn(): 
    # try #2
    # this fails
    feat_placeholders['cat_col'] = tf.SequenceCategoricalColumn(categorical_column=tf.IdentityCategoricalColumn(key='cat_col', number_buckets=4,default_value=None))

    # try #1
    # this also fails
    # feat_placeholders['cat_col'] = tf.feature_column.indicator_column(tf.feature_column.sequence_categorical_column_with_identity(column, num_buckets=4))

    # try #0
    # this fails. Its using the same form for the con_col
    # the resulting error gave hints for the above code.
    # Note, i'm using this url as a guide.  My cat_col is
    # is similar to that code samples 'dayofweek' except it
    # is not a string.
    # https://github.com/GoogleCloudPlatform/training-data-analyst/blob/master/courses/machine_learning/feateng/taxifare_tft/trainer/model.py
    #feat_placeholders['cat_col'] = tf.placeholder(tf.float32, [None])


    return tf.estimator.export.ServingInputReceiver(feat_placeholders.copy(), feat_placeholders)

如果使用try#0,则这是错误消息。你知道吗

ValueError: Items of feature_columns must be a <class 'tensorflow.python.feature_column.feature_column_v2.DenseColumn'>. You can wrap a categorical column with an embedding_column or indicator_column. Given: SequenceCategoricalColumn(categorical_column=IdentityCategoricalColumn(key='cat_col', number_buckets=4, default_value=None))

Lak的答案实施

以Lak的答案为指导,这两个专栏都适用。你知道吗

# This is the list of features we pass as an argument to DNNClassifier
feature_cols = []

# Add the continuous column first
feature_cols.append(tf.feature_column.numeric_column('con_col'))                  

# Add the categorical column which is wrapped?
# This creates new columns from a single column?
category_feature_cols = [tf.feature_column.categorical_column_with_identity('cat_col', num_buckets=4)]
for c in category_feature_cols:
    feat_cols.append(tf.feature_column.indicator_column(c))

# now pass this list to the DNN
tf.estimator.DNNClassifier(feature_columns=feature_cols ...)


def serving_input_fn(): 
    feat_placeholders['con_col'] = tf.placeholder(tf.float32, [None])
    feat_placeholders['cat_col'] = tf.placeholder(tf.int64, [None])

Tags: columnsthenonetfcolumncolconfeature
1条回答
网友
1楼 · 发布于 2024-04-25 20:09:12

在发送到DNN之前,需要包装分类列:

cat_feature_cols = [ tf.feature_column.sequence_categorical_column_with_identity('cat_col', num_buckets=4)) ]
feature_cols = [tf.feature_column.indicator_column(c) for c in cat_feature_cols]

使用指示符列进行一次热编码,或使用嵌入列进行嵌入。你知道吗

相关问题 更多 >