How to Convert Keras Tensor to TensorFlow Tensor?

Viewed 2081

I met some problems when training with tensorflow.keras. I defined a loss function with tensorflow.keras.backend. The code is as follows:

import tensorflow.keras.backend as K

def gradient_penalty_loss(y_true, y_pred, averaged_samples, weight):
    gradients = K.gradients(y_pred, averaged_samples)[0]
    gradients_sqr = K.square(gradients)
    gradient_penalty = K.sum(gradients_sqr,
                              axis=np.arange(1, len(gradients_sqr.shape)))

    # (weight / 2) * ||grad||^2
    # Penalize the gradient norm
    return K.mean(gradient_penalty) * (weight / 2)

def hinge_d(y_true, y_pred):
    return K.mean(K.relu(1.0 - (y_true * y_pred)))

def w_loss(y_true, y_pred):
    return K.mean(y_true * y_pred)

However, an error occurred with the following statements:

Cannot convert a symbolic Keras input/output to a numpy array. This error may indicate that you're trying to pass a symbolic value to a NumPy call, which is not supported. Or, you may be trying to pass Keras symbolic inputs/outputs to a TF API that does not register dispatching, preventing Keras from automatically converting the API call to a lambda layer in the Functional Model.

After searching for some information, I noticed that it might because the output of the loss function is a Keras Tensor, which cannot be manipulated by the TensorFlow. So how can I deal with this problem? Thanks!

1 Answers

This happened due to latest version of numpy (1.20). Downgrade the numpy version as follows (assuming you are using conda):

conda install -c conda-forge numpy=1.19.5

Related