I am writing code about the custom loss function and custom metrics function in Keras. Now it is wrong with the code.
I don't know what values should these custom functions return, a scalar or a tensor which size is 'batch_size'? I tried all of them, surprisingly they all work while the results are different.
So I want to know which of them is right. What is the computing mechanism of 'loss' and 'metrics' when it completes a epoch in training?
The shapes of y_true
and y_predict
are (batch_size,1)
def loss_scalar(y_true,y_pred):
main_loss=K.sum(K.reshape((1+0.2*(K.abs((5-y_true)-5/2)))*K.square(y_true-y_pred),shape=(-1,)))
def loss_tensor(y_true,y_pred):
main_loss=(K.reshape((1+0.2*(K.abs((5-y_true)-5/2)))*K.square(y_true-y_pred),shape=(-1,))
def mae_tensor(y_true,y_pred):
return (K.mean(K.abs(y_true-y_pred),axis=-1))
def mae_scalar(y_true,y_pred):
return K.sum(K.mean(K.abs(y_true-y_pred),axis=-1))