Selectively Iterate over Tensor
The general approach is to use binary masks. Tensorflow provides several boolean functions such as tf.equal and tf.not_equal. For selecting only enterings which are equal to a certain value, you could use tf.equal and then multiply the loss tensor by the obtained binary mask.
I am currently building a CNN with Keras and need to define a custom loss function. I would only like to consider specific parts of my data in the loss and ignore others based on a certain parameter value. But, I am having trouble iterating over the Tensor objects that the Keras loss function expects.
Is there a simple way for me to compute the mean squared error between two Tensors, only looking at selected values in the Tensor?
For example, each Tensor in my case represents a 2D 16x16 grid, with each cell having 2 parameters - shape (16, 16, 2). I only want to compare cells where one of their parameters is equal to 1.