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