基于tensorflow-hub使用预训练bert模型——简单易上手成功率百分百

本文介绍了如何在tensorflow 2.3.0和tensorflow-hub 0.9.0环境下,使用python 3.7.6加载和应用预训练BERT模型。在尝试直接获取模型输出时遇到错误,通过分析错误并尝试多种方法后,发现需要重新构建BERT模型以利用predict函数获取输出。同时,提供了微调预训练模型的步骤和相关资源链接。
摘要由CSDN通过智能技术生成

最近,研究了下如何使用基于tensorflow-hub中预训练bert,一开始找到的关于预模型使用介绍的官方教程国内打不开,所以看了很多博客遇到了很多坑,直至最后找到能打开的教程,才发现使用很简单。

实验版本:

         tensorflow版本: 2.3.0

         tensorflow-hub版本:0.9.0

         python版本: 3.7.6

数据准备:

         首先,熟悉bert的都知道输入有3个:inputIds、inputMask、segmentIds,这个不多说了,百度一大堆。

直接获取bert输出代码:

max_seq_length = 256

input_word_ids = tf.keras.layers.Input(shape=(max_seq_length,),
                                       dtype=tf.int32,name="input_word_ids")
input_mask = tf.keras.layers.Input(shape=(max_seq_length,),
                                   dtype=tf.int32,name="input_mask")
segment_ids = tf.keras.layers.Input(shape=(max_seq_length,),
                                    dtype=tf.int32,name="segment_ids")

# 将trainable值改为False
module = hub.KerasLayer(BERT_URL,trainable=False)#,signature="token")
pooled_output, sequence_output = module([input_mask,segment_ids,input_word_ids])

# 构建模型输入输出
model = tf.keras.Model(inputs=[input_word_ids,input_mask,segment_ids],outputs=[pooled_output,sequence_output])

# 获取输出
output = model.predict([inputIds,inputMask,segmentIds]) 
# output输出结果 ----》 pool_out: shape=[batch, 768];sequence_out: shape=[batch, 256, 768] 

-------------------------------------------------BUG----------------------------------------------

这里也尝试了参考链接3中博客方式获取bert输出结果,但是遇到个问题

         ValueError: Could not find matching function to call loaded from the SavedModel. Got:
                           Positional arguments (2 total):
                         * False
                         * None:

# 实验内容1——参数名来自https://hub.tensorflow.google.cn/tensorflow/bert_zh_L-12_H-768_A-12/2
outputs,_ = hub_module(input_word_ids=tf.constant(tmp_inputids),
    input_mask=tf.constant(tmp_inputMask),
    segment_ids=tf.constant(tmp_segmentIds))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-45-b1533b83b191> in <module>
      2 outputs,_ = hub_module(input_word_ids=tf.constant(tmp_inputids),
      3     input_mask=tf.constant(tmp_inputMask),
----> 4     segment_ids=tf.constant(tmp_segmentIds))
      5 
      6 # # 实验内容2——参数名来自报错提示

/opt/conda/lib/python3.7/site-packages/tensorflow/python/saved_model/load.py in _call_attribute(instance, *args, **kwargs)
    507 
    508 def _call_attribute(instance, *args, **kwargs):
--> 509   return instance.__call__(*args, **kwargs)
    510 
    511 

/opt/conda/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py in __call__(self, *args, **kwds)
    778       else:
    779         compiler = "nonXla"
--> 780         result = self._call(*args, **kwds)
    781 
    782       new_tracing_count = self._get_tracing_count()

/opt/conda/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py in _call(self, *args, **kwds)
    812       # In this case we have not created variables on the first call. So we can
    813       # run the first trace but we should fail if variables are created.
--> 814       results = self._stateful_fn(*args, **kwds)
    815       if self._created_variables:
    816         raise ValueError("Creating variables on a non-first call to a function"

/opt/conda/lib/python3.7/site-packages/tensorflow/python/eager
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值