0

I am trying to define a loss function in Keras

def rmseApprox(y_true, y_pred):
    dum = y_pred
    dum[y_pred>=0]=1.1
    dum[y_pred<0]=1

    return k.abs(K.mean(y_true - dum*y_pred), axis=-1)

which increase the positive values by a factor of 1.1 and the compare it with the true values. I got the following error:

TypeError: 'Tensor' object does not support item assignment
Ioannis Nasios
  • 8,292
  • 4
  • 33
  • 55

1 Answers1

0

The loss function is a tensor and part of the computational graph. It therefore must be defined using the keras backend, and doesn't behave like a "regular" numpy array.

This example should work for you:

def rmseApprox(y_true, y_pred):
    y_pred_corrected = y_pred*(1.05 + K.sign(y_pred)*0.05)
    return K.abs(K.mean(y_true - y_pred_corrected, axis=-1))

Please note that in the above code, the weight for the case y_pred==0 will be 1.05.

Mark Loyman
  • 1,983
  • 1
  • 14
  • 23