Python 如何将'tf.contrib.lookup.index_table_从_file'转换为Tensorflow v2
我是Tensorflow新手,正在处理v1 Tensorflow中的代码,但是Tensorflow中不再支持Python 如何将'tf.contrib.lookup.index_table_从_file'转换为Tensorflow v2,python,tensorflow,tensorflow2.0,Python,Tensorflow,Tensorflow2.0,我是Tensorflow新手,正在处理v1 Tensorflow中的代码,但是Tensorflow中不再支持tf.contrib模块,我在V2中很难找到它们的替代品。 我使用以下代码禁用了V2: import tensorflow.compat.v1 as tf tf.disable_v2_behavior() 但它仍然不能用于文件中的tf.contrib.lookup.index\u table,我得到了一个错误 属性错误:模块“tensorflow\u core.compat.v1”没有属
tf.contrib
模块,我在V2中很难找到它们的替代品。我使用以下代码禁用了V2:
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
但它仍然不能用于文件中的tf.contrib.lookup.index\u table,我得到了一个错误
属性错误:模块“tensorflow\u core.compat.v1”没有属性“contrib”
。我试图查看他们的官方文件,但找不到
V2中文件函数中的
tf.contrib.lookup.index\u table\u的替代品是什么 您可以尝试tf.lookup.StaticVocabularyTable
来实现相同的功能。
如果要从文本文件中获取索引和值,可以在tf.lookup.StaticVocabularyTable
中使用tf.lookup.TextFileInitializer
下面是一个例子
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了解更多详细信息。您可以尝试使用文件中的tf.python.ops.lookup\u ops.index\u table\u,并告诉我们它是否有用。