Crossentropy Keras

I work with Keras and I am trying to rewrite categorical_rossentropy using the Keras backend, but I am stuck.

This is my custom function, I only need a weighted amount of cross-entropy:

def custom_entropy( y_true, y_pred):
    y_pred /= K.sum(y_pred, axis=-1, keepdims=True)

    # clip to prevent NaN and Inf's
    y_pred = K.clip(y_pred, K.epsilon(), 1 - K.epsilon())

   loss = y_true * K.log(y_pred) 
   loss = -K.sum(loss, -1)

   return loss

In my program, I create a label_predwith model.predict().

Finally, I:

    label_pred = model.predict(mfsc_train[:,:,5])
    cc = custom_entropy(label, label_pred)
    ce = K.categorical_crossentropy(label, label_pred)

I get the following error:

Traceback (most recent call last):
  File "SAMME_train_all.py", line 47, in <module>
    ce = K.categorical_crossentropy(label, label_pred)
  File "C:\Users\gionata\AppData\Local\Programs\Python\Python36\lib
s\keras\backend\tensorflow_backend.py", line 2754, in categorical_c
    axis=len(output.get_shape()) - 1,
AttributeError: 'numpy.ndarray' object has no attribute 'get_shape'
+5
source share
2 answers

K.categorical_crossentropyfunctions K.categorical_crossentropysuch as K.categorical_crossentropytensors expect.

From your question it is not obvious what a typical one is label. However, we know that model.predictNumPy always returns ndarrays, so we know that label_predit is not a tensor. It is easy to convert, for example (if it is labelalready a tensor),

custom_entropy(label, K.constant(label_pred))

,

K.eval(custom_entropy(label, K.constant(label_pred)))

, model , , ..

label_pred = model(K.constant(mfsc_train[:,:,5]))
cc = custom_entropy(label, label_pred)
ce = K.categorical_crossentropy(label, label_pred)

label_pred, cc ce .

+6

, :

y_true: True labels. TensorFlow/Theano tensor.
y_pred: Predictions. TensorFlow/Theano tensor of the same shape as y_true.

numpy .

+2

Source: https://habr.com/ru/post/1690020/


All Articles