bojone / bert4keras

keras implement of transformers for humans
https://kexue.fm/archives/6915
Apache License 2.0
5.37k stars 928 forks source link

tensorflow2.1 使用的是第一个获取特征向量的例子,但是保存模型再加载的时候报错 #137

Closed ljx411 closed 3 years ago

ljx411 commented 4 years ago

提问时请尽可能提供如下信息:

基本信息

核心代码

model.save('test.model')
del model
model = keras.models.load_model('test.model')
print(model.predict([np.array([token_ids]), np.array([segment_ids])]))

输出信息

2020-04-28 10:01:45.364435: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2

 ===== predicting =====

[[[ 1.8730946e-04  2.0689853e-01 -3.4783535e-02 ... -3.2371962e-01
    3.5034758e-01 -3.4660268e-01]
  [ 1.6164616e-01  8.9288825e-01  7.3500529e-02 ... -3.6406735e-01
    2.7639452e-01  4.1811669e-01]
  [-1.4505102e-01  1.1246700e+00 -8.6172533e-01 ... -3.2948539e-01
    1.1295428e+00  7.1834779e-01]
  [ 5.6845403e-01  1.2013958e+00  1.0758492e-01 ... -4.3141428e-01
    9.5414519e-02  7.7629066e-01]
  [ 9.0089157e-02  4.7921899e-01 -6.9670230e-03 ... -2.8715193e-01
    1.2070973e+00  1.0765176e+00]
  [-5.7512149e-04  2.0681705e-01 -3.3886090e-02 ... -3.2548040e-01
    3.5285094e-01 -3.5538089e-01]]]

 ===== reloading and predicting =====

2020-04-28 10:02:18.732265: W tensorflow/python/util/util.cc:319] Sets are not currently considered sequences, but this may change in the future, so consider avoiding using them.
WARNING:tensorflow:From D:\anaconda3\envs\tensor2.0\lib\site-packages\tensorflow_core\python\ops\resource_variable_ops.py:1786: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.
Instructions for updating:
If using Keras pass *_constraint arguments to layers.
Traceback (most recent call last):
  File "D:/tensorflow_examples/tensor2.0_practice/even_classfier/bert4keras_example.py", line 40, in <module>
    model = keras.models.load_model('test.model')
  File "D:\anaconda3\envs\tensor2.0\lib\site-packages\tensorflow_core\python\keras\saving\save.py", line 150, in load_model
    return saved_model_load.load(filepath, compile)
  File "D:\anaconda3\envs\tensor2.0\lib\site-packages\tensorflow_core\python\keras\saving\saved_model\load.py", line 89, in load
    model = tf_load.load_internal(path, loader_cls=KerasObjectLoader)
  File "D:\anaconda3\envs\tensor2.0\lib\site-packages\tensorflow_core\python\saved_model\load.py", line 552, in load_internal
    export_dir)
  File "D:\anaconda3\envs\tensor2.0\lib\site-packages\tensorflow_core\python\keras\saving\saved_model\load.py", line 119, in __init__
    self._finalize()
  File "D:\anaconda3\envs\tensor2.0\lib\site-packages\tensorflow_core\python\keras\saving\saved_model\load.py", line 157, in _finalize
    created_layers={layer.name: layer for layer in node.layers})
  File "D:\anaconda3\envs\tensor2.0\lib\site-packages\tensorflow_core\python\keras\engine\network.py", line 1903, in reconstruct_from_config
    process_node(layer, node_data)
  File "D:\anaconda3\envs\tensor2.0\lib\site-packages\tensorflow_core\python\keras\engine\network.py", line 1851, in process_node
    output_tensors = layer(input_tensors, **kwargs)
  File "D:\anaconda3\envs\tensor2.0\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 773, in __call__
    outputs = call_fn(cast_inputs, *args, **kwargs)
  File "D:\anaconda3\envs\tensor2.0\lib\site-packages\tensorflow_core\python\keras\saving\saved_model\utils.py", line 59, in return_outputs_and_add_losses
    outputs, losses = fn(inputs, *args, **kwargs)
  File "D:\anaconda3\envs\tensor2.0\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 568, in __call__
    result = self._call(*args, **kwds)
  File "D:\anaconda3\envs\tensor2.0\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 615, in _call
    self._initialize(args, kwds, add_initializers_to=initializers)
  File "D:\anaconda3\envs\tensor2.0\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 497, in _initialize
    *args, **kwds))
  File "D:\anaconda3\envs\tensor2.0\lib\site-packages\tensorflow_core\python\eager\function.py", line 2389, in _get_concrete_function_internal_garbage_collected
    graph_function, _, _ = self._maybe_define_function(args, kwargs)
  File "D:\anaconda3\envs\tensor2.0\lib\site-packages\tensorflow_core\python\eager\function.py", line 2703, in _maybe_define_function
    graph_function = self._create_graph_function(args, kwargs)
  File "D:\anaconda3\envs\tensor2.0\lib\site-packages\tensorflow_core\python\eager\function.py", line 2593, in _create_graph_function
    capture_by_value=self._capture_by_value),
  File "D:\anaconda3\envs\tensor2.0\lib\site-packages\tensorflow_core\python\framework\func_graph.py", line 978, in func_graph_from_py_func
    func_outputs = python_func(*func_args, **func_kwargs)
  File "D:\anaconda3\envs\tensor2.0\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 439, in wrapped_fn
    return weak_wrapped_fn().__wrapped__(*args, **kwds)
  File "D:\anaconda3\envs\tensor2.0\lib\site-packages\tensorflow_core\python\saved_model\function_deserialization.py", line 262, in restored_function_body
    "\n\n".join(signature_descriptions)))
ValueError: Could not find matching function to call loaded from the SavedModel. Got:
  Positional arguments (2 total):
    * Tensor("inputs:0", shape=(None, None), dtype=float32)
    * embedding
  Keyword arguments: {}

Expected these arguments to match one of the following 0 option(s):

Process finished with exit code 1

自我尝试

不管什么问题,请先尝试自行解决,“万般努力”之下仍然无法解决再来提问。此处请贴上你的努力过程。 自己尝试了删除keras,修改源代码,使用keras是可以的,但是用tf.keras就会报错,也用过tensorflow2.0版本,只保存权重参数是可以加载的,但是直接保存时不行的,因为我想用tensorflow2.0的service,所以想直接用save但是发现报错,对了我也用centos做过尝试也是不行,望解答,谢谢

bojone commented 4 years ago

还没测过tf 2.1,抱歉,建议先用更旧版本tf

ljx411 commented 4 years ago

还没测过tf 2.1,抱歉,建议先用更旧版本tf

十分感谢,我把tensorflow退回2.0就可以了,不知道2.1的问题该怎么解决。等大佬更新版本吧

bojone commented 4 years ago

我找了一下,原来这是tf 2.1的bug.... https://github.com/tensorflow/tensorflow/issues/35888#issuecomment-582142805

真的是无语了,这么显著的bug都能出现,tf的开发者玩火和作死功力越来越强了...

ljx411 commented 4 years ago

我找了一下,原来这是tf 2.1的bug.... tensorflow/tensorflow#35888 (comment)

真的是无语了,这么显著的bug都能出现,tf的开发者玩火和作死功力越来越强了...

原来是遇到BUG了,这个BUG这么多回复了,这些开发者不上心啊