Home > Net >  How to solve, Cannot convert a symbolic Tensor (IteratorGetNext:1) to a numpy array
How to solve, Cannot convert a symbolic Tensor (IteratorGetNext:1) to a numpy array

Time:09-21

I have passed a custom loss function to the Keras model, where I am trying to inverse_transform my labels before calculating the rmse score.

I have transformed my labels of shape (n,1) by standard scaler, where n represents the number of records in label.

My code

# standardization
lab_scaler2 = StandardScaler().fit(label2)
scaled_lab2 = lab_scaler2.transform(label2)

# custom loss function
from keras import backend as k
def root_mean_squared_error(y_true, y_pred):
    try:
        y_true = lab_scaler2.inverse_transform(y_true)
    except Exception as e:
        logging.error(f'y_true: {e}')
    try:
        y_pred = lab_scaler2.inverse_transform(y_pred.reshape(-1,1))
    except Exception as e:
        logging.error(f'y_pred: {e}')
    return k.sqrt(k.mean(k.square(y_pred - y_true)))

Error I am getting

but it is giving an error, which I have recorded in a log file. The error is as follows

# log file
ERROR:root:y_true: Cannot convert a symbolic Tensor (IteratorGetNext:1) to a numpy array. 
This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported
ERROR:root:y_pred: 
        'Tensor' object has no attribute 'reshape'.
        If you are looking for numpy-related methods, please run the following:
        from tensorflow.python.ops.numpy_ops import np_config
        np_config.enable_numpy_behavior()

I found some related problem but it doesn't work for me

CodePudding user response:

As suggested by snoopy, you can't call numpy function in a loss function, even converting to a numpy array wont work, for all the problems involving gradient.

But in your case what you are trying to call is just a Standard Scaler, this method just does:

z = (x - u) / s

where u is the mean of the training, and s is the standard deviation of the training.

standard scaler is a linear trasformation, just do it by hand, you just need to compute the mean and the variance of the data, instead of calling the scaler just do a multiplication and an addition (the inverse operation of the scaler), find mean and variance and do the inverse by hand, its a one line expression:

z = (x - u) / s -> (z * s) u = x.

just take the tensor, multiply it for the variance and sum the mean.

  • Related