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\", 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?

3 votesLP199.00
1 Answers

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()

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

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