3

I am training a CNN to regress on 4 targets related to a given image. Within the image is a point of interest whose position can be defined by phi, and theta (corresponding to x and y of a normal cartesian axis). The targets for my model are sin(phi), cos(phi), sin(theta), and cos(that). I use L2 mean squared error as the loss function for both phi and theta.

The issue I have is that not every image has equal inherent value. Some images have a higher probability of being encountered where as others not so much. This probability scores scale on a metric from 1 -> 1e-100.

My question is how would one incorporate these weights into a loss function so as to make the model better?

Ben Reiniger
  • 12,855
  • 3
  • 20
  • 63

1 Answers1

4

In TensorFlow, inside the fit() method there is one parameter called sample_weights which does what you are looking for:

sample_weight: Optional NumPy array of weights for the training samples, used for weighting the loss function (during training only). You can either pass a flat (1D) NumPy array with the same length as the input samples (1:1 mapping between weights and samples), or in the case of temporal data, you can pass a 2D array with shape (samples, sequence_length), to apply a different weight to every timestep of every sample. This argument is not supported when x is a dataset, generator, or keras.utils.PyDataset instance, instead provide the sample_weights as the third element of x. Note that sample weighting does not apply to metrics specified via the metrics argument in compile(). To apply sample weighting to your metrics, you can specify them via the weighted_metrics in compile() instead.

source

rehaqds
  • 1,801
  • 4
  • 13