In Keras I'm trying to figure out how to calculate a custom metric or loss that filters out or masks some values so that they don't contribute to the returned value. I'm stuck on how to get a tensor slice or how to iterate with an if: over the values in the tensor to select the values of interest.
I happen to be using the Tensorflow backend but would like to do something that is portable.
Attached is a rough outline of what I'm trying to do but it throws the error: TypeError: 'Tensor' object does not support item assignment
def my_filtered_mse(y_true, y_pred):
#Return Mean Squared Error for a subset of values
error = y_pred - y_true
error[y_true == 0.0] = 0 #Don't include errors when y_true is zero
# The previous like throws the error : TypeError: 'Tensor' object does not support item assignment
return K.mean(K.square(error))
#...other stuff ...
model.compile(optimizer=optimizers.adam(),
loss='mean_squared_error',
metrics=[my_filtered_mse])