The reason for the above error when compiling custom functions is that tf2.x’s keras.compile does not support specific values by default
Questions
When using the wrapping method to customize the loss function of the keras model and need to calculate accuracy metrics such as precision or recall, or need to extract the specific values of the inputs y_true and y_prd (operations such as y_true.numpy()), an error message appears:
OperatorNotAllowedInGraphError: using a `tf.Tensor` as a Python `bool` is not allowed: AutoGraph did convert this function. This might indicate you are trying to use an unsupported feature.
Or
AttributeError: 'Tensor' object has no attribute 'numpy'
Solution:
Pass in parameters in the compile function:
run_eagerly=True
Reason:
Tf2.x enables eager mode by default, namely eager execution, that is, dynamic calculation graph. Compared with the static calculation graph of tf1.x, the advantage of eager mode is that it is convenient for debugging, which can easily print tensor values and evaluate results; and Numpy interacts well, and the conversion between tensor and ndarray is convenient and even universal. The tradeoff is that it runs significantly slower. After the static calculation graph is defined, it is almost always executed with C++ code on the tensorflow core, so the calculation efficiency is higher and the speed is faster.
Even so, run_eagerly defaults to False in the model.compile method, which means that the logic of the model is encapsulated in tf.function, which achieves faster computational efficiency (the autograph mechanism converts the dynamic computational graph through the @tf.function wrapper). is a static computation graph). But the @tf.function wrapper requires the function to use basic tf operations, not other operations in python or even functions from other packages, so the first error occurs when calling functions such as sklearn.metrics’ accuracy_score or imblearn.metrcis’ geometric_mean_score function. The second error occurs when using the y_true.numpy() method. The fundamental reason is that the model.compile method does not support the above operations after the static calculation graph converted by the @tf.function wrapper, although tf2.x enables the use of dynamic calculation graphs by default.
After passing run_eagerly=True to the model.compile method, the dynamic calculation graph is used to run, and the above operations can be performed normally. The disadvantage is that the dynamic calculation graph has the disadvantage of low operation efficiency.
Read More:
- [Solved] Networkx Error: Attributeerror: ‘graph’ object has no attribute ‘node’
- [Solved] Pytorch Error: AttributeError: ‘Tensor‘ object has no attribute ‘backword‘
- [Solved] module ‘numpy.random‘ has no attribute ‘default_rng‘ gensim.model
- [Solved] Pytorch Tensor to numpy error: RuntimeError: Can‘t call numpy() on Tensor that requires grad.报错
- [Solved] Pytorch-transformers Error: AttributeError: ‘str‘ object has no attribute ‘shape‘
- [Solved] AttributeError: ‘DataParallel‘ object has no attribute ‘save‘
- [Solved] Python Keras Error: AttributeError: ‘Sequential‘ object has no attribute ‘predict_classes‘
- [Solved] PyQt: RuntimeError: wrapped C/C++ object has been deleted & has no attribute of flush in python
- [Solved] AttributeError: ‘_IncompatibleKeys‘ object has no attribute ‘parameters‘
- [Solved] AttributeError: ‘_IncompatibleKeys’ object has no attribute
- AttributeError: DatetimeProperties object has no attribute
- [Solved] AttributeError: ‘HTMLWriter‘ object has no attribute ‘_temp_names‘
- Python 3.7 Error: AttributeError: ‘str‘ object has no attribute ‘decode‘ [How to Solve]
- [Solved] Python 3.6 Error: ‘dict’ object has no attribute ‘has_key’
- [Solved] AttributeError: ‘DataFrame‘ object has no attribute ‘map‘
- [Modified] AttributeError: ‘socket‘ object has no attribute ‘ioctl‘ python linux
- [Solved] LeNet Script Train Error: AttributeError: ‘DictIterator’ object has no attribute ‘get_next’
- [Solved] AttributeError: ‘PngImageFile‘ object has no attribute ‘imshow‘
- [Solved] AttributeError: ‘NoneType‘ object has no attribute ‘astype‘
- [Solved] AttributeError: ‘DataFrame‘ object has no attribute ‘tolist‘