官网:https://tensorflow.google.cn/guide/feature_columns
参考:https://blog.csdn.net/cjopengler/article/details/78161748
numeric_column
numeric_column( key, shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None )
key: 特征的名字。也就是对应的列名称。
shape: 该key所对应的特征的shape. 默认是1,但是比如one-hot类型的,shape就不是1,而是实际的维度。总之,这里是key所对应的维度,不一定是1.
default_value: 如果不存在使用的默认值
normalizer_fn: 对该特征下的所有数据进行转换。如果需要进行normalize,那么就是使用normalize的函数.这里不仅仅局限于normalize,也可以是任何的转换方法,比如取对数,取指数,这仅仅是一种变换方法.

import tensorflow as tf tf.enable_eager_execution() def Test_numeric_column(): features = {'sales' : [[5], [10], [8], [9]]} columns = [tf.feature_column.numeric_column('sales')] inputs = tf.feature_column.input_layer(features, columns) print(inputs) Test_numeric_column()
bucketized_column
bucketized_column(
source_column,
boundaries
)
source_column: 必须是numeric_column
boundaries: 不同的桶。boundaries=[0., 1., 2.],产生的bucket就是, (-inf, 0.), [0., 1.), [1., 2.), and [2., +inf), 每一个区间分别表示0, 1, 2, 3,所以相当于分桶分了4个.

import tensorflow as tf from tensorflow import feature_column as fc tf.enable_eager_execution() def test_bucketized_column(): price = {'price': [[5.], [15.], [25.], [35.]]} # 4行样本 price_column = fc.numeric_column('price') bucket_price = fc.bucketized_column(price_column, [0, 10, 20, 30, 40]) price_bucket_tensor = fc.input_layer(price, [bucket_price]) print(price_bucket_tensor) test_bucketized_column()
categorical_column_with_vocabulary_list
categorical_column_with_vocabulary_list( key, vocabulary_list, dtype=None, default_value=-1, num_oov_buckets=0 )
key: feature名字
vocabulary_list: 对于category来说,进行转换的list.也就是category列表.
dtype: 仅仅string和int被支持,其他的类型是无法进行这个操作的.
default_value: 当不在vocabulary_list中的默认值,这时候num_oov_buckets必须是0.
num_oov_buckets: 用来处理那些不在vocabulary_list中的值,如果是0,那么使用default_value进行填充;如果大于0,则会在[len(vocabulary_list), len(vocabulary_list)+num_oov_buckets]这个区间上重新计算当前特征的值.
与前面numeric 不同的是,这里返回的是稀疏tensor.

import tensorflow as tf from tensorflow import feature_column as fc tf.enable_eager_execution() def categorical_column_with_vocabulary_list(): color_data = {'color': [['R', 'R'], ['G', 'R'], ['B', 'G'], ['A', 'A']]} # 4行样本 color_column = fc.categorical_column_with_vocabulary_list('color', ['R', 'G', 'B'], dtype=tf.string, default_value=-1) color_column_identy = fc.indicator_column(color_column) color_dense_tensor = fc.input_layer(color_data, [color_column_identy]) print(color_dense_tensor) categorical_column_with_vocabulary_list()