Loss function (and encoding?) for anglesEncoding Angle Data for Neural NetworkWhat is a correct loss for a model predicting angles from images?Weighted loss function for non-random sampleBad performance with ReLU activation function on MNIST data setCross Entropy Loss for One Hot EncodingTensorflow 2.0/Keras - loss function with multiple inputsRegarding loss function

Bit floating sequence

More than 3 domains hosted on IP

Filling attribute tables with values from the same attribute table

Does the word voltage exist in academic engineering?

I won a car in a poker game. How is that taxed in Canada?

Why is Sojdlg123aljg a common password?

What geological processes could form the Crystal Desert on the planet Thra in the Dark Crystal?

Was Rosie the Riveter sourced from a Michelangelo painting?

Examples where "thin + thin = nice and thick"

Sinning and G-d's will, what's wrong with this logic?

Professor refuses to write a recommendation letter to students who haven't written a research paper with him

Do aarakocra have arms as well as wings?

Book/story which features a mental link to a prophet

If every star in the universe except the Sun were destroyed, would we die?

Is mountain bike good for long distances?

Owner keeps cutting corners and poaching workers for his other company

antimatter annihilation in stars

Is Sanskrit really the mother of all languages?

Friend is very nit picky about side comments I don't intend to be taken too seriously

What can we do about our 9-month-old putting fingers down his throat?

Why can't some airports handle heavy aircraft while others do it easily (same runway length)?

How can I know what hashing algorithm SQL Server used to decrypt the encrypted data when using the function DECRYPTBYPASSPHRASE?

Why did Tony's Arc Reactor do this?

First Number to Contain Each Letter



Loss function (and encoding?) for angles


Encoding Angle Data for Neural NetworkWhat is a correct loss for a model predicting angles from images?Weighted loss function for non-random sampleBad performance with ReLU activation function on MNIST data setCross Entropy Loss for One Hot EncodingTensorflow 2.0/Keras - loss function with multiple inputsRegarding loss function






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








3












$begingroup$


I'm training a network to predict the angle of arrival of a signal. Labels are single values in the [-180, 180) interval.



I'm seeing a discontinuity in predictions around ±180 degrees, which makes sense as losses around that gap are incorrectly calculated by root mean square error.



I'm looking for a loss function that works in a modular way. A difference between 175 and -175 degrees should be calculated as 10 (instead of 350), if such thing exists.



It's my understanding that such a function introduces a discontinuity and thus may not be a valid approach. I'm looking for some guidance about how to deal with these kind of circular variables like angles, hour of the day, day of the week...



This has been addressed in the question "Encoding Angle Data for Neural Network", and I feel preserving linearity in angle variables is important (my input is also several angles), and I'm not getting good results with the sin/cos encoding approach proposed in that question. The problem is also discussed here: What is a correct loss for a model predicting angles from images?.



Here is what I'm doing currently, which works quite well with angles (-180, 180).



def metric_stddev_diff(y_true, y_pred):
return tf.keras.backend.std(y_true - y_pred)

def model_create():

model = tf.keras.Sequential([
tf.keras.layers.Dense(128, activation='sigmoid', dtype='float64'),
tf.keras.layers.Dense(64, activation='linear', dtype='float64'),
tf.keras.layers.Dense(1, activation='linear', dtype='float64'),
])

model.compile(optimizer='adam', # 'rmsprop' 'adam',
loss='mean_absolute_error', # 'mean_absolute_error' 'mean_squared_error' 'sparse_categorical_crossentropy'
metrics=['mean_absolute_error', metric_stddev_diff])

return model









share|cite|improve this question









New contributor



jjmontes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$













  • $begingroup$
    How did you try to compare after you map to $sin theta, cos theta$?
    $endgroup$
    – Cowboy Trader
    6 hours ago











  • $begingroup$
    I kept using "mean absolute difference" which was working nicely in the angles case. I suspect that's wrong... I don't really know how to interpret this metric in the $sin theta, cos theta$ case and I guess the loss function is not valid.
    $endgroup$
    – jjmontes
    4 hours ago


















3












$begingroup$


I'm training a network to predict the angle of arrival of a signal. Labels are single values in the [-180, 180) interval.



I'm seeing a discontinuity in predictions around ±180 degrees, which makes sense as losses around that gap are incorrectly calculated by root mean square error.



I'm looking for a loss function that works in a modular way. A difference between 175 and -175 degrees should be calculated as 10 (instead of 350), if such thing exists.



It's my understanding that such a function introduces a discontinuity and thus may not be a valid approach. I'm looking for some guidance about how to deal with these kind of circular variables like angles, hour of the day, day of the week...



This has been addressed in the question "Encoding Angle Data for Neural Network", and I feel preserving linearity in angle variables is important (my input is also several angles), and I'm not getting good results with the sin/cos encoding approach proposed in that question. The problem is also discussed here: What is a correct loss for a model predicting angles from images?.



Here is what I'm doing currently, which works quite well with angles (-180, 180).



def metric_stddev_diff(y_true, y_pred):
return tf.keras.backend.std(y_true - y_pred)

def model_create():

model = tf.keras.Sequential([
tf.keras.layers.Dense(128, activation='sigmoid', dtype='float64'),
tf.keras.layers.Dense(64, activation='linear', dtype='float64'),
tf.keras.layers.Dense(1, activation='linear', dtype='float64'),
])

model.compile(optimizer='adam', # 'rmsprop' 'adam',
loss='mean_absolute_error', # 'mean_absolute_error' 'mean_squared_error' 'sparse_categorical_crossentropy'
metrics=['mean_absolute_error', metric_stddev_diff])

return model









share|cite|improve this question









New contributor



jjmontes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$













  • $begingroup$
    How did you try to compare after you map to $sin theta, cos theta$?
    $endgroup$
    – Cowboy Trader
    6 hours ago











  • $begingroup$
    I kept using "mean absolute difference" which was working nicely in the angles case. I suspect that's wrong... I don't really know how to interpret this metric in the $sin theta, cos theta$ case and I guess the loss function is not valid.
    $endgroup$
    – jjmontes
    4 hours ago














3












3








3


1



$begingroup$


I'm training a network to predict the angle of arrival of a signal. Labels are single values in the [-180, 180) interval.



I'm seeing a discontinuity in predictions around ±180 degrees, which makes sense as losses around that gap are incorrectly calculated by root mean square error.



I'm looking for a loss function that works in a modular way. A difference between 175 and -175 degrees should be calculated as 10 (instead of 350), if such thing exists.



It's my understanding that such a function introduces a discontinuity and thus may not be a valid approach. I'm looking for some guidance about how to deal with these kind of circular variables like angles, hour of the day, day of the week...



This has been addressed in the question "Encoding Angle Data for Neural Network", and I feel preserving linearity in angle variables is important (my input is also several angles), and I'm not getting good results with the sin/cos encoding approach proposed in that question. The problem is also discussed here: What is a correct loss for a model predicting angles from images?.



Here is what I'm doing currently, which works quite well with angles (-180, 180).



def metric_stddev_diff(y_true, y_pred):
return tf.keras.backend.std(y_true - y_pred)

def model_create():

model = tf.keras.Sequential([
tf.keras.layers.Dense(128, activation='sigmoid', dtype='float64'),
tf.keras.layers.Dense(64, activation='linear', dtype='float64'),
tf.keras.layers.Dense(1, activation='linear', dtype='float64'),
])

model.compile(optimizer='adam', # 'rmsprop' 'adam',
loss='mean_absolute_error', # 'mean_absolute_error' 'mean_squared_error' 'sparse_categorical_crossentropy'
metrics=['mean_absolute_error', metric_stddev_diff])

return model









share|cite|improve this question









New contributor



jjmontes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$




I'm training a network to predict the angle of arrival of a signal. Labels are single values in the [-180, 180) interval.



I'm seeing a discontinuity in predictions around ±180 degrees, which makes sense as losses around that gap are incorrectly calculated by root mean square error.



I'm looking for a loss function that works in a modular way. A difference between 175 and -175 degrees should be calculated as 10 (instead of 350), if such thing exists.



It's my understanding that such a function introduces a discontinuity and thus may not be a valid approach. I'm looking for some guidance about how to deal with these kind of circular variables like angles, hour of the day, day of the week...



This has been addressed in the question "Encoding Angle Data for Neural Network", and I feel preserving linearity in angle variables is important (my input is also several angles), and I'm not getting good results with the sin/cos encoding approach proposed in that question. The problem is also discussed here: What is a correct loss for a model predicting angles from images?.



Here is what I'm doing currently, which works quite well with angles (-180, 180).



def metric_stddev_diff(y_true, y_pred):
return tf.keras.backend.std(y_true - y_pred)

def model_create():

model = tf.keras.Sequential([
tf.keras.layers.Dense(128, activation='sigmoid', dtype='float64'),
tf.keras.layers.Dense(64, activation='linear', dtype='float64'),
tf.keras.layers.Dense(1, activation='linear', dtype='float64'),
])

model.compile(optimizer='adam', # 'rmsprop' 'adam',
loss='mean_absolute_error', # 'mean_absolute_error' 'mean_squared_error' 'sparse_categorical_crossentropy'
metrics=['mean_absolute_error', metric_stddev_diff])

return model






loss-functions tensorflow keras circular-statistics






share|cite|improve this question









New contributor



jjmontes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share|cite|improve this question









New contributor



jjmontes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|cite|improve this question




share|cite|improve this question








edited 4 hours ago







jjmontes













New contributor



jjmontes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked 8 hours ago









jjmontesjjmontes

1164 bronze badges




1164 bronze badges




New contributor



jjmontes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




jjmontes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • $begingroup$
    How did you try to compare after you map to $sin theta, cos theta$?
    $endgroup$
    – Cowboy Trader
    6 hours ago











  • $begingroup$
    I kept using "mean absolute difference" which was working nicely in the angles case. I suspect that's wrong... I don't really know how to interpret this metric in the $sin theta, cos theta$ case and I guess the loss function is not valid.
    $endgroup$
    – jjmontes
    4 hours ago

















  • $begingroup$
    How did you try to compare after you map to $sin theta, cos theta$?
    $endgroup$
    – Cowboy Trader
    6 hours ago











  • $begingroup$
    I kept using "mean absolute difference" which was working nicely in the angles case. I suspect that's wrong... I don't really know how to interpret this metric in the $sin theta, cos theta$ case and I guess the loss function is not valid.
    $endgroup$
    – jjmontes
    4 hours ago
















$begingroup$
How did you try to compare after you map to $sin theta, cos theta$?
$endgroup$
– Cowboy Trader
6 hours ago





$begingroup$
How did you try to compare after you map to $sin theta, cos theta$?
$endgroup$
– Cowboy Trader
6 hours ago













$begingroup$
I kept using "mean absolute difference" which was working nicely in the angles case. I suspect that's wrong... I don't really know how to interpret this metric in the $sin theta, cos theta$ case and I guess the loss function is not valid.
$endgroup$
– jjmontes
4 hours ago





$begingroup$
I kept using "mean absolute difference" which was working nicely in the angles case. I suspect that's wrong... I don't really know how to interpret this metric in the $sin theta, cos theta$ case and I guess the loss function is not valid.
$endgroup$
– jjmontes
4 hours ago











1 Answer
1






active

oldest

votes


















3














$begingroup$

Almost any loss function that is symmetric and differentiable at $0$ is locally quadratic. Thus, you don't have to be too fussy when searching for a good loss function when you need symmetry and differentiability.



Notice that with nearby angles $phi$ and $theta,$ the Taylor series expansion of the cosine gives



$$mathcalL(phi,theta)=2(1 - cos(phi-theta)) = (phi-theta)^2 + O((phi-theta)^4)$$



is locally quadratic at $phi-theta=0$ (and all integral multiples of $2pi$) through third order. Moreover, this function of $phi$ and $theta$ isn't badly behaved: it's defined for all angles, is differentiable everywhere, and--most importantly--respects the modular nature of angle comparison. Thus $mathcalL$ is a natural and simple angular version of a quadratic loss. This would be a good place to start your analysis.



If you need more flexibility, consider defining your loss as a function of $sqrt2(1-cos(phi-theta)):$ clearly this is a circular analog of the absolute difference.






share|cite|improve this answer









$endgroup$

















    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "65"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );







    jjmontes is a new contributor. Be nice, and check out our Code of Conduct.









    draft saved

    draft discarded
















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstats.stackexchange.com%2fquestions%2f425234%2floss-function-and-encoding-for-angles%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    $begingroup$

    Almost any loss function that is symmetric and differentiable at $0$ is locally quadratic. Thus, you don't have to be too fussy when searching for a good loss function when you need symmetry and differentiability.



    Notice that with nearby angles $phi$ and $theta,$ the Taylor series expansion of the cosine gives



    $$mathcalL(phi,theta)=2(1 - cos(phi-theta)) = (phi-theta)^2 + O((phi-theta)^4)$$



    is locally quadratic at $phi-theta=0$ (and all integral multiples of $2pi$) through third order. Moreover, this function of $phi$ and $theta$ isn't badly behaved: it's defined for all angles, is differentiable everywhere, and--most importantly--respects the modular nature of angle comparison. Thus $mathcalL$ is a natural and simple angular version of a quadratic loss. This would be a good place to start your analysis.



    If you need more flexibility, consider defining your loss as a function of $sqrt2(1-cos(phi-theta)):$ clearly this is a circular analog of the absolute difference.






    share|cite|improve this answer









    $endgroup$



















      3














      $begingroup$

      Almost any loss function that is symmetric and differentiable at $0$ is locally quadratic. Thus, you don't have to be too fussy when searching for a good loss function when you need symmetry and differentiability.



      Notice that with nearby angles $phi$ and $theta,$ the Taylor series expansion of the cosine gives



      $$mathcalL(phi,theta)=2(1 - cos(phi-theta)) = (phi-theta)^2 + O((phi-theta)^4)$$



      is locally quadratic at $phi-theta=0$ (and all integral multiples of $2pi$) through third order. Moreover, this function of $phi$ and $theta$ isn't badly behaved: it's defined for all angles, is differentiable everywhere, and--most importantly--respects the modular nature of angle comparison. Thus $mathcalL$ is a natural and simple angular version of a quadratic loss. This would be a good place to start your analysis.



      If you need more flexibility, consider defining your loss as a function of $sqrt2(1-cos(phi-theta)):$ clearly this is a circular analog of the absolute difference.






      share|cite|improve this answer









      $endgroup$

















        3














        3










        3







        $begingroup$

        Almost any loss function that is symmetric and differentiable at $0$ is locally quadratic. Thus, you don't have to be too fussy when searching for a good loss function when you need symmetry and differentiability.



        Notice that with nearby angles $phi$ and $theta,$ the Taylor series expansion of the cosine gives



        $$mathcalL(phi,theta)=2(1 - cos(phi-theta)) = (phi-theta)^2 + O((phi-theta)^4)$$



        is locally quadratic at $phi-theta=0$ (and all integral multiples of $2pi$) through third order. Moreover, this function of $phi$ and $theta$ isn't badly behaved: it's defined for all angles, is differentiable everywhere, and--most importantly--respects the modular nature of angle comparison. Thus $mathcalL$ is a natural and simple angular version of a quadratic loss. This would be a good place to start your analysis.



        If you need more flexibility, consider defining your loss as a function of $sqrt2(1-cos(phi-theta)):$ clearly this is a circular analog of the absolute difference.






        share|cite|improve this answer









        $endgroup$



        Almost any loss function that is symmetric and differentiable at $0$ is locally quadratic. Thus, you don't have to be too fussy when searching for a good loss function when you need symmetry and differentiability.



        Notice that with nearby angles $phi$ and $theta,$ the Taylor series expansion of the cosine gives



        $$mathcalL(phi,theta)=2(1 - cos(phi-theta)) = (phi-theta)^2 + O((phi-theta)^4)$$



        is locally quadratic at $phi-theta=0$ (and all integral multiples of $2pi$) through third order. Moreover, this function of $phi$ and $theta$ isn't badly behaved: it's defined for all angles, is differentiable everywhere, and--most importantly--respects the modular nature of angle comparison. Thus $mathcalL$ is a natural and simple angular version of a quadratic loss. This would be a good place to start your analysis.



        If you need more flexibility, consider defining your loss as a function of $sqrt2(1-cos(phi-theta)):$ clearly this is a circular analog of the absolute difference.







        share|cite|improve this answer












        share|cite|improve this answer



        share|cite|improve this answer










        answered 2 hours ago









        whuberwhuber

        217k34 gold badges477 silver badges870 bronze badges




        217k34 gold badges477 silver badges870 bronze badges
























            jjmontes is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded

















            jjmontes is a new contributor. Be nice, and check out our Code of Conduct.












            jjmontes is a new contributor. Be nice, and check out our Code of Conduct.











            jjmontes is a new contributor. Be nice, and check out our Code of Conduct.














            Thanks for contributing an answer to Cross Validated!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstats.stackexchange.com%2fquestions%2f425234%2floss-function-and-encoding-for-angles%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

            Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

            François Viète Contents Biography Work and thought Bibliography See also Notes Further reading External links Navigation menup. 21Google Bookspp. 75–77Google BooksDe thou (from University of Saint Andrews)ArchivedGoogle BooksGoogle BooksGoogle BooksGoogle booksGoogle Bookscc-parthenay.frL'histoire universelle (fr)Universal History (en)ArchivedAdsabs.harvard.eduPagesperso-orange.frArchive.orgChikara Sasaki. Descartes' mathematical thought p.259Google BooksGoogle BooksGoogle Bookspp. 152 and onwardGoogle BooksGoogle BooksScribd.comGoogle Books1257-7979Google BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGallica.bnf.frGoogle BooksGoogle Books"François Viète"Francois Viète: Father of Modern Algebraic NotationThe Lawyer and the GamblerAbout TarporleySite de Jean-Paul GuichardL'algèbre nouvelle"About the Harmonicon"cb120511976(data)1188044800000 0001 0913 5903n82164680ola2013766880073431702w6vt1sb70287374827140948071409480