【问题标题】:How to get a session in a custom defined metric?如何在自定义指标中获取会话?
【发布时间】:2019-02-12 13:08:27
【问题描述】:

我正在尝试在 Keras 中运行自定义指标。我成功了,但我不相信它的结果,所以我想检查一些值。麻烦的是一切都在张量中,我想将它们转换为 ndarrays 以检查它们。要转换它们,我必须有一个会话来评估它们。当我尝试使用 Keras 后端进行会话时,出现错误:

InvalidArgumentError(参见上面的回溯):您必须为占位符张量“Dense_1_target_1”提供一个值,其 dtype 为 float 和 shape [?,?] [[节点:Dense_1_target_1 = Placeholderdtype=DT_FLOAT, shape=[?,?], _device="/job:localhost/replica:0/task:0/device:GPU:0"]]

我唯一想要的是能够打印一些关于张量的信息:值、形状等。

from keras import backend as K

def t_zeros(Y_true, y_pred):
""" Just count # zero's in Y_true and try to print some info """
    threshold = 0.5
    true_zeros = K.less(Y_true, threshold) # element-wise True where Y_true < theshold
    true_zeros = K.cast(true_zeros, K.floatx())  # cast to 0.0 / 1.0
    n_zeros = K.sum(true_zeros)

    sess = K.get_session()
    y_t = Y_true.eval(session=sess) # <== error happens here
    print(y_t.shape)

    return n_zeros

【问题讨论】:

    标签: python tensorflow keras metrics


    【解决方案1】:

    请记住,tensorflow 使用延迟评估。

    所以你不能print 函数中的值。您需要创建一个打印节点并将其连接到整个图形中。

    类似的东西

    def t_zeros(Y_true, y_pred):
    """ Just count # zero's in Y_true and try to print some info """
        threshold = 0.5
        true_zeros = K.less(Y_true, threshold) # element-wise True where Y_true < theshold
        true_zeros = K.cast(true_zeros, K.floatx())  # cast to 0.0 / 1.0
        n_zeros = K.sum(true_zeros)
    
        return tf.Print(n_zeros, [n_zeros]) 
    
    ... 
    my_metric = t_zeros(Y_true, y_pred)  # Returns the tensor, but we need to make sure it's evaluated
    ...
    train_op = tf.group(train_op, my_metric) 
    

    如果您愿意,您可以将其连接到其他操作,只要确保它得到评估即可。

    【讨论】:

    • 我知道这一点,这正是我想将其转换为 numpy 数组的原因。这需要访问会话。这就是我想访问 tf.Session 的原因。
    【解决方案2】:

    如果您不介意使用 Tensorflow,您可以使用 tf.py_func 或其更现代的版本 tf.py_function 通过传递您拥有的任何张量来调用 python 函数,而无需会话。 (Tensorflow Documentation)。

    您传递给函数的张量预计会变成numpy 数据/数组。 py_funcpy_function 之间的唯一区别是,py_func 张量自动转换为 numpy 数组,而 py_function 仍然是张量,您必须手动调用 .numpy()

    Tensorflow 会为您将此调用添加到图形执行中。

    from keras import backend as K
    import tensorflow as tf
    
    def t_zeros(Y_true, y_pred):
    """ Just count # zero's in Y_true and try to print some info """
        threshold = 0.5
        true_zeros = K.less(Y_true, threshold) # element-wise True where Y_true < theshold
        true_zeros = K.cast(true_zeros, K.floatx())  # cast to 0.0 / 1.0
        n_zeros = K.sum(true_zeros)
    
        result = tf.py_function(some_function, [Y_true], [tf.float64, tf.int32]) # where the last argument
        # is an array representing the return type(s) of `some_function()`.
        # If `some_function` returns nothing, then you can do
        # tf.py_function(some_function, [Y_true], [])
    
        return n_zeros
    
    def some_function(input):
        '''
        If you called this through py_func, the inputs are already numpy arrays,
        if you called through py_function, they're Tensors and you have to call
        input.numpy().
        '''
        input = input.numpy()
        print(input)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 2020-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多