python - 如何将 `tf.contrib.lookup.index_table_from_file` 转换为 Tensorflow v2
问题描述
我是 Tensorflow 的新手,正在处理来自 v1 Tensorflow 的代码,但是 Tensorflowtf.contrib
不再支持模块,而且我在 V2 中找不到它们的替代品时遇到了麻烦。
我用这段代码禁用了 V2:
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
但它仍然不起作用,tf.contrib.lookup.index_table_from_file
我得到一个错误
AttributeError: module 'tensorflow_core.compat.v1' has no attribute 'contrib'
。我尝试在此处查看他们的官方文档,但找不到。
tf.contrib.lookup.index_table_from_file
V2 中函数的替代品是什么 ?
解决方案
你可以尝试tf.lookup.StaticVocabularyTable
达到同样的效果。
如果你想从 TextFile 中获取索引和值,你可以tf.lookup.TextFileInitializer
在tf.lookup.StaticVocabularyTable
.
下面是一个例子。
num_oov_buckets = 3
input_tensor = tf.constant(["emerson", "lake", "palmer", "king", "crimnson"])
table = tf.lookup.StaticVocabularyTable(
tf.lookup.TextFileInitializer(
filename,
key_dtype=tf.string, key_index=tf.lookup.TextFileIndex.WHOLE_LINE,
value_dtype=tf.int64, value_index=tf.lookup.TextFileIndex.LINE_NUMBER,
delimiter="\t"),
num_oov_buckets)
out = table.lookup(input_tensor)
您可以关注Tensorflow 的官方文档以获取更多详细信息。
推荐阅读
- javascript - Javascript Regexp 根据掩码替换
- elasticsearch - 弹性搜索角色未授予预期权限
- excel - Gephi电子表格导入不会加载边缘表中列出的所有边缘
- python - 训练/测试、准确率和 f1 分数的 OOB 分数
- firebase - 我想在flutter firebase的文本字段中获取一些特定的用户详细信息,但是有很多错误,所以它没有获取任何数据
- sql - MS Access 查询,包括自身的连接(使用子查询)并报告不存在的值(空值)
- react-hook-form - 类型错误:无法读取未定义的属性“消息”---使用 react-hook-form
- google-apps-script - 如果在表格中显示“OK”,则将其涂成绿色
- amazon-web-services - AWSElasticBeanstalkFullAccess(提供完全访问权限...)等效?
- javascript - 如何确定使用的 avro 版本?