jiangxinyang227 / textClassifier

tensorflow implementation
1.14k stars 555 forks source link

BERT中的bert_blstm_atten.py无法predict #15

Closed zysNLP closed 4 years ago

zysNLP commented 4 years ago

在执行predict.sh时:

python bert_blstm_atten.py \ --task_name=imdb \ --do_predict=True \ --data_dir=data/ \ --vocab_file=modelParams/uncased_L-12_H-768_A-12/vocab.txt \ --bert_config_file=modelParams/uncased_L-12_H-768_A-12/bert_config.json \ --init_checkpoint=blstm_output/ \ --max_seq_length=128 \ --output_dir=predicts/imdb/

报错!: Instructions for updating: Please use keras.layers.RNN(cell), which is equivalent to this API INFO:tensorflow:H shape: Tensor("Attention/add:0", shape=(?, 128, 256), dtype=float32) INFO:tensorflow:r shape: Tensor("Attention/MatMul_1:0", shape=(?, 256, 1), dtype=float32) INFO:tensorflow:sequeeze_r shape: Tensor("Attention/Squeeze:0", shape=(?, 256), dtype=float32) INFO:tensorflow:sentence embedding shape: Tensor("Attention/Tanh_1:0", shape=(?, 256), dtype=float32) INFO:tensorflow:output shape: Tensor("Attention/dropout/mul:0", shape=(?, 256), dtype=float32) INFO:tensorflow:output_w shape: <tf.Variable 'output/output_w:0' shape=(256, 2) dtype=float32_ref> INFO:tensorflow:predictions: Tensor("predictions:0", shape=(?,), dtype=int64) INFO:tensorflow:Error recorded from prediction_loop: Assignment map with scope only name Attention should map to scope only Attention/Variable. Should be 'scope/': 'other_scope/'. INFO:tensorflow:prediction_loop marked as finished WARNING:tensorflow:Reraising captured error Traceback (most recent call last): File "bert_blstm_atten.py", line 865, in tf.app.run() File "/home/zys/anaconda3/envs/TensorFlow/lib/python3.6/site-packages/tensorflow/python/platform/app.py", line 125, in run _sys.exit(main(argv)) File "bert_blstm_atten.py", line 847, in main for (i, prediction) in enumerate(result): File "/home/zys/anaconda3/envs/TensorFlow/lib/python3.6/site-packages/tensorflow/contrib/tpu/python/tpu/tpu_estimator.py", line 2500, in predict rendezvous.raise_errors() File "/home/zys/anaconda3/envs/TensorFlow/lib/python3.6/site-packages/tensorflow/contrib/tpu/python/tpu/error_handling.py", line 128, in raise_errors six.reraise(typ, value, traceback) File "/home/zys/anaconda3/envs/TensorFlow/lib/python3.6/site-packages/six.py", line 693, in reraise raise value File "/home/zys/anaconda3/envs/TensorFlow/lib/python3.6/site-packages/tensorflow/contrib/tpu/python/tpu/tpu_estimator.py", line 2494, in predict yield_single_examples=yield_single_examples): File "/home/zys/anaconda3/envs/TensorFlow/lib/python3.6/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 611, in predict features, None, model_fn_lib.ModeKeys.PREDICT, self.config) File "/home/zys/anaconda3/envs/TensorFlow/lib/python3.6/site-packages/tensorflow/contrib/tpu/python/tpu/tpu_estimator.py", line 2251, in _call_model_fn config) File "/home/zys/anaconda3/envs/TensorFlow/lib/python3.6/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1112, in _call_model_fn model_fn_results = self._model_fn(features=features, kwargs) File "/home/zys/anaconda3/envs/TensorFlow/lib/python3.6/site-packages/tensorflow/contrib/tpu/python/tpu/tpu_estimator.py", line 2534, in _model_fn features, labels, is_export_mode=is_export_mode) File "/home/zys/anaconda3/envs/TensorFlow/lib/python3.6/site-packages/tensorflow/contrib/tpu/python/tpu/tpu_estimator.py", line 1323, in call_without_tpu return self._call_model_fn(features, labels, is_export_mode=is_export_mode) File "/home/zys/anaconda3/envs/TensorFlow/lib/python3.6/site-packages/tensorflow/contrib/tpu/python/tpu/tpu_estimator.py", line 1593, in _call_model_fn estimator_spec = self._model_fn(features=features, kwargs) File "bert_blstm_atten.py", line 539, in model_fn tf.train.init_from_checkpoint(init_checkpoint, assignment_map) File "/home/zys/anaconda3/envs/TensorFlow/lib/python3.6/site-packages/tensorflow/python/training/checkpoint_utils.py", line 190, in init_from_checkpoint _init_from_checkpoint, args=(ckpt_dir_or_file, assignment_map)) File "/home/zys/anaconda3/envs/TensorFlow/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py", line 1516, in merge_call return self._merge_call(merge_fn, args, kwargs) File "/home/zys/anaconda3/envs/TensorFlow/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py", line 1524, in _merge_call return merge_fn(self._distribution_strategy, *args, **kwargs) File "/home/zys/anaconda3/envs/TensorFlow/lib/python3.6/site-packages/tensorflow/python/training/checkpoint_utils.py", line 246, in _init_from_checkpoint scopes, tensor_name_in_ckpt)) ValueError: Assignment map with scope only name Attention should map to scope only Attention/Variable. Should be 'scope/': 'other_scope/'.

在do_train和do_eval时都没问题,我写了predict.sh执行就不行了,看起来像缺少了一些variable,能看下是怎么回事吗?谢谢!

zysNLP commented 4 years ago

对于这个错误,我修改了bert/modeling.py中339行的 assignment_map[name] = name 改为 assignment_map[name] = name_to_variable[name] 之后报了另一个错。发现没法产生test_results.tsv 我又修改了blstm_atten.py中的137行,在logits后面加入一行: probabilities = tf.nn.softmax(logits, axis=-1) 修改了bert_blstm_atten.py中的model_fn_builder-->model_fn函数,在else最后将 predictions=predictions, 修改为 predictions={"probabilities": probabilities}, 这样,就能运行最后的predict.sh并产生test_results.tsv了,有点好奇作者为什么要改动最后一点,是故意不想输出tsv么?