guillaumegenthial / tf_ner

Simple and Efficient Tensorflow implementations of NER models with tf.estimator and tf.data
Apache License 2.0
923 stars 275 forks source link

TypeError: Using a `tf.Tensor` as a Python `bool` is not allowed. #9

Closed aetherealtea closed 6 years ago

aetherealtea commented 6 years ago

I would be grateful if you could help me with this problem. I tried to execute model training with example data and received the error:

Traceback (most recent call last): File "/opt/project/models/lstm_crf/main.py", line 171, in tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec) File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/estimator/training.py", line 471, in train_and_evaluate return executor.run() File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/estimator/training.py", line 610, in run return self.run_local() File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/estimator/training.py", line 711, in run_local saving_listeners=saving_listeners) File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/estimator/estimator.py", line 356, in train loss = self._train_model(input_fn, hooks, saving_listeners) File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/estimator/estimator.py", line 1181, in _train_model return self._train_model_default(input_fn, hooks, saving_listeners) File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/estimator/estimator.py", line 1211, in _train_model_default features, labels, model_fn_lib.ModeKeys.TRAIN, self.config) File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/estimator/estimator.py", line 1169, in _call_model_fn model_fn_results = self._model_fn(features=features, **kwargs) File "/opt/project/models/lstm_crf/main.py", line 117, in model_fn 'precision': tf.metrics.precision(tags, pred_ids, num_tags, indices, weights), File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/metrics_impl.py", line 2024, in precision if updates_collections: File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 669, in bool raise TypeError("Using a tf.Tensor as a Python bool is not allowed. " TypeError: Using a tf.Tensor as a Python bool is not allowed. Use if t is not None: instead of if t: to test if a tensor is defined, and use TensorFlow ops such as tf.cond to execute subgraphs conditioned on the value of a tensor.

I'm using Docker with tensorflow:latest-gpu-py3.

guillaumegenthial commented 6 years ago

I'm curious about this line

File "/opt/project/models/lstm_crf/main.py", line 117, in model_fn 'precision': tf.metrics.precision(tags, pred_ids, num_tags, indices, weights),

it does seem that you changed the code?

You need to use tf_metrics as explained in the README.md (or just comment the metric lines) if you don't care about real-time metrics.

aetherealtea commented 6 years ago

Double-checked tf_metrics, it solved the problem, thank you.