tensorflow.feature_column

1. 数值列:tf.feature_column.numeric_column

用于dense特征,可指定该特征的数值类型为float64等(默认为float64),也可指定该特征是一个10维向量或矩阵。

# Represent a tf.float64 scalar.
numeric_feature_column = tf.feature_column.numeric_column(key="SepalLength",dtype=tf.float64)

# Represent a 10-element vector in which each cell contains a tf.float32.
vector_feature_column = tf.feature_column.numeric_column(key="Bowling", shape=10)

# Represent a 10x5 matrix in which each cell contains a tf.float32.
matrix_feature_column = tf.feature_column.numeric_column(key="MyMatrix", shape=[10,5])

 

2. 分桶列:tf.feature_column.bucketized_column

对某数值列进行分桶,例如出生年份列,可根据提供的boundaries列表中的3个值,将数值对应的分组。每个分组可用onehot变量表示。

# First, convert the raw input to a numeric column.
numeric_feature_column = tf.feature_column.numeric_column("Year")

# Then, bucketize the numeric column on the years 1960, 1980, and 2000.
bucketized_feature_column = tf.feature_column.bucketized_column(
    source_column = numeric_feature_column,
    boundaries = [1960, 1980, 2000])
#---------------------------------------
< 1960                      [1, 0, 0, 0]
>= 1960 and < 1980          [0, 1, 0, 0]
>= 1980 and < 2000          [0, 0, 1, 0]
> 2000                      [0, 0, 0, 1]

3. 分类标识列:tf.feature_column.categorical_column_with_identity

主要用于值为整数的类别型变量,数字表示离散特征值的编码数字。把numerical data转乘one hot encoding。num_buckets用于确定每一个one-hot向量的最大特征类别数。

4. 分类词汇列:tf.feature_column.categorical_column_with_vocabulary_list、file

根据单词的序列顺序,把单词根据index转换成one hot encoding。主要用于大量文本类型特征处理时,将特征处理为数值的场景。其后跟着的list用于定义该变量的所有类别。

import tensorflow as tf
sess=tf.Session()

#特征数据
features = {
    'sex': ['male', 'male', 'female', 'female'],
}

#特征列
sex_column = tf.feature_column.categorical_column_with_vocabulary_list('sex', ['male', 'female'])
sex_column = tf.feature_column.indicator_column(sex_column)
#组合特征列
columns = [
    sex_column
]

#输入层(数据,特征列)
inputs = tf.feature_column.input_layer(features, columns)

#初始化并运行
init = tf.global_variables_initializer()
sess.run(tf.tables_initializer())
sess.run(init)

v=sess.run(inputs)
print(v)

In [33]:
[[1. 0.]
 [1. 0.]
 [0. 1.]
 [0. 1.]]

5.通过哈希处理的列:tf.feature_column.categorical_column_with_hash_bucket

对于处理包含大量文字或数字类别的特征时可使用hash的方式,这能快速地建立对应的对照表。对于这种类型的特征列,模型会计算输入的哈希值,然后使用模运算符将其置于其中一个 hash_bucket_size 类别中,缺点则是会有哈希冲突的问题,如以下伪代码所示:

# pseudocode
feature_id = hash(raw_feature) % hash_buckets_size

hashed_feature_column =
    tf.feature_column.categorical_column_with_hash_bucket(
        key = "some_feature",
        hash_buckets_size = 100) # The number of categories

6. 组合列:tf.feature_column.crossed_column

通过将多个特征组合为一个特征(称为特征组合),模型可学习每个特征组合的单独权重。

交叉后的特征会基于hash_bucket_size的大小进行hash得到结果,理论上,转换操作的公式如下:Hash(cartesian product of features) % hash_bucket_size

tf.feature_column.crossed_column(
    keys,
    hash_bucket_size,
    hash_key=None
)

参考: 

https://zhuanlan.zhihu.com/p/73701872

https://iii.run/archives/263.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值