A problem with a multilabel classification and custom function

username: chrismaliszewski
cooked: <p>I’m looking for some small ideas/advice about how I could proceed with the problem I have.</p>
<p>I’m having difficulties creating a custom loss function for the problem I’m trying to resolve. My inputs are images of the kind (A.).<br>
They seem to be a total mess but it’s possible to get information from them, certain classes, where outputs look as follows (B.), so ones in the indices of the certain class.</p>
<p>In the prove of concept I was using BinaryCrossentropy loss function. Its results were fine, (C.)<br>
(left: the results of the BC, right: expected output)</p>
<p>But using BC for the problem has a big issue: it shows the accuracy of, let’s say, 99% but actually it can be just 60%. To calculate correct accuracy I was using NumPy cosine similarity function.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/7/a71423ada949770e262071f204b7cfbaa70ca177.png" data-download-href="https://discuss.pytorch.org/uploads/default/a71423ada949770e262071f204b7cfbaa70ca177" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/a/7/a71423ada949770e262071f204b7cfbaa70ca177_2_690x443.png" alt="image" data-base62-sha1="nQ2SVDQy6QA1wtUSzPQX6wN3Nfp" width="690" height="443" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/a/7/a71423ada949770e262071f204b7cfbaa70ca177_2_690x443.png, https://discuss.pytorch.org/uploads/default/optimized/3X/a/7/a71423ada949770e262071f204b7cfbaa70ca177_2_1035x664.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/a/7/a71423ada949770e262071f204b7cfbaa70ca177_2_1380x886.png 2x" data-dominant-color="C2BCD3"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1456×936 122 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>The problem has now evolved and I BC is not enough anymore so I have to create a custom loss function. I tried using CosineSimilarity inside a custom loss function but it is not enough and, actually, it gives very bad results for some reason.</p>
<p>Could you suggest an approach that could help to achieve the outputs I need, that’s sharp, narrow, high spikes?</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="chrismaliszewski" data-post="1" data-topic="96158">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/chrismaliszewski/48/28746_2.png" class="avatar"> chrismaliszewski:</div>
<blockquote>
<p>it shows the accuracy of, let’s say, 99% but actually it can be just 60%. To calculate correct accuracy I was using NumPy cosine similarity function.</p>
</blockquote>
</aside>
<p>Could you explain this issue a bit more?<br>
Do you think the accuracy calculation is wrong or could there be some bugs in the output computation?</p>

username: chrismaliszewski
cooked: <p><strong>Explanation:</strong> The loss of a model goes to very small numbers and the accuracy to the very high ones very fast, within a few epochs. But these numbers are illusory; the results, in reality, are far from perfect.</p>
<p>Because of that, I added another level of calculating accuracy. Instead of relying on the model one and I calculate it with NumPy cosine similarity function. In reality, the accuracy after a few epochs is just 30-60% using the above function, not 95%&gt;. In the previous version of the problem I, fortunately, was able to achieve 98% accuracy using cosine similarity after ~25 epochs.</p>
<hr>
<p><strong>Additional comment on the matter:</strong> I’m nooby in terms of ML. The newest problem I am trying to solve is far more complex than the previous one. It pushes me to create a custom loss function (LF). I think it should behave similarly to the binary cross-entropy LF as it so far did but, at the same time, it should somewhat be similar to cosine similarity to further punish the model for worse accuracy and to eliminate the necessity of calculating NumPy cosine similarity. Unfortunately, calculating them both together in one LF (with sums, multiplications, substractions) doesn’t provide good results, as I am not able to achieve even ~75% of the model’s accuracy.</p>
<p>I am not sure what direction I should go anymore. I think I am lacking some basic knowledge of how loss functions operate, how to point a model to the direction I want it to go rather than it, the problem, being a computational one.<br>
I’ll be grateful for any advice.</p>