keras 在训练过程中,tensorflow 有没有办法迭代所有的训练误差?

jfewjypa  于 2022-12-13  发布在  其他
关注(0)|答案(1)|浏览(134)

我正在尝试设计一个新的损失函数,对一个训练批中的每个数据迭代所有的训练错误,并根据不同错误的大小计算新的损失。有没有什么方法可以实现呢?因为在设计损失函数时,error.shape[0]将为None,所以传统的方法可能无法用于迭代错误。
error=Ypred-Ytrue,而且它们的shape[0]都是None,所以我现在不知道怎么迭代错误,我需要知道训练过程中的错误,然后把它们的大小和一个特定的值比较,知道有多少个错误大于这个值,然后根据这个值计算损失。

e3bfsja2

e3bfsja21#

通过跟踪上下文和Ops错误,您可以从Ops错误中看到一些相同,一些不同,它返回失败的Ops,它告诉您请求发送和接收的实际任务。

  1. TF.Function:在本页的同一个例子中,我描述了更多关于ops in和ops out错误函数的信息,一些继承类也告诉了你同样的信息。
  2. Errors causes mapping:错误函数(类)和引用。
    示例:从ops返回中引发函数调用错误消息.
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))

相关问题