macanv / BERT-BiLSTM-CRF-NER

Tensorflow solution of NER task Using BiLSTM-CRF model with Google BERT Fine-tuning And private Server services
https://github.com/macanv/BERT-BiLSMT-CRF-NER
4.68k stars 1.25k forks source link

terminal_predict.py: FailedPreconditionError: Attempting to use uninitialized value #326

Open iblackholmes opened 4 years ago

iblackholmes commented 4 years ago

作者你好,我运行bert-base-ner-train之后,再运行terminal_predict.py,返回了FailedPreconditionError: Attempting to use uninitialized value的错误,请问这是什么原因呢?

这是我工作空间的目录结构(为了节省展示空间,省略了一部分的目录)

----BERT-BiLSTM-CRF-NER\
    |----build.sh
    |----thu_classification.py
    |----data_process.py
    |----README.md
    |----requirement.txt
    |----run.py
    |----client_test.py
    |----setup.py
    |----bert_base\
    |    | ...
    |----NERdata\
    |    |----test.txt
    |    |----train.txt
    |    |----dev.txt
    |----pictures\
    |    | ...
    |----output\
    |    |----label_list.pkl
    |    |----label2id.pkl
    |    |----checkpoint\
    |    |    |----ner_model.pb
    |    |----result_dir\
    |    |    |----label_list.pkl
    |    |    |----train.tf_record
    |    |    |----label2id.pkl
    |    |    |----eval.tf_record
    |    |    |----events.out.tfevents.1588175086.01179c4756f6
    |    |    |----graph.pbtxt
    |    |    |----eval\
    |    |    |    |----events.out.tfevents.1588175288.01179c4756f6
    |    |    |----model.ckpt-24500.data-00000-of-00001
    |    |    |----model.ckpt-24500.index
    |    |    |----model.ckpt-24500.meta
    |    |    |----model.ckpt-25000.data-00000-of-00001
    |    |    |----model.ckpt-25000.index
    |    |    |----model.ckpt-25000.meta
    |    |    |----model.ckpt-25500.index
    |    |    |----model.ckpt-25500.meta
    |    |    |----model.ckpt-26000.data-00000-of-00001
    |    |    |----model.ckpt-26000.index
    |    |    |----model.ckpt-26000.meta
    |    |    |----model.ckpt-26080.index
    |    |    |----model.ckpt-26080.meta
    |    |    |----predict.tf_record
    |    |    |----token_test.txt
    |    |    |----label_test.txt
    |    |    |----predict_score.txt
    |    |    |----checkpoint
    |    |    |----model.ckpt-26080.data-00000-of-00001
    |    |    |----model.ckpt-25500.data-00000-of-00001
    |----bert_base.egg-info\
    |    | ...
    |----build\
    |    | ...
    |----dist\
    |    |----bert_base-0.0.9-py3.6.egg
    |----bertModel\
    |    |----vocab.txt
    |    |----bert_config.json
    |    |----bert_model.ckpt.data-00000-of-00001
    |    |----bert_model.ckpt.meta
    |    |----bert_model.ckpt.index
    |----terminal_predict.py

运行bert-base-ner-train采用的参数

bert-base-ner-train \
    -data_dir ./NERdata\
    -output_dir ./output/result_dir\
    -init_checkpoint ./bertModel/bert_model.ckpt \
    -bert_config_file ./bertModel/bert_config.json \
    -vocab_file ./bertModel/vocab.txt \
    -max_seq_length 128 \
    -batch_size 8 \

训练结束后,运行terminal_predict.py的报错

...
going to restore checkpoint
WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
WARNING:tensorflow:From /content/drive/Shared drives/teamDrive/BERT-BiLSTM-CRF-NER/bert_base/bert/modeling.py:673: dense (from tensorflow.python.layers.core) is deprecated and will be removed in a future version.
Instructions for updating:
Use keras.layers.dense instead.
WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/contrib/crf/python/ops/crf.py:567: dynamic_rnn (from tensorflow.python.ops.rnn) is deprecated and will be removed in a future version.
Instructions for updating:
Please use `keras.layers.RNN(cell)`, which is equivalent to this API
WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn.py:626: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
{1: 'X', 2: 'B-PER', 9: 'I-PER', 5: 'O', 6: 'I-LOC', 7: 'B-ORG', 8: 'I-ORG', 4: '[SEP]', 3: '[CLS]', 10: 'B-LOC'}
input the test sentence:
阿萨姆奶茶比喜茶好喝多了
2020-04-30 04:57:09.061334: I tensorflow/stream_executor/dso_loader.cc:152] successfully opened CUDA library libcublas.so.10.0 locally
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py", line 1334, in _do_call
    return fn(*args)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py", line 1319, in _run_fn
    options, feed_dict, fetch_list, target_list, run_metadata)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py", line 1407, in _call_tf_sessionrun
    run_metadata)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value bert/encoder/layer_5/attention/output/LayerNorm/beta
     [[{{node bert/encoder/layer_5/attention/output/LayerNorm/beta/read}}]]
     [[{{node rnn_1/while/Switch_2}}]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "terminal_predict.py", line 350, in <module>
    predict_online()
  File "terminal_predict.py", line 113, in predict_online
    pred_ids_result = sess.run([pred_ids], feed_dict)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py", line 929, in run
    run_metadata_ptr)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py", line 1152, in _run
    feed_dict_tensor, options, run_metadata)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py", line 1328, in _do_run
    run_metadata)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py", line 1348, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value bert/encoder/layer_5/attention/output/LayerNorm/beta
     [[node bert/encoder/layer_5/attention/output/LayerNorm/beta/read (defined at /content/drive/Shared drives/teamDrive/BERT-BiLSTM-CRF-NER/bert_base/bert/modeling.py:366) ]]
     [[node rnn_1/while/Switch_2 (defined at /content/drive/Shared drives/teamDrive/BERT-BiLSTM-CRF-NER/bert_base/train/lstm_crf_layer.py:62) ]]

Caused by op 'bert/encoder/layer_5/attention/output/LayerNorm/beta/read', defined at:
  File "terminal_predict.py", line 64, in <module>
    dropout_rate=   1.0
  File "/content/drive/Shared drives/teamDrive/BERT-BiLSTM-CRF-NER/bert_base/train/models.py", line 89, in create_model
    use_one_hot_embeddings=use_one_hot_embeddings
  File "/content/drive/Shared drives/teamDrive/BERT-BiLSTM-CRF-NER/bert_base/bert/modeling.py", line 217, in __init__
    do_return_all_layers=True)
  File "/content/drive/Shared drives/teamDrive/BERT-BiLSTM-CRF-NER/bert_base/bert/modeling.py", line 865, in transformer_model
    attention_output = layer_norm(attention_output + layer_input)
  File "/content/drive/Shared drives/teamDrive/BERT-BiLSTM-CRF-NER/bert_base/bert/modeling.py", line 366, in layer_norm
    inputs=input_tensor, begin_norm_axis=-1, begin_params_axis=-1, scope=name)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/contrib/framework/python/ops/arg_scope.py", line 182, in func_with_args
    return func(*args, **current_args)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/contrib/layers/python/layers/layers.py", line 2300, in layer_norm
    trainable=trainable)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/contrib/framework/python/ops/arg_scope.py", line 182, in func_with_args
    return func(*args, **current_args)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/contrib/framework/python/ops/variables.py", line 350, in model_variable
    aggregation=aggregation)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/contrib/framework/python/ops/arg_scope.py", line 182, in func_with_args
    return func(*args, **current_args)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/contrib/framework/python/ops/variables.py", line 277, in variable
    aggregation=aggregation)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/variable_scope.py", line 1479, in get_variable
    aggregation=aggregation)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/variable_scope.py", line 1220, in get_variable
    aggregation=aggregation)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/variable_scope.py", line 547, in get_variable
    aggregation=aggregation)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/variable_scope.py", line 499, in _true_getter
    aggregation=aggregation)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/variable_scope.py", line 911, in _get_single_variable
    aggregation=aggregation)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/variables.py", line 213, in __call__
    return cls._variable_v1_call(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/variables.py", line 176, in _variable_v1_call
    aggregation=aggregation)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/variables.py", line 155, in <lambda>
    previous_getter = lambda **kwargs: default_variable_creator(None, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/variable_scope.py", line 2495, in default_variable_creator
    expected_shape=expected_shape, import_scope=import_scope)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/variables.py", line 217, in __call__
    return super(VariableMetaclass, cls).__call__(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/variables.py", line 1395, in __init__
    constraint=constraint)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/variables.py", line 1557, in _init_from_args
    self._snapshot = array_ops.identity(self._variable, name="read")
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/dispatch.py", line 180, in wrapper
    return target(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/array_ops.py", line 81, in identity
    ret = gen_array_ops.identity(input, name=name)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 3890, in identity
    "Identity", input=input, name=name)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py", line 788, in _apply_op_helper
    op_def=op_def)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/deprecation.py", line 507, in new_func
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py", line 3300, in create_op
    op_def=op_def)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py", line 1801, in __init__
    self._traceback = tf_stack.extract_stack()

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value bert/encoder/layer_5/attention/output/LayerNorm/beta
     [[node bert/encoder/layer_5/attention/output/LayerNorm/beta/read (defined at /content/drive/Shared drives/teamDrive/BERT-BiLSTM-CRF-NER/bert_base/bert/modeling.py:366) ]]
     [[node rnn_1/while/Switch_2 (defined at /content/drive/Shared drives/teamDrive/BERT-BiLSTM-CRF-NER/bert_base/train/lstm_crf_layer.py:62) ]]
Orange-jam commented 2 years ago

请问您解决了嘛?