如何在tf.estimator的input_fn中使用查找表?

时间:2019-06-20 08:44:54

标签: python tensorflow

我想在估算器的input_fn中使用查找表(将单词转换为id),但是出现错误“表未初始化”

我试图在input_fn中添加tf.add_to_collection(tf.GraphKeys.TABLE_INITIALIZERS,tf.tables_initializer())和在model_fn中添加tf.train.Scaffold()。finalize(),但是错误相同。

代码:

def model_fn(features, labels, params, mode):

    # Defines model's ops.
    pass

def input_fn_builder(params):    


    def token(d,y):       

        words=tf.string_split([d['text']])
        tokens=tf.sparse_tensor_to_dense(words,default_value='UNK')        

        table=tf.contrib.lookup.index_table_from_file(vocab_file)
        tokens=table.lookup(tokens) # convert to ids       

        return {'text':tokens},y    
    def input_fn():        
        dataset=tf.data.Dataset.from_tensor_slices(({'text':x},y) )  
        if is_train:
            dataset=dataset.shuffle(1000).repeat(epochs)
        dataset=dataset.map(token)
        dataset=dataset.batch(batch_size)

        return dataset # 
    return input_fn

estimator = tf.estimator.Estimator(model_fn)

estimator.train(input_fn_builder(params))

错误跟踪:

FailedPreconditionError                   Traceback (most recent call last)
E:\Python\virtualenv\NER\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
   1333     try:
-> 1334       return fn(*args)
   1335     except errors.OpError as e:

E:\Python\virtualenv\NER\lib\site-packages\tensorflow\python\client\session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
   1318       return self._call_tf_sessionrun(
-> 1319           options, feed_dict, fetch_list, target_list, run_metadata)
   1320 

E:\Python\virtualenv\NER\lib\site-packages\tensorflow\python\client\session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
   1406         self._session, options, feed_dict, fetch_list, target_list,
-> 1407         run_metadata)
   1408 

FailedPreconditionError: Table not initialized.
     [[{{node hash_table_Lookup}}]]
     [[{{node IteratorGetNext}}]]

0 个答案:

没有答案