keras 尝试加载已保存的Tensorflow埃尔莫模型,但得到“TypeError:'str'对象不可调用”

g0czyy6m  于 2022-11-13  发布在  其他
关注(0)|答案(2)|浏览(230)

我尝试在一个与我训练的函数不同的函数中加载一个保存的Tensorflow埃尔莫模型,因为我想使用该模型进行多次预测,而不必每次都训练它。我的(简化)代码如下:
第一个
最后,在训练模型之后,这会给我错误“TypeError:“str”对象不可调用”,并返回以下跟踪:

Traceback (most recent call last):
  File "usc_coordinator.py", line 62, in <module>
    run_usc_coordinator(fIn, fOut, mode)
  File "usc_coordinator.py", line 32, in run_usc_coordinator
    user_story_builder(fast_mode, file_in)
  File "/home/ubuntu/PA/PA_AI4US/PythonVersion/src/builder.py", line 45, in builder
    print('PRED_LABELS: ', predict_labels(lst))
  File "/home/ubuntu/PA/PA_AI4US/PythonVersion/src/word_classifier.py", line 161, in predict_labels
    loaded_model = tf.keras.models.model_from_json(loaded_model_json, custom_objects={'elmo_embedding': elmo_embedding})
  File "/home/ubuntu/.local/lib/python3.8/site-packages/tensorflow/python/keras/saving/model_config.py", line 122, in model_from_json
    return deserialize(config, custom_objects=custom_objects)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/tensorflow/python/keras/layers/serialization.py", line 171, in deserialize
    return generic_utils.deserialize_keras_object(
  File "/home/ubuntu/.local/lib/python3.8/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 354, in deserialize_keras_object
    return cls.from_config(
  File "/home/ubuntu/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py", line 616, in from_config
    input_tensors, output_tensors, created_layers = reconstruct_from_config(
  File "/home/ubuntu/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py", line 1214, in reconstruct_from_config
    process_node(layer, node_data)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py", line 1162, in process_node
    output_tensors = layer(input_tensors, **kwargs)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer_v1.py", line 776, in __call__
    outputs = call_fn(cast_inputs, *args, **kwargs)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/tensorflow/python/keras/layers/core.py", line 903, in call
    result = self.function(inputs, **kwargs)
  File "/home/ubuntu/PA/PA_AI4US/PythonVersion/src/word_classifier.py", line 101, in <lambda>
    embedding = Lambda(lambda text, : elmo_embedding(text), output_shape=(MAX_LEN, 1024))(input_text, )
TypeError: 'str' object is not callable

我的版本是:

Python 3.8.10
Keras 2.3.0
Tensorflow 2.3.1
Tensorflow-hub 0.10.0

我猜这个错误是由变量input_text设置为dtype tf. string引起的。但是,我不知道如何在不破坏训练序列的情况下处理这个错误。
我希望有人能帮忙!

9gm1akwq

9gm1akwq1#

这是tensorflow v2.3.1中的一个错误:
加载带有Lambda图层的模型导致“str”对象不可调用异常#46659
https://github.com/tensorflow/tensorflow/issues/46659

b4qexyjb

b4qexyjb2#

您应该将字符串更改为其他内容,例如,如果我必须计算2+2,则这将是错误的代码:

x = "2"
print(x + 2)

这说明:

Traceback (most recent call):
   File "main.py", line 2, in <module>
      print(x + 2)
TypeError: can only concatenate str (not "int") to str

那我们该怎么办?把它转换成别的东西

x = "2"
y = int(x)
print(y + 2)

输出:4个
int =整数,float =浮点数。
编辑:你不能把一些东西转换成其他的东西,比如

x = "This is an example"
y = int(x)

这将显示:

Traceback (most recent call last):
   File "main.py", line 2, in <module>
      y = int(x)
ValueError: invalid literal for int() with base10: 'This is an example'

#For a string to array
x = "1-2"
y = x.split("-")
print(y)

y将为[“1”,“2”]
或者,如果您不知道缩写形式,您可以使用完整形式
第32行,'str'是不可调用fast_mode,file_In将是字符串,其他行也是如此

相关问题