0

I want to build a network that, for a given input, predicts the hour of the day. The result should be in range 0 to 24.

I tried to solve it as a classification problem but that doesn't seem to be the right way. My problem is that I don't know how to build a cyclic loss function. For example, in my case, if the network gets an output of 1 and the true label is 23 I want the distance to be 2 and not 22. Is there a layer for that which I can use?

Praveen
  • 6,872
  • 3
  • 43
  • 62
GiladD
  • 33
  • 4
  • Welcome to SO! This is a good question; it should not have been voted down. However, it is always preferred that you show some of the work you did, ideally with a code or pseudocode snippet. That will make it less likely for your question to be misinterpreted as one lacking research and effort. – Praveen Dec 07 '16 at 21:44

1 Answers1

1

As far as I know there is no pre written cyclic loss function. For cyclic loss you should write your own loss function like this:

import keras.backend as K
def cyclic_loss(y_true, y_pred): 
    return K.min((y_pred +24 - y_true) % 24, (y_true + 24 - y_pred) % 24)

model.compile(optimizer='sgd', loss=cyclic_loss, metrics=['acc'])

However it is not a classification problem if you define your loss like that. If you want a classification problem you should encode your output with one-hot encoding and use cross-entropy as loss function. Then you have a probability for each hour of the day and you take the hour with the highest probability.

As a regression task you can use the cyclic loss function as described above.

Thomas Pinetz
  • 6,948
  • 2
  • 27
  • 46
  • Thank you very much. Its seems like i should use it as regression task.Is it the same loss if i try to write it in tensorflow? – GiladD Dec 05 '16 at 13:35
  • If you use keras as a wrapper yes. If not you will have to use the tensorflow backend. I have not had the plessure of using the tensorflow backend myself directly so I can not say for certain though. – Thomas Pinetz Dec 05 '16 at 14:20
  • ok thanks i will work with keras. is there a way to ensure that the output of y_pred will be in modulo 24?I put a relu to make y_pred>0(Dense(1,activation='relu',name=y_pred) but way wouldnt it go higer then 24?the loss function in concave and not convex – GiladD Dec 05 '16 at 17:30
  • The first thing that comes to mind is to use keras.backend.switch (https://keras.io/backend/#switch) to return a really high loss in case the result is higher than 24 or lower than 0. Then the function would still be convex. This should then act the same as a threshold function. – Thomas Pinetz Dec 06 '16 at 07:54