tf.lookup: 在tf内部做string2id的映射

之前在写tf模型的时候,对于category类型的特征,经常是预处理成id,然后才输入到模型中去,category->id的映射通常是独立与tf代码的。tf.lookup模块中提供了使用tensorflow原生api将category特征映射为id的方法,本文将介绍这些方法。

tf.lookup模块中有两类方法:

  • Initializer:负责构建 category -> id 映射表
    • tf.lookup.KeyValueTensorInitializer: 通过显式指定 category->id构建映射表
    • tf.lookup.TextFileInitializer:从文件读取数据构建映射表
  • Table:复制执行 category -> id 的映射
    • tf.lookup.StaticHashTable:通过给定的映射表,进行映射,如果没有找到,则返回默认值
    • tf.lookup.StaticVocabularyTable:通过给定的映射表,进行映射,如果没有找到,则会映射为 hash(<term>) % num_oov_buckets + vocab_size

输入

import tensorflow as tf 
keys_tensor = tf.constant(['牛奶', '鸡蛋'])
vals_tensor = tf.constant([3, 4])
input_tensor = tf.constant(['鸡蛋', '白菜'])
table = tf.lookup.StaticHashTable(
    tf.lookup.KeyValueTensorInitializer(keys_tensor, vals_tensor), -1)
out = table.lookup(input_tensor)
with tf.Session() as sess:
    sess.run(tf.tables_initializer())
    print(sess.run(out))

输出

 输入

""" profile_feats.txt
    hello
    world
"""
init = tf.lookup.TextFileInitializer(
  filename='profile_feats.txt',
  key_dtype=tf.string, key_index=tf.lookup.TextFileIndex.WHOLE_LINE,
  value_dtype=tf.int64, value_index=tf.lookup.TextFileIndex.LINE_NUMBER)
table = tf.lookup.StaticHashTable(init, -1)
out = table.lookup(tf.constant('world'))
with tf.Session() as sess:
    sess.run(tf.tables_initializer())
    print(sess.run(out))

 输出

  

 TF中的哈希处理列&哈希冲突处理

 

tf.lookup: 在tf内部做string2id的映射 - 知乎tensorflow1.15之前在写tf模型的时候,对于category类型的特征,经常是预处理成id,然后才输入到模型中去,category->id的映射通常是独立与tf代码的。tf.lookup模块中提供了使用tensorflow原生api将category特…https://zhuanlan.zhihu.com/p/346341718

 

TF中的哈希处理列&哈希冲突处理 - 知乎一. TF中的哈希处理列: 1.hash冲突的可能性高,它是调用的string_to_hash_bucket_fast进行的hash处理 2.hash冲突的可能性低,它是调用的string_to_hash_bucket_strong进行的hash处理 二. sparse_column_with_hash_…https://zhuanlan.zhihu.com/p/38044142

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值