Keras: Not understanding error

Created on 6 Oct 2017  路  8Comments  路  Source: keras-team/keras

I've written my own loss function which seems to get stuck in this error which I can't figure out.
Seems it could either be sample_weight or mask, but i don't know what to do with it.

Can anyone help?

image

Most helpful comment

The bug is in not in Keras but in range.
In python3
range( 1, None) ->typeError : 'NoneType' object cannot be interpreted as an integer
Your ndim should be not be None.
Most likely K.n_dim( score_array ) is problematic
(This bug often also strikes in its variant np.zeros(4,5) instead of np.zeros((4,5)) )

Keras expect a one dimensionnal vector as a loss : 1 float loss value by example of a batch.

All 8 comments

Hello
What does a print(ndim) and print(weight_ndim) before line 465 displays ?

print(ndim): none
print(weight_ndim): 1

What does keras compile expect to get back from the loss function? What dimensionality?

The bug is in not in Keras but in range.
In python3
range( 1, None) ->typeError : 'NoneType' object cannot be interpreted as an integer
Your ndim should be not be None.
Most likely K.n_dim( score_array ) is problematic
(This bug often also strikes in its variant np.zeros(4,5) instead of np.zeros((4,5)) )

Keras expect a one dimensionnal vector as a loss : 1 float loss value by example of a batch.

Thanks for clarification, do you know any solution?

It seems to relate quite heavily to tf.map_fn(single_loss, tf.range(tf.shape(y_pred)[0])) if I try to get the shape of this it's unknown and _dims is None.
So it is actually more of a tensorflow problem than keras.
However if anyone knows how to get around it please help.

Okay so I got on by doing this:

full_return = tf.map_fn(single_loss, tf.range(tf.shape(y_pred)[0])) full_return.set_shape((None,)) return full_return

So now it can get the dimensions which is 1 as expected.
As always more problems follow... of course....

Was minor error i was able to compile it now so I will close the issue.

Was this page helpful?
0 / 5 - 0 ratings