【问题标题】:Tensorflow how to change hub.Module() to local folderTensorflow 如何将 hub.Module() 更改为本地文件夹
【发布时间】:2020-07-02 22:40:18
【问题描述】:

我该如何改变:

BERT_MODEL = "https://tfhub.dev/google/bert_multi_cased_L-12_H-768_A-12/1"

def create_tokenizer_from_hub_module():
  """Get the vocab file and casing info from the Hub module."""
  with tf.Graph().as_default():
    bert_module = hub.Module(BERT_MODEL)
    tokenization_info = bert_module(signature="tokenization_info", as_dict=True)
    with tf.Session() as sess:
      vocab_file, do_lower_case = sess.run([tokenization_info["vocab_file"],
                                            tokenization_info["do_lower_case"]])

  return bert.tokenization.FullTokenizer(
      vocab_file=vocab_file, do_lower_case=do_lower_case)

tokenizer = create_tokenizer_from_hub_module()

这样我就可以在没有 hub.Module() 调用的情况下加载本地 BERT 模型,因为它不适用于本地路径。

我从不同的网站下载了一个不同的 TF1 预训练模型,解压缩并存储在 /test/module/

如果我在BERT_MODEL = "/test/module" 以上更改,我需要如何更改其余部分?我现在收到字符串错误,因为tokenization_info = bert_module(signature="tokenization_info", as_dict=True) 不起作用。

请帮助我是 TF 新手 - 请注意我需要使用 TF1,而不是 TF2。

注意:根据以下建议,我得到:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-11-a98e44536f87> in <module>()
      9   return vocab_file, do_lower_case
     10 
---> 11 print(get_bert_tokenizer_info("/tmp/local_copy"))
     12 # Will print: (b'/tmp/local_copy/assets/vocab.txt', False)

4 frames
/usr/local/lib/python3.6/dist-packages/tensorflow_hub/registry.py in __call__(self, *args, **kwargs)
     43     raise RuntimeError(
     44         "Missing implementation that supports: %s(*%r, **%r)" % (
---> 45             self._name, args, kwargs))
     46 
     47 

RuntimeError: Missing implementation that supports: loader(*('/tmp/local_copy',), **{})

【问题讨论】:

    标签: tensorflow tensorflow-hub


    【解决方案1】:

    hub.Module 适用于本地未压缩路径,因此您可以将 BERT_MODEL 更改为另一个路径并重复使用相同的代码。

    例子:

    创建模块的本地副本:

    mkdir /tmp/local_copy
    wget "https://tfhub.dev/google/bert_multi_cased_L-12_H-768_A-12/1?tf-hub-format=compressed" -O "module.tar.gz"
    tar -C /tmp/local_copy -xzvf module.tar.gz
    

    使用模块的本地副本:

    import tensorflow as tf
    import tensorflow_hub as hub
    
    def get_bert_tokenizer_info(bert_module):
      """Get the vocab file and casing info from the Hub module."""
      with tf.Graph().as_default():
        bert_module = hub.Module(bert_module)
        tokenization_info = bert_module(signature="tokenization_info", as_dict=True)
        with tf.Session() as sess:
          vocab_file, do_lower_case = sess.run([tokenization_info["vocab_file"],
                                                tokenization_info["do_lower_case"]])
      return vocab_file, do_lower_case
    
    print(get_bert_tokenizer_info("/tmp/local_copy"))
    # Will print: (b'/tmp/local_copy/assets/vocab.txt', False)
    

    【讨论】:

    • 您好,谢谢!但很抱歉,它不起作用 - 我添加了上面收到的错误消息;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多