import tensorflow as tf
import traceback
import contextlib
@tf.function
def double(a):
print("Tracing with", a)
return a + a
# Some helper code to demonstrate the kinds of errors you might encounter.
@contextlib.contextmanager
def assert_raises(error_class):
try:
yield
except error_class as e:
print('Caught expected exception \n {}:'.format(error_class))
traceback.print_exc(limit=2)
print("=======================")
print( e.node_def )
print( e.error_code )
print( e.experimental_payloads )
print( e.op )
print( e.message )
print("=======================")
OpError = tf.errors.OpError( e.node_def, e.op, e.message, e.error_code )
print( OpError.node_def )
print( OpError.error_code )
print( OpError.experimental_payloads )
print( OpError.message )
print( OpError.op )
print("=======================")
UnknownError = tf.errors.UnknownError( e.node_def, e.op, e.message )
print( UnknownError.node_def )
print( UnknownError.error_code )
print( UnknownError.experimental_payloads )
print( UnknownError.message )
print( UnknownError.op )
print("=======================")
AbortedError = tf.errors.AbortedError( e.node_def, e.op, e.message )
print( AbortedError.node_def )
print( AbortedError.error_code )
print( AbortedError.experimental_payloads )
print( AbortedError.message )
print( AbortedError.op )
print("=======================")
except Exception as e:
raise e
else:
raise Exception('Expected {} to be raised but no error was raised!'.format(
error_class))
double_strings = double.get_concrete_function(tf.constant("a"))
with assert_raises(tf.errors.InvalidArgumentError):
double_strings(tf.constant(1))
输出:与所有原因相比,ops错误告诉你操作TF发送了消息并返回ops。
Tracing with Tensor("a:0", shape=(), dtype=string)
Caught expected exception
<class 'tensorflow.python.framework.errors_impl.InvalidArgumentError'>:
Traceback (most recent call last):
File "F:\temp\Python\PyGame\FlappyBird\test_tf_errors.py", line 20, in assert_raises
yield
File "F:\temp\Python\PyGame\FlappyBird\test_tf_errors.py", line 67, in <module>
double_strings(tf.constant(1))
tensorflow.python.framework.errors_impl.InvalidArgumentError: cannot compute __inference_double_7 as input #0(zero-based) was expected to be a string tensor but is a int32 tensor [Op:__inference_double_7]
=======================
None
3
{}
None
cannot compute __inference_double_7 as input #0(zero-based) was expected to be a string tensor but is a int32 tensor [Op:__inference_double_7]
=======================
None
3
{}
cannot compute __inference_double_7 as input #0(zero-based) was expected to be a string tensor but is a int32 tensor [Op:__inference_double_7]
None
=======================
None
2
{}
cannot compute __inference_double_7 as input #0(zero-based) was expected to be a string tensor but is a int32 tensor [Op:__inference_double_7]
None
=======================
None
10
{}
cannot compute __inference_double_7 as input #0(zero-based) was expected to be a string tensor but is a int32 tensor [Op:__inference_double_7]
None
=======================
示例:我的实现,在生成错误类之前,错误并不总是返回,有时它只是一个异常,因为尚未收到它。
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Class / Functions
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
class Custom_Loss_Function(tf.keras.losses.Loss):
def __init__(self):
super().__init__()
def call(self, y_true, y_pred):
log_y_pred = tf.math.log(y_pred)
log_y_pred = tf.cast( log_y_pred, dtype=tf.float32 )
y_true = tf.cast( y_true, dtype=tf.float32 )
elements = tf.zeros([1, 1])
elements = -tf.math.multiply_no_nan(x=log_y_pred, y=y_true)
loss_value = tf.reduce_mean(tf.reduce_sum(elements,axis=1))
return cal( log_y_pred, y_true )
@tf.function
def cal( log_y_pred, y_true ):
elements = -tf.math.multiply_no_nan(x=log_y_pred, y=y_true)
with assert_raises(tf.errors.UnknownError):
loss_value = tf.reduce_mean(tf.reduce_sum(elements,axis=1)) + tf.ones([1,3])
print( 'loss_value: ' + str( loss_value ) )
return loss_value
@contextlib.contextmanager
def assert_raises(error_class):
try:
yield
except error_class as e:
print('Caught expected exception \n {}:'.format(error_class))
traceback.print_exc(limit=2)
print("=======================")
print( e.node_def )
print( e.error_code )
print( e.experimental_payloads )
print( e.op )
print( e.message )
except Exception as e:
raise e
else:
raise Exception('Expected {} to be raised but no error was raised!'.format(
error_class))
1条答案
按热度按时间e3bfsja21#
通过跟踪上下文和Ops错误,您可以从Ops错误中看到一些相同,一些不同,它返回失败的Ops,它告诉您请求发送和接收的实际任务。
示例:从ops返回中引发函数调用错误消息.
输出:与所有原因相比,ops错误告诉你操作TF发送了消息并返回ops。
示例:我的实现,在生成错误类之前,错误并不总是返回,有时它只是一个异常,因为尚未收到它。