首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tensorflow中的分类连续交叉特征列

Tensorflow中的分类连续交叉特征列
EN

Stack Overflow用户
提问于 2018-10-16 09:12:19
回答 1查看 909关注 0票数 4

在使用Tensorflow的估计量和feature_column时,可以跨越一个范畴列和一个桶形连续列交叉柱,而不是一个范畴和数字交叉。可以通过column.py#L704实现这个功能吗?

在Tensforflow图中看到实现相同结果的任何替代方法也将是很棒的。

代码语言:javascript
复制
import numpy as np

cont = np.array([1,2,3])
cat = np.array(['cat', 'dog', 'cat'])

cross_function(cat, cont) = np.array([[1,0],[0,2],[3,0]])
EN

回答 1

Stack Overflow用户

发布于 2018-10-19 11:50:13

在这里回答我自己的问题。所涉及的步骤是:

  1. 对分类特征进行数字编码
    • 在图中,所以它在火车和服务中是可能的

  1. 数值结果的一种热编码
  2. 将其与连续变量相乘

代码:

代码语言:javascript
复制
import numpy as np
import tensorflow as tf

cont = np.array([1,2,3])
cat = np.array(['cat', 'dog', 'cat'])
categories = np.unique(cat)

def categorical_continuous_interaction(categorical_onehot, continuous):

    cont = tf.expand_dims(continuous, 0)
    return tf.transpose(tf.multiply(tf.transpose(categorical_onehot), cont))

def transformation_function(feature_dictionary, mapping_table):

    continuous_feature = feature_dictionary['cont']

    categorical_feature = mapping_table.lookup(feature_dictionary['cat'])
    onehot = tf.one_hot(categorical_feature, categories.shape[0])
    cross_feature = categorical_continuous_interaction(onehot, continuous_feature)

    return {'feature_name': cross_feature}

def input_function(dataframe, label_key, ...):
    # categorical mapping tables, these must be generated outside of the dataset 
    # transformation function but within the input function
    mapping_table = tf.contrib.lookup.index_table_from_tensor(
        mapping=tf.constant(categories),
        num_oov_buckets=0, 
        default_value=-1
    )

    # Generate the dataset of a dictionary of all of the dataframes columns
    dataset = tf.data.Dataset.from_tensor_slices(dict(dataframe))
    # Convert to a dataset of tuples of dicts with the labels as one tuple
    dataset = dataset.map(lambda x: split_label(x, label_key))
    # Transform the features dict within the dataset
    dataset = dataset.map(lambda features, labels: (transformation_function(
        features, mapping_table=mapping_table), labels))

    ...

    return dataset

def serving_input_fn():
    # categorical mapping tables, these must be generated outside of the dataset 
    # transformation function but within the input function
    mapping_table=tf.contrib.lookup.index_table_from_tensor(
        mapping=tf.constant(categories),
        num_oov_buckets=0, 
        default_value=-1
    )
    numeric_receiver_tensors = {
        name: tf.placeholder(dtype=tf.float32, shape=[1], name=name+"_placeholder")
        for name in numeric_feature_column_names
    }
    categorical_receiver_tensors = {
        name: tf.placeholder(dtype=tf.string, shape=[1], name=name+"_placeholder")
        for name in categorical_feature_column_names
    }
    receiver_tensors = {**numeric_receiver_tensors, **categorical_receiver_tensors}

    features = transformation_function(receiver_tensors, 
        country_mapping_table=country_mapping_table)

    return tf.estimator.export.ServingInputReceiver(features, receiver_tensors)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52831934

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档