如何在不启用keras中的run_标志的情况下将张量转换为numpy数组

2024-04-29 12:37:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写自己的度量回调函数,其中我使用sklearn计算度量,为此,我需要将y_true和y_pred张量作为numpy数组。我的函数如下所示:

def precision_macro(y_true, y_pred):
    # get the y_true and y_pred tensors as 1-D numpy array
    y_true_array = np.array(true)
    y_pred_array = np.array(pred)
    ....................
    ....................
        CALCULATIONS
    ....................
    ....................
    precision = precision_score(y_true_array, y_pred_array, average="macro", zero_division=0)
    return precision

如果我在编译调用期间设置run_eargly=True如下所示,则一切正常:

model.compile(optimizer=keras.optimizers.RMSprop(learning_rate=lr),
                      loss='binary_crossentropy',
                      metrics=model_metrics,
                      run_eagerly=True) 

但是,这是非常昂贵的,而且比将标志设置为False要慢,但是如果我不将标志设置为True,那么转换就会出现问题。以下是我在没有将run_Eagrey标志设置为True的情况下尝试过的一些事情,但都不起作用:

如果我只是没有设置run_急切地标记,我会得到以下错误

NotImplementedError: Cannot convert a symbolic Tensor (ExpandDims:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported

然后我试着

import tensorflow.keras.backend as K

def precision_macro(y_true, y_pred):
    # get the y_true and y_pred tensors as 1-D numpy array
    y_true_array = K.eval(y_true)
    y_pred_array = K.eval(y_pred)
    ....................

或者我试着调用张量上的numpy函数

def precision_macro(y_true, y_pred):
    # get the y_true and y_pred tensors as 1-D numpy array
    y_true_array = y_true.numpy()
    y_pred_array = y_pred.numpy()
    ....................

我还尝试在这样的会话中运行此命令:

import tensorflow.keras.backend as K
import tensorflow as tf

def precision_macro(y_true, y_pred):
    sess = tf.compat.v1.Session()
    # get the y_true and y_pred tensors as 1-D numpy array
    with sess.as_default():
       y_true_array = K.eval(y_true)
       y_pred_array = K.eval(y_pred)
    ....................

对于所有情况,我都会得到以下错误

AttributeError: 'Tensor' object has no attribute 'numpy'

我试着这样运行它:

import tensorflow.keras.backend as K
import tensorflow as tf

def precision_macro(y_true, y_pred):
    sess = tf.compat.v1.Session()
    # get the y_true and y_pred tensors as 1-D numpy array
    with sess.as_default():
       y_true_array = sess.run(y_true)
       y_pred_array = sess.run(y_pred)
    ....................

我得到以下错误

InvalidArgumentError: 2 root error(s) found. (0) Invalid argument: You must feed a value for placeholder tensor 'iterator' with dtype resource (1) Invalid argument: You must feed a value for placeholder tensor 'iterator' with dtype resource

我尝试将numpy从1.19.5降级到1.18.5,但也没有成功,我得到了相同的错误

正在尝试tf.numpy_function()

import tensorflow as tf

def numpy_function(y_true, y_pred):
    # get the y_true and y_pred tensors as 1-D numpy array
    y_true_array = y_true
    y_pred_array = y_pred
    .... calculations ....
    precision = precision_score(y_true_array, y_pred_array, average="macro", zero_division=0)
    precision_per_class.append(precision_score(y_true_array, y_pred_array, average=None, zero_division=0))
    return precision

作为度量传递的函数

@tf.function
def precision_macro(y_true, y_pred):
    score = tf.numpy_function(numpy_function, [y_true, y_pred], tf.float32)

    return score

我这样编译,其中model_metrics = ['accuracy', precision_macro]

model.compile(optimizer=keras.optimizers.RMSprop(learning_rate=lr),
                      loss='binary_crossentropy',
                      metrics=model_metrics)

我得到以下错误

TypeError: 'Tensor' object does not support item assignment

我正在使用keras = 2.6.{}{}

有人能帮我吗


Tags: andtherunnumpytruegettfdef
1条回答
网友
1楼 · 发布于 2024-04-29 12:37:56

你可以试试tf.numpy_function。它接收一个函数作为参数,并将在传入函数之前自动将张量转换为numpy数组,并将函数的输出转换回张量。见示例:

def my_numpy_func(y_true, y_pred):
  # y_true and y_pred are already numpy arrays
  # put all numpy or sklearn codes here
  return metrics.mean_squared_error(y_true, y_pred)

@tf.function
def custom_metric(y_true, y_pred):
  # y_true and y_pred are tensors
  # no numpy or sklearn code is allowed here
  score = tf.numpy_function(my_numpy_func, [y_true, y_pred], tf.float32)
  return score

它将允许您拥有run_eagerly = False,并且在理论上,运行得更快,因为大多数计算都是在图形模式下进行的,除了这个自定义度量

相关问题 更多 >