Keras custom loss gives AttributeError: 'int' object has no attribute 'get_shape'

I'm using keras and trying to create a custom loss. When I want to use maximum function I'm getting the following error.

def custom_loss(y_true, y_pred):
        return K.maximum(y_true - y_pred)
File "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'

What's wrong with this? Why am I getting it?

keraskeras-backend
3 votesLP190.00
1 Answers
JO220.00
3

Try this one

# Define value1 and value2 variables
def custom_loss(y_true, y_pred):
    return K.max(y_true - y_pred)

loss = custom_loss(value1, value2)

sess = K.get_session()
print(sess.run(loss))

You get that error, because you use maximum function, which takes more than 1 argument, but in your code you pass just 1 value.

Reply
Couldn't find what you were looking for?and we will find an expert to answer.
How helpful was this page?