3
$\begingroup$

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(\theta)$. 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 whereas others not so much. This probability scores scale on a metric from $1 \to 1e^{-100}$.

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

$\endgroup$
4
  • 2
    $\begingroup$ You're probably going to have to start seriously thinking about floating-point accuracy if you have numbers across 100 orders of magnitude in your model.... I certainly noticed the need for higher precision once when I was looking at forces that operated on a scale about six orders of magnitude smaller than the rest of the problem. $\endgroup$ Commented Feb 1 at 7:50
  • 2
    $\begingroup$ But also, what kind of problem would you be looking at where you would care about an image that you would expect to see one in one googol times as often as another image? I know that in something like particle physics, you might care about one in a billion images, say. A one in a googol image is one that, if you look at a billion images per second for the age of the universe, you're still less likely to find than to win the lottery and get hit by an asteroid at the same time. $\endgroup$ Commented Feb 1 at 7:54
  • $\begingroup$ Thank you for your response! Yes, I was also worried about floating point precision and possibly utilizing one over the exponential factor as a weight value instead of the true value. The weight values come from a Monte Carlo astroparticle simulation. $\endgroup$ Commented Feb 3 at 1:04
  • $\begingroup$ Do you expect to see a one in a googol astroparticle image? I guess I am still a bit confused...if you do not, you could certainly save yourself quite a bit of trouble in your calculations and simulations by ignoring such rare events. $\endgroup$ Commented Feb 3 at 1:30

1 Answer 1

4
$\begingroup$

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

$\endgroup$
2
  • $\begingroup$ Thank you! I unfortunately am using a pytorch framework. Do you know of an analogous method for this library? $\endgroup$ Commented Feb 3 at 1:05
  • $\begingroup$ Have you checked stackoverflow.com/questions/66374709/… ? $\endgroup$ Commented Feb 3 at 8:44

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.