I am new to neural networks and keras and am having trouble writing this custom loss function:
I am using TensorFlow as backend. I saw other examples and wrote the loss function in this way:
from keras import backend as K
def depth_loss_func(pred_depth,actual_depth):
n = pred_depth.shape[0]
di = K.log(pred_depth)-K.log(actual_depth)
di_sq = K.square(di)
sum_d = K.sum(di)
sum_d_sq = K.sum(di_sq)
loss = ((1/n)*sum_d_sq)-((1/(n*n))*sum_d*sum_d) # getting an error in this step
return loss
The error I am getting is :
TypeError: unsupported operand type(s) for /: 'int' and 'Dimension'
Also I am not sure how to incorporate the learning rate in the loss function. Thanks for your help.
Instead of using "n", which seems not to be the most elegant way in my opinion, try using the K.mean
function:
di = K.log(pred_depth)-K.log(actual_depth)
di_mean = K.mean(di)
sq_mean = K.mean(K.square(di))
loss = (sq_mean - (lamb*di_mean*di_mean)) # getting an error in this step