bert4keras
bert4keras copied to clipboard
AttributeError: '_thread._local' object has no attribute 'value'
提问时请尽可能提供如下信息:
基本信息
- 你使用的操作系统:
- 你使用的Python版本: 3.8
- 你使用的Tensorflow版本: 2.2
- 你使用的Keras版本: 2.3.1
- 你使用的bert4keras版本: 0.10.7
- 你使用纯keras还是tf.keras: keras
- 你加载的预训练模型:bert
核心代码
# 请在此处贴上你的核心代码。
# 请尽量只保留关键部分,不要无脑贴全部代码。
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
config = tf.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1)
sess = tf.Session(config=config)
tf.keras.backend.set_session(sess)
graph = tf.get_default_graph()
def gen_synonyms(text, num_candidates=10, num_questions=5, mask_idxs=[]):
''''含义: 产生sent的n个相似句,然后返回最相似的k个。
做法:用seq2seq生成,并用encoder算相似度并排序。
'''
with graph.as_default():
tf.keras.backend.set_session(sess)
r = synonyms_generator.generate(text, num_candidates, mask_idxs=mask_idxs)
r = [i for i in set(r) if i != text]
r = [text] + r
X, S = [], []
for t in r:
x, s = tokenizer.encode(t)
X.append(x)
S.append(s)
X = sequence_padding(X)
S = sequence_padding(S)
Z = encoder.predict([X, S])
Z /= (Z**2).sum(axis=1, keepdims=True)**0.5
argsort = np.dot(Z[1:], -Z[0]).argsort()
return [r[i + 1] for i in argsort[:num_questions]]
输出信息
# 请在此处贴上你的调试输出
Traceback (most recent call last):
File "/home/user/.local/lib/python3.8/site-packages/gradio/app.py", line 199, in predict
prediction, durations = await run_in_threadpool(
File "/home/user/.local/lib/python3.8/site-packages/starlette/concurrency.py", line 39, in run_in_threadpool
return await anyio.to_thread.run_sync(func, *args)
File "/home/user/.local/lib/python3.8/site-packages/anyio/to_thread.py", line 28, in run_sync
return await get_asynclib().run_sync_in_worker_thread(func, *args, cancellable=cancellable,
File "/home/user/.local/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 818, in run_sync_in_worker_thread
return await future
File "/home/user/.local/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 754, in run
result = context.run(func, *args)
File "/home/user/.local/lib/python3.8/site-packages/gradio/interface.py", line 530, in process
predictions, durations = self.run_prediction(
File "/home/user/.local/lib/python3.8/site-packages/gradio/interface.py", line 487, in run_prediction
prediction = predict_fn(*processed_input)
File "app.py", line 95, in gen_synonyms
r = synonyms_generator.generate(text, num_candidates, mask_idxs=mask_idxs)
File "app.py", line 77, in generate
output_ids = self.random_sample([token_ids, segment_ids], n, topp=topp) # 基于随机采样
File "/home/user/.local/lib/python3.8/site-packages/bert4keras/snippets.py", line 627, in random_sample
probas, states = self.predict(
File "/home/user/.local/lib/python3.8/site-packages/bert4keras/snippets.py", line 525, in new_predict
prediction = predict(self, inputs, output_ids, states)
File "app.py", line 70, in predict
return self.last_token(seq2seq).predict([token_ids, segment_ids])
File "/home/user/.local/lib/python3.8/site-packages/bert4keras/snippets.py", line 552, in last_token
outputs = [
File "/home/user/.local/lib/python3.8/site-packages/bert4keras/snippets.py", line 553, in
keras.layers.Lambda(lambda x: x[:, -1])(output)
File "/home/user/.local/lib/python3.8/site-packages/keras/backend/tensorflow_backend.py", line 73, in symbolic_fn_wrapper
if _SYMBOLIC_SCOPE.value:
AttributeError: '_thread._local' object has no attribute 'value'
自我尝试
不管什么问题,请先尝试自行解决,“万般努力”之下仍然无法解决再来提问。此处请贴上你的努力过程。 import tensorflow.compat.v1 as tf tf.disable_v2_behavior()
import keras.backend.tensorflow_backend as tb tb._SYMBOLIC_SCOPE.value = True
降低keras版本都不行
https://github.com/bojone/bert4keras/issues/381 应该也是多线程的问题
参考这里:https://github.com/bojone/bert4keras/blob/3161648d20bfe7f501297d4bb33a0bad1ffd4002/bert4keras/snippets.py#L808
自我尝试
不管什么问题,请先尝试自行解决,“万般努力”之下仍然无法解决再来提问。此处请贴上你的努力过程。 import tensorflow.compat.v1 as tf tf.disable_v2_behavior()
import keras.backend.tensorflow_backend as tb tb._SYMBOLIC_SCOPE.value = True
降低keras版本都不行
您好,请问您这个问题解决了吗?
我也遇到这个问题,我是通过uvicorn+fastapi+ner模型部署遇到的。目前版本也降了,上面的方法试了一遍,目前无解
在predict代码前面加上: import keras.backend.tensorflow_backend as tb tb._SYMBOLIC_SCOPE.value = True 可解
我也遇到这个问题,我是通过uvicorn+fastapi+ner模型部署遇到的。目前版本也降了,上面的方法试了一遍,目前无解
同fastapi,请问你后来解决了吗
我也遇到这个问题,我是通过uvicorn+fastapi+ner模型部署遇到的。目前版本也降了,上面的方法试了一遍,目前无解
同fastapi,请问你后来解决了吗
tf1.15,keras2.3.1,uvicorn + fastapi异步部署; 模型加载完后加一句:model_graph = tf.compat.v1.get_default_graph() 模型每次推理前加一句:with model_graph.as_default(): model.predict() 也可解