Reputation: 16998
I have as my output a 1-D array of 5 elements, it looks like this in the model:
out_vector = Dense(out_count, activation='relu', name='out_vector')(network_layer_3)
where out_count
is 5 (not that it really matters). When I compare it to true_out_vector
, another 5D array, I want the loss to be the "maximum of the absolute differences of the elements".
Simple example what I mean:
v1 = [94, 1000, 50, 85, 23]
v2 = [100, 430, 88, 12, 90]
I want my loss to equal the biggest absolute difference, which is |1000 - 430| = 570
clearly, since element 2 has this biggest difference. I'm having trouble making this happen in Keras. Here's what I've tried:
def customLoss(yTrue,yPred):
return K.maximum(K.abs(yTrue - yPred))
But I get the error:
File "C:\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 609, in ndim
dims = x.get_shape()._dims
AttributeError: 'int' object has no attribute 'get_shape'
I'm sure there should be a simple way to do what I'm going after.
Upvotes: 1
Views: 1303
Reputation: 1329
How about this:
from keras import backend as K
v1 = K.constant([94, 1000, 50, 85, 23])
v2 = K.constant([100, 430, 88, 12, 90])
def customLoss(yTrue, yPred):
return K.max(K.abs(yTrue - yPred))
result = customLoss(v1, v2)
sess = K.get_session()
print(sess.run(result))
and the output is:
570.0
NB: The method K.maximum
takes two tensors while you are passing only one
Upvotes: 1